diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-26 03:47:22 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-26 03:47:22 +0000 |
commit | b0f91150cf04f462ef39f5f087490007bb320410 (patch) | |
tree | 05e84e993abce255691b30f7710c9783470b5708 /webkit/glue/form_field.cc | |
parent | 74b6bf33c972f6e676a4f79a77c0b02b5599ff5f (diff) | |
download | chromium_src-b0f91150cf04f462ef39f5f087490007bb320410.zip chromium_src-b0f91150cf04f462ef39f5f087490007bb320410.tar.gz chromium_src-b0f91150cf04f462ef39f5f087490007bb320410.tar.bz2 |
AutoFill: Copy FormManager::LabelForElement and the corresponding
FormManager::InferLabelForElement to form_field.cc until the
FormData/FormFieldValues consolidation is finished.
BUG=33031
TEST=none
Review URL: http://codereview.chromium.org/1394003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42730 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/glue/form_field.cc')
-rw-r--r-- | webkit/glue/form_field.cc | 77 |
1 files changed, 73 insertions, 4 deletions
diff --git a/webkit/glue/form_field.cc b/webkit/glue/form_field.cc index 721b3a7..7717dea 100644 --- a/webkit/glue/form_field.cc +++ b/webkit/glue/form_field.cc @@ -6,8 +6,80 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" +#include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" +#include "third_party/WebKit/WebKit/chromium/public/WebElement.h" +#include "third_party/WebKit/WebKit/chromium/public/WebLabelElement.h" +#include "third_party/WebKit/WebKit/chromium/public/WebNode.h" +#include "third_party/WebKit/WebKit/chromium/public/WebNodeList.h" +using WebKit::WebElement; +using WebKit::WebLabelElement; using WebKit::WebInputElement; +using WebKit::WebNode; +using WebKit::WebNodeList; + +// TODO(jhawkins): Remove the following methods once AutoFill has been switched +// over to using FormData. +// WARNING: This code must stay in sync with the corresponding code in +// FormManager until we can remove this. +namespace { + +string16 InferLabelForElement(const WebInputElement& element) { + string16 inferred_label; + WebNode previous = element.previousSibling(); + if (!previous.isNull()) { + if (previous.isTextNode()) { + inferred_label = previous.nodeValue(); + TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label); + } + + // If we didn't find text, check for previous paragraph. + // Eg. <p>Some Text</p><input ...> + // Note the lack of whitespace between <p> and <input> elements. + if (inferred_label.empty()) { + if (previous.isElementNode()) { + WebElement element = previous.toElement<WebElement>(); + if (element.hasTagName("p")) { + inferred_label = element.innerText(); + TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label); + } + } + } + + // If we didn't find paragraph, check for previous paragraph to this. + // Eg. <p>Some Text</p> <input ...> + // Note the whitespace between <p> and <input> elements. + if (inferred_label.empty()) { + previous = previous.previousSibling(); + if (!previous.isNull() && previous.isElementNode()) { + WebElement element = previous.toElement<WebElement>(); + if (element.hasTagName("p")) { + inferred_label = element.innerText(); + TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label); + } + } + } + } + + return inferred_label; +} + +string16 LabelForElement(const WebInputElement& element) { + WebNodeList labels = element.document().getElementsByTagName("label"); + for (unsigned i = 0; i < labels.length(); ++i) { + WebElement e = labels.item(i).toElement<WebElement>(); + if (e.hasTagName("label")) { + WebLabelElement label = e.toElement<WebLabelElement>(); + if (label.correspondingControl() == element) + return label.innerText(); + } + } + + // Infer the label from context if not found in label element. + return InferLabelForElement(element); +} + +} // namespace namespace webkit_glue { @@ -16,10 +88,7 @@ FormField::FormField() { FormField::FormField(const WebInputElement& input_element) { name_ = input_element.nameForAutofill(); - - // TODO(jhawkins): Extract the field label. For now we just use the field - // name. - label_ = name_; + label_ = LabelForElement(input_element); value_ = input_element.value(); TrimWhitespace(value_, TRIM_LEADING, &value_); |