Home | History | Annotate | Download | only in forms
      1 /*
      2  * Copyright (C) 2005, 2011 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 "core/html/forms/RadioInputType.h"
     24 
     25 #include "core/HTMLNames.h"
     26 #include "core/InputTypeNames.h"
     27 #include "core/dom/Document.h"
     28 #include "core/dom/ElementTraversal.h"
     29 #include "core/events/KeyboardEvent.h"
     30 #include "core/events/MouseEvent.h"
     31 #include "core/html/HTMLInputElement.h"
     32 #include "core/page/SpatialNavigation.h"
     33 #include "platform/text/PlatformLocale.h"
     34 #include "wtf/PassOwnPtr.h"
     35 
     36 namespace blink {
     37 
     38 namespace {
     39 
     40 HTMLElement* nextElement(const HTMLElement& element, bool forward)
     41 {
     42     return forward ? Traversal<HTMLElement>::next(element) : Traversal<HTMLElement>::previous(element);
     43 }
     44 
     45 } // namespace
     46 
     47 using namespace HTMLNames;
     48 
     49 PassRefPtrWillBeRawPtr<InputType> RadioInputType::create(HTMLInputElement& element)
     50 {
     51     return adoptRefWillBeNoop(new RadioInputType(element));
     52 }
     53 
     54 const AtomicString& RadioInputType::formControlType() const
     55 {
     56     return InputTypeNames::radio;
     57 }
     58 
     59 bool RadioInputType::valueMissing(const String&) const
     60 {
     61     return element().isInRequiredRadioButtonGroup() && !element().checkedRadioButtonForGroup();
     62 }
     63 
     64 String RadioInputType::valueMissingText() const
     65 {
     66     return locale().queryString(blink::WebLocalizedString::ValidationValueMissingForRadio);
     67 }
     68 
     69 void RadioInputType::handleClickEvent(MouseEvent* event)
     70 {
     71     event->setDefaultHandled();
     72 }
     73 
     74 void RadioInputType::handleKeydownEvent(KeyboardEvent* event)
     75 {
     76     BaseCheckableInputType::handleKeydownEvent(event);
     77     if (event->defaultHandled())
     78         return;
     79     const String& key = event->keyIdentifier();
     80     if (key != "Up" && key != "Down" && key != "Left" && key != "Right")
     81         return;
     82 
     83     // Left and up mean "previous radio button".
     84     // Right and down mean "next radio button".
     85     // Tested in WinIE, and even for RTL, left still means previous radio button
     86     // (and so moves to the right). Seems strange, but we'll match it. However,
     87     // when using Spatial Navigation, we need to be able to navigate without
     88     // changing the selection.
     89     Document& document = element().document();
     90     if (isSpatialNavigationEnabled(document.frame()))
     91         return;
     92     bool forward = (key == "Down" || key == "Right");
     93 
     94     // We can only stay within the form's children if the form hasn't been demoted to a leaf because
     95     // of malformed HTML.
     96     for (HTMLElement* htmlElement = nextElement(element(), forward); htmlElement; htmlElement = nextElement(*htmlElement, forward)) {
     97         // Once we encounter a form element, we know we're through.
     98         if (isHTMLFormElement(*htmlElement))
     99             break;
    100         // Look for more radio buttons.
    101         if (!isHTMLInputElement(*htmlElement))
    102             continue;
    103         HTMLInputElement* inputElement = toHTMLInputElement(htmlElement);
    104         if (inputElement->form() != element().form())
    105             break;
    106         if (inputElement->type() == InputTypeNames::radio && inputElement->name() == element().name() && inputElement->isFocusable()) {
    107             RefPtrWillBeRawPtr<HTMLInputElement> protector(inputElement);
    108             document.setFocusedElement(inputElement);
    109             inputElement->dispatchSimulatedClick(event, SendNoEvents);
    110             event->setDefaultHandled();
    111             return;
    112         }
    113     }
    114 }
    115 
    116 void RadioInputType::handleKeyupEvent(KeyboardEvent* event)
    117 {
    118     const String& key = event->keyIdentifier();
    119     if (key != "U+0020")
    120         return;
    121     // If an unselected radio is tabbed into (because the entire group has nothing
    122     // checked, or because of some explicit .focus() call), then allow space to check it.
    123     if (element().checked())
    124         return;
    125     dispatchSimulatedClickIfActive(event);
    126 }
    127 
    128 bool RadioInputType::isKeyboardFocusable() const
    129 {
    130     if (!InputType::isKeyboardFocusable())
    131         return false;
    132 
    133     // When using Spatial Navigation, every radio button should be focusable.
    134     if (isSpatialNavigationEnabled(element().document().frame()))
    135         return true;
    136 
    137     // Never allow keyboard tabbing to leave you in the same radio group. Always
    138     // skip any other elements in the group.
    139     Element* currentFocusedElement = element().document().focusedElement();
    140     if (isHTMLInputElement(currentFocusedElement)) {
    141         HTMLInputElement& focusedInput = toHTMLInputElement(*currentFocusedElement);
    142         if (focusedInput.type() == InputTypeNames::radio && focusedInput.form() == element().form() && focusedInput.name() == element().name())
    143             return false;
    144     }
    145 
    146     // Allow keyboard focus if we're checked or if nothing in the group is checked.
    147     return element().checked() || !element().checkedRadioButtonForGroup();
    148 }
    149 
    150 bool RadioInputType::shouldSendChangeEventAfterCheckedChanged()
    151 {
    152     // Don't send a change event for a radio button that's getting unchecked.
    153     // This was done to match the behavior of other browsers.
    154     return element().checked();
    155 }
    156 
    157 PassOwnPtrWillBeRawPtr<ClickHandlingState> RadioInputType::willDispatchClick()
    158 {
    159     // An event handler can use preventDefault or "return false" to reverse the selection we do here.
    160     // The ClickHandlingState object contains what we need to undo what we did here in didDispatchClick.
    161 
    162     // We want radio groups to end up in sane states, i.e., to have something checked.
    163     // Therefore if nothing is currently selected, we won't allow the upcoming action to be "undone", since
    164     // we want some object in the radio group to actually get selected.
    165 
    166     OwnPtrWillBeRawPtr<ClickHandlingState> state = adoptPtrWillBeNoop(new ClickHandlingState);
    167 
    168     state->checked = element().checked();
    169     state->checkedRadioButton = element().checkedRadioButtonForGroup();
    170     element().setChecked(true, DispatchChangeEvent);
    171 
    172     return state.release();
    173 }
    174 
    175 void RadioInputType::didDispatchClick(Event* event, const ClickHandlingState& state)
    176 {
    177     if (event->defaultPrevented() || event->defaultHandled()) {
    178         // Restore the original selected radio button if possible.
    179         // Make sure it is still a radio button and only do the restoration if it still belongs to our group.
    180         HTMLInputElement* checkedRadioButton = state.checkedRadioButton.get();
    181         if (!checkedRadioButton)
    182             element().setChecked(false);
    183         else if (checkedRadioButton->type() == InputTypeNames::radio
    184             && checkedRadioButton->form() == element().form()
    185             && checkedRadioButton->name() == element().name())
    186             checkedRadioButton->setChecked(true);
    187     }
    188 
    189     // The work we did in willDispatchClick was default handling.
    190     event->setDefaultHandled();
    191 }
    192 
    193 bool RadioInputType::shouldAppearIndeterminate() const
    194 {
    195     return !element().checkedRadioButtonForGroup();
    196 }
    197 
    198 } // namespace blink
    199