Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1544)

Side by Side Diff: webkit/glue/form_field.cc

Issue 1394003: AutoFill: Copy FormManager::LabelForElement and the corresponding (Closed)
Patch Set: Add origin warning. Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "webkit/glue/form_field.h" 5 #include "webkit/glue/form_field.h"
6 6
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
10 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h"
11 #include "third_party/WebKit/WebKit/chromium/public/WebLabelElement.h"
12 #include "third_party/WebKit/WebKit/chromium/public/WebNode.h"
13 #include "third_party/WebKit/WebKit/chromium/public/WebNodeList.h"
9 14
15 using WebKit::WebElement;
16 using WebKit::WebLabelElement;
10 using WebKit::WebInputElement; 17 using WebKit::WebInputElement;
18 using WebKit::WebNode;
19 using WebKit::WebNodeList;
20
21 // TODO(jhawkins): Remove the following methods once AutoFill has been switched
22 // over to using FormData.
23 // WARNING: This code must stay in sync with the corresponding code in
24 // FormManager until we can remove this.
25 namespace {
26
27 string16 InferLabelForElement(const WebInputElement& element) {
28 string16 inferred_label;
29 WebNode previous = element.previousSibling();
30 if (!previous.isNull()) {
31 if (previous.isTextNode()) {
32 inferred_label = previous.nodeValue();
33 TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label);
34 }
35
36 // If we didn't find text, check for previous paragraph.
37 // Eg. <p>Some Text</p><input ...>
38 // Note the lack of whitespace between <p> and <input> elements.
39 if (inferred_label.empty()) {
40 if (previous.isElementNode()) {
41 WebElement element = previous.toElement<WebElement>();
42 if (element.hasTagName("p")) {
43 inferred_label = element.innerText();
44 TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label);
45 }
46 }
47 }
48
49 // If we didn't find paragraph, check for previous paragraph to this.
50 // Eg. <p>Some Text</p> <input ...>
51 // Note the whitespace between <p> and <input> elements.
52 if (inferred_label.empty()) {
53 previous = previous.previousSibling();
54 if (!previous.isNull() && previous.isElementNode()) {
55 WebElement element = previous.toElement<WebElement>();
56 if (element.hasTagName("p")) {
57 inferred_label = element.innerText();
58 TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label);
59 }
60 }
61 }
62 }
63
64 return inferred_label;
65 }
66
67 string16 LabelForElement(const WebInputElement& element) {
68 WebNodeList labels = element.document().getElementsByTagName("label");
69 for (unsigned i = 0; i < labels.length(); ++i) {
70 WebElement e = labels.item(i).toElement<WebElement>();
71 if (e.hasTagName("label")) {
72 WebLabelElement label = e.toElement<WebLabelElement>();
73 if (label.correspondingControl() == element)
74 return label.innerText();
75 }
76 }
77
78 // Infer the label from context if not found in label element.
79 return InferLabelForElement(element);
80 }
81
82 } // namespace
11 83
12 namespace webkit_glue { 84 namespace webkit_glue {
13 85
14 FormField::FormField() { 86 FormField::FormField() {
15 } 87 }
16 88
17 FormField::FormField(const WebInputElement& input_element) { 89 FormField::FormField(const WebInputElement& input_element) {
18 name_ = input_element.nameForAutofill(); 90 name_ = input_element.nameForAutofill();
19 91 label_ = LabelForElement(input_element);
20 // TODO(jhawkins): Extract the field label. For now we just use the field
21 // name.
22 label_ = name_;
23 92
24 value_ = input_element.value(); 93 value_ = input_element.value();
25 TrimWhitespace(value_, TRIM_LEADING, &value_); 94 TrimWhitespace(value_, TRIM_LEADING, &value_);
26 95
27 form_control_type_ = input_element.formControlType(); 96 form_control_type_ = input_element.formControlType();
28 input_type_ = input_element.inputType(); 97 input_type_ = input_element.inputType();
29 } 98 }
30 99
31 FormField::FormField(const string16& label, 100 FormField::FormField(const string16& label,
32 const string16& name, 101 const string16& name,
(...skipping 27 matching lines...) Expand all
60 << UTF16ToUTF8(field.name()) 129 << UTF16ToUTF8(field.name())
61 << " " 130 << " "
62 << UTF16ToUTF8(field.value()) 131 << UTF16ToUTF8(field.value())
63 << " " 132 << " "
64 << UTF16ToUTF8(field.form_control_type()) 133 << UTF16ToUTF8(field.form_control_type())
65 << " " 134 << " "
66 << field.input_type(); 135 << field.input_type();
67 } 136 }
68 137
69 } // namespace webkit_glue 138 } // namespace webkit_glue
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698