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 "Settings.h"
#include "SpatialNavigation.h"
namespace WebCore {
using namespace HTMLNames;
const AtomicString& RadioInputType::formControlType() const
{
return InputTypeNames::radio();
}
bool RadioInputType::valueMissing(const String&) const
{
return element().isInRequiredRadioButtonGroup() && !element().checkedRadioButtonForGroup();
}
String RadioInputType::valueMissingText() const
{
return validationMessageValueMissingForRadioText();
}
void RadioInputType::handleClickEvent(MouseEvent* event)
{
event->setDefaultHandled();
}
void RadioInputType::handleKeydownEvent(KeyboardEvent* event)
{
BaseCheckableInputType::handleKeydownEvent(event);
if (event->defaultHandled())
return;
const String& key = event->keyIdentifier();
if (key != "Up" && key != "Down" && key != "Left" && key != "Right")
return;
if (isSpatialNavigationEnabled(element().document().frame()))
return;
bool forward = (key == "Down" || key == "Right");
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);
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;
}
}
}
void RadioInputType::handleKeyupEvent(KeyboardEvent* event)
{
const String& key = event->keyIdentifier();
if (key != "U+0020")
return;
if (element().checked())
return;
dispatchSimulatedClickIfActive(event);
}
bool RadioInputType::isKeyboardFocusable(KeyboardEvent* event) const
{
if (!InputType::isKeyboardFocusable(event))
return false;
if (isSpatialNavigationEnabled(element().document().frame()))
return true;
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()
{
return element().checked();
}
void RadioInputType::willDispatchClick(InputElementClickState& state)
{
state.checked = element().checked();
state.checkedRadioButton = element().checkedRadioButtonForGroup();
element().setChecked(true, DispatchChangeEvent);
}
void RadioInputType::didDispatchClick(Event* event, const InputElementClickState& state)
{
if (event->defaultPrevented() || event->defaultHandled()) {
HTMLInputElement* checkedRadioButton = state.checkedRadioButton.get();
if (checkedRadioButton
&& checkedRadioButton->isRadioButton()
&& checkedRadioButton->form() == element().form()
&& checkedRadioButton->name() == element().name()) {
checkedRadioButton->setChecked(true);
}
}
event->setDefaultHandled();
}
bool RadioInputType::isRadioButton() const
{
return true;
}
bool RadioInputType::matchesIndeterminatePseudoClass() const
{
const HTMLInputElement& element = this->element();
if (const RadioButtonGroups* radioButtonGroups = element.radioButtonGroups())
return !radioButtonGroups->hasCheckedButton(&element);
return !element.checked();
}
}