| 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/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
| 10 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" | 10 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 if (requirements & REQUIRE_ELEMENTS_ENABLED && | 195 if (requirements & REQUIRE_ELEMENTS_ENABLED && |
| 196 !input_element.isEnabledFormControl()) | 196 !input_element.isEnabledFormControl()) |
| 197 continue; | 197 continue; |
| 198 | 198 |
| 199 string16 label = LabelForElement(input_element); | 199 string16 label = LabelForElement(input_element); |
| 200 string16 name = input_element.nameForAutofill(); | 200 string16 name = input_element.nameForAutofill(); |
| 201 string16 value = input_element.value(); | 201 string16 value = input_element.value(); |
| 202 string16 form_control_type = input_element.formControlType(); | 202 string16 form_control_type = input_element.formControlType(); |
| 203 WebInputElement::InputType input_type = input_element.inputType(); | 203 WebInputElement::InputType input_type = input_element.inputType(); |
| 204 FormField field = FormField(label, | 204 FormField field = FormField(label, |
| 205 name, | 205 name, |
| 206 value, | 206 value, |
| 207 form_control_type, | 207 form_control_type, |
| 208 input_type); | 208 input_type); |
| 209 form->fields.push_back(field); | 209 form->fields.push_back(field); |
| 210 } | 210 } |
| 211 } | 211 } |
| 212 | 212 |
| 213 // static | 213 // static |
| 214 string16 FormManager::LabelForElement(const WebInputElement& element) { | 214 string16 FormManager::LabelForElement(const WebInputElement& element) { |
| 215 WebNodeList labels = element.document().getElementsByTagName("label"); | 215 WebNodeList labels = element.document().getElementsByTagName("label"); |
| 216 for (unsigned i = 0; i < labels.length(); ++i) { | 216 for (unsigned i = 0; i < labels.length(); ++i) { |
| 217 WebElement e = labels.item(i).toElement<WebElement>(); | 217 WebElement e = labels.item(i).toElement<WebElement>(); |
| 218 if (e.hasTagName("label")) { | 218 if (e.hasTagName("label")) { |
| 219 WebLabelElement label = e.toElement<WebLabelElement>(); | 219 WebLabelElement label = e.toElement<WebLabelElement>(); |
| 220 if (label.correspondingControl() == element) | 220 if (label.correspondingControl() == element) |
| 221 return label.innerText(); | 221 return label.innerText(); |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 return string16(); | 224 |
| 225 // Infer the label from context if not found in label element. |
| 226 return FormManager::InferLabelForElement(element); |
| 225 } | 227 } |
| 228 |
| 229 // static |
| 230 string16 FormManager::InferLabelForElement(const WebInputElement& element) { |
| 231 string16 inferred_label; |
| 232 WebNode previous = element.previousSibling(); |
| 233 if (!previous.isNull()) { |
| 234 if (previous.isTextNode()) { |
| 235 inferred_label = previous.nodeValue(); |
| 236 TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label); |
| 237 } |
| 238 |
| 239 // If we didn't find text, check for previous paragraph. |
| 240 // Eg. <p>Some Text</p><input ...> |
| 241 // Note the lack of whitespace between <p> and <input> elements. |
| 242 if (inferred_label.empty()) { |
| 243 if (previous.isElementNode()) { |
| 244 WebElement element = previous.toElement<WebElement>(); |
| 245 if (element.hasTagName("p")) { |
| 246 inferred_label = element.innerText(); |
| 247 TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label); |
| 248 } |
| 249 } |
| 250 } |
| 251 |
| 252 // If we didn't find paragraph, check for previous paragraph to this. |
| 253 // Eg. <p>Some Text</p> <input ...> |
| 254 // Note the whitespace between <p> and <input> elements. |
| 255 if (inferred_label.empty()) { |
| 256 previous = previous.previousSibling(); |
| 257 if (!previous.isNull() && previous.isElementNode()) { |
| 258 WebElement element = previous.toElement<WebElement>(); |
| 259 if (element.hasTagName("p")) { |
| 260 inferred_label = element.innerText(); |
| 261 TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label); |
| 262 } |
| 263 } |
| 264 } |
| 265 } |
| 266 |
| 267 return inferred_label; |
| 268 } |
| OLD | NEW |