OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/form_manager.h" | 5 #include "chrome/renderer/form_manager.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/scoped_vector.h" | 8 #include "base/scoped_vector.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
11 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" | 11 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" |
12 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h" | 12 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h" |
13 #include "third_party/WebKit/WebKit/chromium/public/WebFormControlElement.h" | 13 #include "third_party/WebKit/WebKit/chromium/public/WebFormControlElement.h" |
14 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | 14 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
15 #include "third_party/WebKit/WebKit/chromium/public/WebInputElement.h" | 15 #include "third_party/WebKit/WebKit/chromium/public/WebInputElement.h" |
16 #include "third_party/WebKit/WebKit/chromium/public/WebLabelElement.h" | 16 #include "third_party/WebKit/WebKit/chromium/public/WebLabelElement.h" |
17 #include "third_party/WebKit/WebKit/chromium/public/WebNode.h" | 17 #include "third_party/WebKit/WebKit/chromium/public/WebNode.h" |
18 #include "third_party/WebKit/WebKit/chromium/public/WebNodeList.h" | 18 #include "third_party/WebKit/WebKit/chromium/public/WebNodeList.h" |
| 19 #include "third_party/WebKit/WebKit/chromium/public/WebOptionElement.h" |
19 #include "third_party/WebKit/WebKit/chromium/public/WebSelectElement.h" | 20 #include "third_party/WebKit/WebKit/chromium/public/WebSelectElement.h" |
20 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" | 21 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" |
21 #include "third_party/WebKit/WebKit/chromium/public/WebVector.h" | 22 #include "third_party/WebKit/WebKit/chromium/public/WebVector.h" |
22 #include "webkit/glue/form_data.h" | 23 #include "webkit/glue/form_data.h" |
23 #include "webkit/glue/form_field.h" | 24 #include "webkit/glue/form_field.h" |
24 | 25 |
25 using webkit_glue::FormData; | 26 using webkit_glue::FormData; |
26 using webkit_glue::FormField; | 27 using webkit_glue::FormField; |
27 using WebKit::WebDocument; | 28 using WebKit::WebDocument; |
28 using WebKit::WebElement; | 29 using WebKit::WebElement; |
29 using WebKit::WebFormControlElement; | 30 using WebKit::WebFormControlElement; |
30 using WebKit::WebFormElement; | 31 using WebKit::WebFormElement; |
31 using WebKit::WebFrame; | 32 using WebKit::WebFrame; |
32 using WebKit::WebInputElement; | 33 using WebKit::WebInputElement; |
33 using WebKit::WebLabelElement; | 34 using WebKit::WebLabelElement; |
34 using WebKit::WebNode; | 35 using WebKit::WebNode; |
35 using WebKit::WebNodeList; | 36 using WebKit::WebNodeList; |
| 37 using WebKit::WebOptionElement; |
36 using WebKit::WebSelectElement; | 38 using WebKit::WebSelectElement; |
37 using WebKit::WebString; | 39 using WebKit::WebString; |
38 using WebKit::WebVector; | 40 using WebKit::WebVector; |
39 | 41 |
40 namespace { | 42 namespace { |
41 | 43 |
42 // The number of fields required by AutoFill. Ideally we could send the forms | 44 // The number of fields required by AutoFill. Ideally we could send the forms |
43 // to AutoFill no matter how many fields are in the forms; however, finding the | 45 // to AutoFill no matter how many fields are in the forms; however, finding the |
44 // label for each field is a costly operation and we can't spare the cycles if | 46 // label for each field is a costly operation and we can't spare the cycles if |
45 // it's not necessary. | 47 // it's not necessary. |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 if (element.hasTagName("dt")) { | 205 if (element.hasTagName("dt")) { |
204 inferred_label = FindChildText(element); | 206 inferred_label = FindChildText(element); |
205 } | 207 } |
206 } | 208 } |
207 } | 209 } |
208 } | 210 } |
209 | 211 |
210 return inferred_label; | 212 return inferred_label; |
211 } | 213 } |
212 | 214 |
| 215 void GetOptionStringsFromElement(WebKit::WebFormControlElement element, |
| 216 std::vector<string16>* option_strings) { |
| 217 DCHECK(!element.isNull()); |
| 218 DCHECK(option_strings); |
| 219 option_strings->clear(); |
| 220 if (element.formControlType() == ASCIIToUTF16("select-one")) { |
| 221 WebSelectElement select_element = element.to<WebSelectElement>(); |
| 222 |
| 223 // For select-one elements copy option strings. |
| 224 WebVector<WebElement> list_items = select_element.listItems(); |
| 225 option_strings->reserve(list_items.size()); |
| 226 for (size_t i = 0; i < list_items.size(); ++i) { |
| 227 if (list_items[i].hasTagName("option")) |
| 228 option_strings->push_back(list_items[i].to<WebOptionElement>().value()); |
| 229 } |
| 230 } |
| 231 } |
| 232 |
213 } // namespace | 233 } // namespace |
214 | 234 |
215 FormManager::FormManager() { | 235 FormManager::FormManager() { |
216 } | 236 } |
217 | 237 |
218 FormManager::~FormManager() { | 238 FormManager::~FormManager() { |
219 Reset(); | 239 Reset(); |
220 } | 240 } |
221 | 241 |
222 // static | 242 // static |
223 void FormManager::WebFormControlElementToFormField( | 243 void FormManager::WebFormControlElementToFormField( |
224 const WebFormControlElement& element, bool get_value, FormField* field) { | 244 const WebFormControlElement& element, bool get_value, FormField* field) { |
225 DCHECK(field); | 245 DCHECK(field); |
226 | 246 |
227 // The label is not officially part of a WebFormControlElement; however, the | 247 // The label is not officially part of a WebFormControlElement; however, the |
228 // labels for all form control elements are scraped from the DOM and set in | 248 // labels for all form control elements are scraped from the DOM and set in |
229 // WebFormElementToFormData. | 249 // WebFormElementToFormData. |
230 field->set_name(element.nameForAutofill()); | 250 field->set_name(element.nameForAutofill()); |
231 field->set_form_control_type(element.formControlType()); | 251 field->set_form_control_type(element.formControlType()); |
232 | 252 |
| 253 // Set option strings on the field if available. |
| 254 std::vector<string16> option_strings; |
| 255 GetOptionStringsFromElement(element, &option_strings); |
| 256 field->set_option_strings(option_strings); |
| 257 |
233 if (element.formControlType() == WebString::fromUTF8("text")) { | 258 if (element.formControlType() == WebString::fromUTF8("text")) { |
234 const WebInputElement& input_element = element.toConst<WebInputElement>(); | 259 const WebInputElement& input_element = element.toConst<WebInputElement>(); |
235 field->set_size(input_element.size()); | 260 field->set_size(input_element.size()); |
236 } | 261 } |
237 | 262 |
238 if (!get_value) | 263 if (!get_value) |
239 return; | 264 return; |
240 | 265 |
241 // TODO(jhawkins): In WebKit, move value() and setValue() to | 266 // TODO(jhawkins): In WebKit, move value() and setValue() to |
242 // WebFormControlElement. | 267 // WebFormControlElement. |
(...skipping 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
856 return; | 881 return; |
857 | 882 |
858 WebInputElement input_element = field->to<WebInputElement>(); | 883 WebInputElement input_element = field->to<WebInputElement>(); |
859 | 884 |
860 // If the maxlength attribute contains a negative value, maxLength() | 885 // If the maxlength attribute contains a negative value, maxLength() |
861 // returns the default maxlength value. | 886 // returns the default maxlength value. |
862 input_element.setSuggestedValue( | 887 input_element.setSuggestedValue( |
863 data->value().substr(0, input_element.maxLength())); | 888 data->value().substr(0, input_element.maxLength())); |
864 input_element.setAutofilled(true); | 889 input_element.setAutofilled(true); |
865 } | 890 } |
OLD | NEW |