1/*
2 * Copyright (C) 2005-2018 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "config.h"
23#include "RadioInputType.h"
24
25#include "Frame.h"
26#include "HTMLFormElement.h"
27#include "HTMLInputElement.h"
28#include "HTMLNames.h"
29#include "InputTypeNames.h"
30#include "KeyboardEvent.h"
31#include "LocalizedStrings.h"
32#include "MouseEvent.h"
33#include "NodeTraversal.h"
34#include "SpatialNavigation.h"
35
36namespace WebCore {
37
38using namespace HTMLNames;
39
40const AtomicString& RadioInputType::formControlType() const
41{
42 return InputTypeNames::radio();
43}
44
45bool RadioInputType::valueMissing(const String&) const
46{
47 ASSERT(element());
48 return element()->isInRequiredRadioButtonGroup() && !element()->checkedRadioButtonForGroup();
49}
50
51String RadioInputType::valueMissingText() const
52{
53 return validationMessageValueMissingForRadioText();
54}
55
56void RadioInputType::handleClickEvent(MouseEvent& event)
57{
58 event.setDefaultHandled();
59}
60
61auto RadioInputType::handleKeydownEvent(KeyboardEvent& event) -> ShouldCallBaseEventHandler
62{
63 if (BaseCheckableInputType::handleKeydownEvent(event) == ShouldCallBaseEventHandler::No)
64 return ShouldCallBaseEventHandler::No;
65 if (event.defaultHandled())
66 return ShouldCallBaseEventHandler::Yes;
67 const String& key = event.keyIdentifier();
68 if (key != "Up" && key != "Down" && key != "Left" && key != "Right")
69 return ShouldCallBaseEventHandler::Yes;
70
71 ASSERT(element());
72 // Left and up mean "previous radio button".
73 // Right and down mean "next radio button".
74 // Tested in WinIE, and even for RTL, left still means previous radio button (and so moves
75 // to the right). Seems strange, but we'll match it.
76 // However, when using Spatial Navigation, we need to be able to navigate without changing the selection.
77 if (isSpatialNavigationEnabled(element()->document().frame()))
78 return ShouldCallBaseEventHandler::Yes;
79 bool forward = (key == "Down" || key == "Right");
80
81 // We can only stay within the form's children if the form hasn't been demoted to a leaf because
82 // of malformed HTML.
83 RefPtr<Node> node = element();
84 while ((node = (forward ? NodeTraversal::next(*node) : NodeTraversal::previous(*node)))) {
85 // Once we encounter a form element, we know we're through.
86 if (is<HTMLFormElement>(*node))
87 break;
88 // Look for more radio buttons.
89 if (!is<HTMLInputElement>(*node))
90 continue;
91 RefPtr<HTMLInputElement> inputElement = downcast<HTMLInputElement>(node.get());
92 if (inputElement->form() != element()->form())
93 break;
94 if (inputElement->isRadioButton() && inputElement->name() == element()->name() && inputElement->isFocusable()) {
95 element()->document().setFocusedElement(inputElement.get());
96 inputElement->dispatchSimulatedClick(&event, SendNoEvents, DoNotShowPressedLook);
97 event.setDefaultHandled();
98 return ShouldCallBaseEventHandler::Yes;
99 }
100 }
101 return ShouldCallBaseEventHandler::Yes;
102}
103
104void RadioInputType::handleKeyupEvent(KeyboardEvent& event)
105{
106 const String& key = event.keyIdentifier();
107 if (key != "U+0020")
108 return;
109
110 ASSERT(element());
111 // If an unselected radio is tabbed into (because the entire group has nothing
112 // checked, or because of some explicit .focus() call), then allow space to check it.
113 if (element()->checked())
114 return;
115 dispatchSimulatedClickIfActive(event);
116}
117
118bool RadioInputType::isKeyboardFocusable(KeyboardEvent* event) const
119{
120 if (!InputType::isKeyboardFocusable(event))
121 return false;
122
123 ASSERT(element());
124 // When using Spatial Navigation, every radio button should be focusable.
125 if (isSpatialNavigationEnabled(element()->document().frame()))
126 return true;
127
128 // Never allow keyboard tabbing to leave you in the same radio group. Always
129 // skip any other elements in the group.
130 RefPtr<Element> currentFocusedNode = element()->document().focusedElement();
131 if (is<HTMLInputElement>(currentFocusedNode)) {
132 HTMLInputElement& focusedInput = downcast<HTMLInputElement>(*currentFocusedNode);
133 if (focusedInput.isRadioButton() && focusedInput.form() == element()->form() && focusedInput.name() == element()->name())
134 return false;
135 }
136
137 // Allow keyboard focus if we're checked or if nothing in the group is checked.
138 return element()->checked() || !element()->checkedRadioButtonForGroup();
139}
140
141bool RadioInputType::shouldSendChangeEventAfterCheckedChanged()
142{
143 // Don't send a change event for a radio button that's getting unchecked.
144 // This was done to match the behavior of other browsers.
145 ASSERT(element());
146 return element()->checked();
147}
148
149void RadioInputType::willDispatchClick(InputElementClickState& state)
150{
151 ASSERT(element());
152 // An event handler can use preventDefault or "return false" to reverse the selection we do here.
153 // The InputElementClickState object contains what we need to undo what we did here in didDispatchClick.
154
155 // We want radio groups to end up in sane states, i.e., to have something checked.
156 // Therefore if nothing is currently selected, we won't allow the upcoming action to be "undone", since
157 // we want some object in the radio group to actually get selected.
158
159 state.checked = element()->checked();
160 state.checkedRadioButton = element()->checkedRadioButtonForGroup();
161
162 element()->setChecked(true);
163}
164
165void RadioInputType::didDispatchClick(Event& event, const InputElementClickState& state)
166{
167 if (event.defaultPrevented() || event.defaultHandled()) {
168 // Restore the original selected radio button if possible.
169 // Make sure it is still a radio button and only do the restoration if it still belongs to our group.
170 auto& button = state.checkedRadioButton;
171 ASSERT(element());
172 if (button && button->isRadioButton() && button->form() == element()->form() && button->name() == element()->name())
173 button->setChecked(true);
174 } else if (state.checked != element()->checked())
175 fireInputAndChangeEvents();
176
177 // The work we did in willDispatchClick was default handling.
178 event.setDefaultHandled();
179}
180
181bool RadioInputType::isRadioButton() const
182{
183 return true;
184}
185
186bool RadioInputType::matchesIndeterminatePseudoClass() const
187{
188 ASSERT(element());
189 auto& element = *this->element();
190 if (auto* radioButtonGroups = element.radioButtonGroups())
191 return !radioButtonGroups->hasCheckedButton(element);
192 return !element.checked();
193}
194
195} // namespace WebCore
196