RadioInputType.cpp [plain text]
#include "config.h"
#include "RadioInputType.h"
#include "Frame.h"
#include "HTMLFormElement.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "InputTypeNames.h"
#include "KeyboardEvent.h"
#include "LocalizedStrings.h"
#include "MouseEvent.h"
#include "NodeTraversal.h"
#include "SpatialNavigation.h"
namespace WebCore {
using namespace HTMLNames;
const AtomString& RadioInputType::formControlType() const
{
return InputTypeNames::radio();
}
bool RadioInputType::valueMissing(const String&) const
{
ASSERT(element());
return element()->isInRequiredRadioButtonGroup() && !element()->checkedRadioButtonForGroup();
}
String RadioInputType::valueMissingText() const
{
return validationMessageValueMissingForRadioText();
}
void RadioInputType::handleClickEvent(MouseEvent& event)
{
event.setDefaultHandled();
}
auto RadioInputType::handleKeydownEvent(KeyboardEvent& event) -> ShouldCallBaseEventHandler
{
if (BaseCheckableInputType::handleKeydownEvent(event) == ShouldCallBaseEventHandler::No)
return ShouldCallBaseEventHandler::No;
if (event.defaultHandled())
return ShouldCallBaseEventHandler::Yes;
const String& key = event.keyIdentifier();
if (key != "Up" && key != "Down" && key != "Left" && key != "Right")
return ShouldCallBaseEventHandler::Yes;
ASSERT(element());
if (isSpatialNavigationEnabled(element()->document().frame()))
return ShouldCallBaseEventHandler::Yes;
bool forward = (key == "Down" || key == "Right");
RefPtr<Node> node = element();
while ((node = (forward ? NodeTraversal::next(*node) : NodeTraversal::previous(*node)))) {
if (is<HTMLFormElement>(*node))
break;
if (!is<HTMLInputElement>(*node))
continue;
RefPtr<HTMLInputElement> inputElement = downcast<HTMLInputElement>(node.get());
if (inputElement->form() != element()->form())
break;
if (inputElement->isRadioButton() && inputElement->name() == element()->name() && inputElement->isFocusable()) {
element()->document().setFocusedElement(inputElement.get());
inputElement->dispatchSimulatedClick(&event, SendNoEvents, DoNotShowPressedLook);
event.setDefaultHandled();
return ShouldCallBaseEventHandler::Yes;
}
}
return ShouldCallBaseEventHandler::Yes;
}
void RadioInputType::handleKeyupEvent(KeyboardEvent& event)
{
const String& key = event.keyIdentifier();
if (key != "U+0020")
return;
ASSERT(element());
if (element()->checked())
return;
dispatchSimulatedClickIfActive(event);
}
bool RadioInputType::isKeyboardFocusable(KeyboardEvent* event) const
{
if (!InputType::isKeyboardFocusable(event))
return false;
ASSERT(element());
if (isSpatialNavigationEnabled(element()->document().frame()))
return true;
RefPtr<Element> currentFocusedNode = element()->document().focusedElement();
if (is<HTMLInputElement>(currentFocusedNode)) {
HTMLInputElement& focusedInput = downcast<HTMLInputElement>(*currentFocusedNode);
if (focusedInput.isRadioButton() && focusedInput.form() == element()->form() && focusedInput.name() == element()->name())
return false;
}
return element()->checked() || !element()->checkedRadioButtonForGroup();
}
bool RadioInputType::shouldSendChangeEventAfterCheckedChanged()
{
ASSERT(element());
return element()->checked();
}
void RadioInputType::willDispatchClick(InputElementClickState& state)
{
ASSERT(element());
state.checked = element()->checked();
state.checkedRadioButton = element()->checkedRadioButtonForGroup();
element()->setChecked(true);
}
void RadioInputType::didDispatchClick(Event& event, const InputElementClickState& state)
{
if (event.defaultPrevented() || event.defaultHandled()) {
auto& button = state.checkedRadioButton;
ASSERT(element());
if (button && button->isRadioButton() && button->form() == element()->form() && button->name() == element()->name())
button->setChecked(true);
else
element()->setChecked(false);
} else if (state.checked != element()->checked())
fireInputAndChangeEvents();
event.setDefaultHandled();
}
bool RadioInputType::matchesIndeterminatePseudoClass() const
{
ASSERT(element());
auto& element = *this->element();
if (auto* radioButtonGroups = element.radioButtonGroups())
return !radioButtonGroups->hasCheckedButton(element);
return !element.checked();
}
}