summaryrefslogtreecommitdiffstats
path: root/WebCore/html/RadioInputType.cpp
blob: 4c459bea054d852e7d2b6ef62f251433032c832b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (C) 2005 Apple Inc. All rights reserved.
 * Copyright (C) 2010 Google Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "RadioInputType.h"

#include "Frame.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "KeyboardEvent.h"
#include "LocalizedStrings.h"
#include "MouseEvent.h"
#include "Settings.h"
#include "SpatialNavigation.h"
#include <wtf/PassOwnPtr.h>

namespace WebCore {

using namespace HTMLNames;

PassOwnPtr<InputType> RadioInputType::create(HTMLInputElement* element)
{
    return adoptPtr(new RadioInputType(element));
}

const AtomicString& RadioInputType::formControlType() const
{
    return InputTypeNames::radio();
}

bool RadioInputType::valueMissing(const String&) const
{
    return !element()->checkedRadioButtons().checkedButtonForGroup(element()->name());
}

String RadioInputType::valueMissingText() const
{
    return validationMessageValueMissingForRadioText();
}

bool RadioInputType::handleClickEvent(MouseEvent* event)
{
    event->setDefaultHandled();
    return true;
}

bool RadioInputType::handleKeydownEvent(KeyboardEvent* event)
{
    if (BaseCheckableInputType::handleKeydownEvent(event))
        return true;
    const String& key = event->keyIdentifier();
    if (key != "Up" && key != "Down" && key != "Left" && key != "Right")
        return false;

    // Left and up mean "previous radio button".
    // Right and down mean "next radio button".
    // Tested in WinIE, and even for RTL, left still means previous radio button (and so moves
    // to the right).  Seems strange, but we'll match it.
    // However, when using Spatial Navigation, we need to be able to navigate without changing the selection.
    Document* document = element()->document();
    if (isSpatialNavigationEnabled(document->frame()))
        return false;
    bool forward = (key == "Down" || key == "Right");

    // We can only stay within the form's children if the form hasn't been demoted to a leaf because
    // of malformed HTML.
    Node* node = element();
    while ((node = (forward ? node->traverseNextNode() : node->traversePreviousNode()))) {
        // Once we encounter a form element, we know we're through.
        if (node->hasTagName(formTag))
            break;
        // Look for more radio buttons.
        if (!node->hasTagName(inputTag))
            continue;
        HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(node);
        if (inputElement->form() != element()->form())
            break;
        if (inputElement->isRadioButton() && inputElement->name() == element()->name() && inputElement->isFocusable()) {
            inputElement->setChecked(true);
            document->setFocusedNode(inputElement);
            inputElement->dispatchSimulatedClick(event, false, false);
            event->setDefaultHandled();
            return true;
        }
    }
    return false;
}

bool RadioInputType::handleKeyupEvent(KeyboardEvent* event)
{
    const String& key = event->keyIdentifier();
    if (key != "U+0020")
        return false;
    // If an unselected radio is tabbed into (because the entire group has nothing
    // checked, or because of some explicit .focus() call), then allow space to check it.
    if (element()->checked())
        return false;
    dispatchSimulatedClickIfActive(event);
    return true;
}

} // namespace WebCore