OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2005, 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2005, 2011 Apple Inc. All rights reserved. |
3 * Copyright (C) 2010 Google Inc. All rights reserved. | 3 * Copyright (C) 2010 Google Inc. All rights reserved. |
4 * | 4 * |
5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
9 * | 9 * |
10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 // (and so moves to the right). Seems strange, but we'll match it. However, | 103 // (and so moves to the right). Seems strange, but we'll match it. However, |
104 // when using Spatial Navigation, we need to be able to navigate without | 104 // when using Spatial Navigation, we need to be able to navigate without |
105 // changing the selection. | 105 // changing the selection. |
106 Document& document = element().document(); | 106 Document& document = element().document(); |
107 if (isSpatialNavigationEnabled(document.frame())) | 107 if (isSpatialNavigationEnabled(document.frame())) |
108 return; | 108 return; |
109 bool forward = computedTextDirection() == RTL | 109 bool forward = computedTextDirection() == RTL |
110 ? (key == "ArrowDown" || key == "ArrowLeft") | 110 ? (key == "ArrowDown" || key == "ArrowLeft") |
111 : (key == "ArrowDown" || key == "ArrowRight"); | 111 : (key == "ArrowDown" || key == "ArrowRight"); |
112 | 112 |
113 // We can only stay within the form's children if the form hasn't been demoted
to a leaf because | 113 // We can only stay within the form's children if the form hasn't been demoted |
114 // of malformed HTML. | 114 // to a leaf because of malformed HTML. |
115 HTMLInputElement* inputElement = findNextFocusableRadioButtonInGroup( | 115 HTMLInputElement* inputElement = findNextFocusableRadioButtonInGroup( |
116 toHTMLInputElement(&element()), forward); | 116 toHTMLInputElement(&element()), forward); |
117 if (!inputElement) { | 117 if (!inputElement) { |
118 // Traverse in reverse direction till last or first radio button | 118 // Traverse in reverse direction till last or first radio button |
119 forward = !(forward); | 119 forward = !(forward); |
120 HTMLInputElement* nextInputElement = findNextFocusableRadioButtonInGroup( | 120 HTMLInputElement* nextInputElement = findNextFocusableRadioButtonInGroup( |
121 toHTMLInputElement(&element()), forward); | 121 toHTMLInputElement(&element()), forward); |
122 while (nextInputElement) { | 122 while (nextInputElement) { |
123 inputElement = nextInputElement; | 123 inputElement = nextInputElement; |
124 nextInputElement = | 124 nextInputElement = |
125 findNextFocusableRadioButtonInGroup(nextInputElement, forward); | 125 findNextFocusableRadioButtonInGroup(nextInputElement, forward); |
126 } | 126 } |
127 } | 127 } |
128 if (inputElement) { | 128 if (inputElement) { |
129 document.setFocusedElement( | 129 document.setFocusedElement( |
130 inputElement, | 130 inputElement, |
131 FocusParams(SelectionBehaviorOnFocus::None, WebFocusTypeNone, nullptr)); | 131 FocusParams(SelectionBehaviorOnFocus::None, WebFocusTypeNone, nullptr)); |
132 inputElement->dispatchSimulatedClick(event, SendNoEvents); | 132 inputElement->dispatchSimulatedClick(event, SendNoEvents); |
133 event->setDefaultHandled(); | 133 event->setDefaultHandled(); |
134 return; | 134 return; |
135 } | 135 } |
136 } | 136 } |
137 | 137 |
138 void RadioInputType::handleKeyupEvent(KeyboardEvent* event) { | 138 void RadioInputType::handleKeyupEvent(KeyboardEvent* event) { |
139 const String& key = event->key(); | 139 const String& key = event->key(); |
140 if (key != " ") | 140 if (key != " ") |
141 return; | 141 return; |
142 // If an unselected radio is tabbed into (because the entire group has nothing | 142 // If an unselected radio is tabbed into (because the entire group has nothing |
143 // checked, or because of some explicit .focus() call), then allow space to ch
eck it. | 143 // checked, or because of some explicit .focus() call), then allow space to |
| 144 // check it. |
144 if (element().checked()) | 145 if (element().checked()) |
145 return; | 146 return; |
146 dispatchSimulatedClickIfActive(event); | 147 dispatchSimulatedClickIfActive(event); |
147 } | 148 } |
148 | 149 |
149 bool RadioInputType::isKeyboardFocusable() const { | 150 bool RadioInputType::isKeyboardFocusable() const { |
150 if (!InputType::isKeyboardFocusable()) | 151 if (!InputType::isKeyboardFocusable()) |
151 return false; | 152 return false; |
152 | 153 |
153 // When using Spatial Navigation, every radio button should be focusable. | 154 // When using Spatial Navigation, every radio button should be focusable. |
154 if (isSpatialNavigationEnabled(element().document().frame())) | 155 if (isSpatialNavigationEnabled(element().document().frame())) |
155 return true; | 156 return true; |
156 | 157 |
157 // Never allow keyboard tabbing to leave you in the same radio group. Always | 158 // Never allow keyboard tabbing to leave you in the same radio group. Always |
158 // skip any other elements in the group. | 159 // skip any other elements in the group. |
159 Element* currentFocusedElement = element().document().focusedElement(); | 160 Element* currentFocusedElement = element().document().focusedElement(); |
160 if (isHTMLInputElement(currentFocusedElement)) { | 161 if (isHTMLInputElement(currentFocusedElement)) { |
161 HTMLInputElement& focusedInput = toHTMLInputElement(*currentFocusedElement); | 162 HTMLInputElement& focusedInput = toHTMLInputElement(*currentFocusedElement); |
162 if (focusedInput.type() == InputTypeNames::radio && | 163 if (focusedInput.type() == InputTypeNames::radio && |
163 focusedInput.form() == element().form() && | 164 focusedInput.form() == element().form() && |
164 focusedInput.name() == element().name()) | 165 focusedInput.name() == element().name()) |
165 return false; | 166 return false; |
166 } | 167 } |
167 | 168 |
168 // Allow keyboard focus if we're checked or if nothing in the group is checked
. | 169 // Allow keyboard focus if we're checked or if nothing in the group is |
| 170 // checked. |
169 return element().checked() || !element().checkedRadioButtonForGroup(); | 171 return element().checked() || !element().checkedRadioButtonForGroup(); |
170 } | 172 } |
171 | 173 |
172 bool RadioInputType::shouldSendChangeEventAfterCheckedChanged() { | 174 bool RadioInputType::shouldSendChangeEventAfterCheckedChanged() { |
173 // Don't send a change event for a radio button that's getting unchecked. | 175 // Don't send a change event for a radio button that's getting unchecked. |
174 // This was done to match the behavior of other browsers. | 176 // This was done to match the behavior of other browsers. |
175 return element().checked(); | 177 return element().checked(); |
176 } | 178 } |
177 | 179 |
178 ClickHandlingState* RadioInputType::willDispatchClick() { | 180 ClickHandlingState* RadioInputType::willDispatchClick() { |
179 // An event handler can use preventDefault or "return false" to reverse the se
lection we do here. | 181 // An event handler can use preventDefault or "return false" to reverse the |
180 // The ClickHandlingState object contains what we need to undo what we did her
e in didDispatchClick. | 182 // selection we do here. The ClickHandlingState object contains what we need |
| 183 // to undo what we did here in didDispatchClick. |
181 | 184 |
182 // We want radio groups to end up in sane states, i.e., to have something chec
ked. | 185 // We want radio groups to end up in sane states, i.e., to have something |
183 // Therefore if nothing is currently selected, we won't allow the upcoming act
ion to be "undone", since | 186 // checked. Therefore if nothing is currently selected, we won't allow the |
184 // we want some object in the radio group to actually get selected. | 187 // upcoming action to be "undone", since we want some object in the radio |
| 188 // group to actually get selected. |
185 | 189 |
186 ClickHandlingState* state = new ClickHandlingState; | 190 ClickHandlingState* state = new ClickHandlingState; |
187 | 191 |
188 state->checked = element().checked(); | 192 state->checked = element().checked(); |
189 state->checkedRadioButton = element().checkedRadioButtonForGroup(); | 193 state->checkedRadioButton = element().checkedRadioButtonForGroup(); |
190 element().setChecked(true, DispatchChangeEvent); | 194 element().setChecked(true, DispatchChangeEvent); |
191 m_isInClickHandler = true; | 195 m_isInClickHandler = true; |
192 return state; | 196 return state; |
193 } | 197 } |
194 | 198 |
195 void RadioInputType::didDispatchClick(Event* event, | 199 void RadioInputType::didDispatchClick(Event* event, |
196 const ClickHandlingState& state) { | 200 const ClickHandlingState& state) { |
197 if (event->defaultPrevented() || event->defaultHandled()) { | 201 if (event->defaultPrevented() || event->defaultHandled()) { |
198 // Restore the original selected radio button if possible. | 202 // Restore the original selected radio button if possible. |
199 // Make sure it is still a radio button and only do the restoration if it st
ill belongs to our group. | 203 // Make sure it is still a radio button and only do the restoration if it |
| 204 // still belongs to our group. |
200 HTMLInputElement* checkedRadioButton = state.checkedRadioButton.get(); | 205 HTMLInputElement* checkedRadioButton = state.checkedRadioButton.get(); |
201 if (!checkedRadioButton) | 206 if (!checkedRadioButton) |
202 element().setChecked(false); | 207 element().setChecked(false); |
203 else if (checkedRadioButton->type() == InputTypeNames::radio && | 208 else if (checkedRadioButton->type() == InputTypeNames::radio && |
204 checkedRadioButton->form() == element().form() && | 209 checkedRadioButton->form() == element().form() && |
205 checkedRadioButton->name() == element().name()) | 210 checkedRadioButton->name() == element().name()) |
206 checkedRadioButton->setChecked(true); | 211 checkedRadioButton->setChecked(true); |
207 } else if (state.checked != element().checked()) { | 212 } else if (state.checked != element().checked()) { |
208 element().dispatchChangeEventIfNeeded(); | 213 element().dispatchChangeEventIfNeeded(); |
209 } | 214 } |
(...skipping 19 matching lines...) Expand all Loading... |
229 *inputElement, current->form(), forward)) { | 234 *inputElement, current->form(), forward)) { |
230 if (current->form() == inputElement->form() && | 235 if (current->form() == inputElement->form() && |
231 inputElement->type() == InputTypeNames::radio && | 236 inputElement->type() == InputTypeNames::radio && |
232 inputElement->name() == current->name()) | 237 inputElement->name() == current->name()) |
233 return inputElement; | 238 return inputElement; |
234 } | 239 } |
235 return nullptr; | 240 return nullptr; |
236 } | 241 } |
237 | 242 |
238 } // namespace blink | 243 } // namespace blink |
OLD | NEW |