2 * Copyright (C) 2005, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
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.
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.
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.
23 #include "RadioInputType.h"
26 #include "HTMLInputElement.h"
27 #include "HTMLNames.h"
28 #include "KeyboardEvent.h"
29 #include "LocalizedStrings.h"
30 #include "MouseEvent.h"
32 #include "SpatialNavigation.h"
33 #include <wtf/PassOwnPtr.h>
37 using namespace HTMLNames;
39 PassOwnPtr<InputType> RadioInputType::create(HTMLInputElement* element)
41 return adoptPtr(new RadioInputType(element));
44 const AtomicString& RadioInputType::formControlType() const
46 return InputTypeNames::radio();
49 bool RadioInputType::valueMissing(const String&) const
51 return !element()->checkedRadioButtons().checkedButtonForGroup(element()->name());
54 String RadioInputType::valueMissingText() const
56 return validationMessageValueMissingForRadioText();
59 void RadioInputType::handleClickEvent(MouseEvent* event)
61 event->setDefaultHandled();
64 void RadioInputType::handleKeydownEvent(KeyboardEvent* event)
66 BaseCheckableInputType::handleKeydownEvent(event);
67 if (event->defaultHandled())
69 const String& key = event->keyIdentifier();
70 if (key != "Up" && key != "Down" && key != "Left" && key != "Right")
73 // Left and up mean "previous radio button".
74 // Right and down mean "next radio button".
75 // Tested in WinIE, and even for RTL, left still means previous radio button (and so moves
76 // to the right). Seems strange, but we'll match it.
77 // However, when using Spatial Navigation, we need to be able to navigate without changing the selection.
78 Document* document = element()->document();
79 if (isSpatialNavigationEnabled(document->frame()))
81 bool forward = (key == "Down" || key == "Right");
83 // We can only stay within the form's children if the form hasn't been demoted to a leaf because
85 Node* node = element();
86 while ((node = (forward ? node->traverseNextNode() : node->traversePreviousNode()))) {
87 // Once we encounter a form element, we know we're through.
88 if (node->hasTagName(formTag))
90 // Look for more radio buttons.
91 if (!node->hasTagName(inputTag))
93 HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(node);
94 if (inputElement->form() != element()->form())
96 if (inputElement->isRadioButton() && inputElement->name() == element()->name() && inputElement->isFocusable()) {
97 document->setFocusedNode(inputElement);
98 inputElement->dispatchSimulatedClick(event, false, false);
99 event->setDefaultHandled();
105 void RadioInputType::handleKeyupEvent(KeyboardEvent* event)
107 const String& key = event->keyIdentifier();
110 // If an unselected radio is tabbed into (because the entire group has nothing
111 // checked, or because of some explicit .focus() call), then allow space to check it.
112 if (element()->checked())
114 dispatchSimulatedClickIfActive(event);
117 bool RadioInputType::isKeyboardFocusable() const
119 // When using Spatial Navigation, every radio button should be focusable.
120 if (isSpatialNavigationEnabled(element()->document()->frame()))
123 // Never allow keyboard tabbing to leave you in the same radio group. Always
124 // skip any other elements in the group.
125 Node* currentFocusedNode = element()->document()->focusedNode();
126 if (currentFocusedNode && currentFocusedNode->hasTagName(inputTag)) {
127 HTMLInputElement* focusedInput = static_cast<HTMLInputElement*>(currentFocusedNode);
128 if (focusedInput->isRadioButton() && focusedInput->form() == element()->form() && focusedInput->name() == element()->name())
132 // Allow keyboard focus if we're checked or if nothing in the group is checked.
133 return element()->checked() || !element()->checkedRadioButtons().checkedButtonForGroup(element()->name());
136 void RadioInputType::attach()
139 element()->updateCheckedRadioButtons();
142 bool RadioInputType::shouldSendChangeEventAfterCheckedChanged()
144 // Don't send a change event for a radio button that's getting unchecked.
145 // This was done to match the behavior of other browsers.
146 return element()->checked();
149 PassOwnPtr<ClickHandlingState> RadioInputType::willDispatchClick()
151 // An event handler can use preventDefault or "return false" to reverse the selection we do here.
152 // The ClickHandlingState object contains what we need to undo what we did here in didDispatchClick.
154 // We want radio groups to end up in sane states, i.e., to have something checked.
155 // Therefore if nothing is currently selected, we won't allow the upcoming action to be "undone", since
156 // we want some object in the radio group to actually get selected.
158 OwnPtr<ClickHandlingState> state = adoptPtr(new ClickHandlingState);
160 state->checked = element()->checked();
161 state->indeterminate = element()->indeterminate();
162 state->checkedRadioButton = element()->checkedRadioButtons().checkedButtonForGroup(element()->name());
164 if (element()->indeterminate())
165 element()->setIndeterminate(false);
166 element()->setChecked(true, true);
168 return state.release();
171 void RadioInputType::didDispatchClick(Event* event, const ClickHandlingState& state)
173 if (event->defaultPrevented() || event->defaultHandled()) {
174 // Restore the original selected radio button if possible.
175 // Make sure it is still a radio button and only do the restoration if it still belongs to our group.
176 HTMLInputElement* checkedRadioButton = state.checkedRadioButton.get();
177 if (checkedRadioButton
178 && checkedRadioButton->isRadioButton()
179 && checkedRadioButton->form() == element()->form()
180 && checkedRadioButton->name() == element()->name()) {
181 checkedRadioButton->setChecked(true);
183 element()->setIndeterminate(state.indeterminate);
186 // The work we did in willDispatchClick was default handling.
187 event->setDefaultHandled();
190 bool RadioInputType::isRadioButton() const
195 } // namespace WebCore