summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/common/render_messages.h2
-rw-r--r--chrome/renderer/form_manager.cc23
-rw-r--r--chrome/renderer/form_manager.h3
-rw-r--r--chrome/renderer/form_manager_unittest.cc95
-rw-r--r--webkit/glue/form_data.h17
5 files changed, 120 insertions, 20 deletions
diff --git a/chrome/common/render_messages.h b/chrome/common/render_messages.h
index 42b0313..8599ad0 100644
--- a/chrome/common/render_messages.h
+++ b/chrome/common/render_messages.h
@@ -1550,6 +1550,7 @@ struct ParamTraits<FormData> {
WriteParam(m, p.name);
WriteParam(m, p.origin);
WriteParam(m, p.action);
+ WriteParam(m, p.labels);
WriteParam(m, p.elements);
WriteParam(m, p.values);
WriteParam(m, p.submit);
@@ -1559,6 +1560,7 @@ struct ParamTraits<FormData> {
ReadParam(m, iter, &p->name) &&
ReadParam(m, iter, &p->origin) &&
ReadParam(m, iter, &p->action) &&
+ ReadParam(m, iter, &p->labels) &&
ReadParam(m, iter, &p->elements) &&
ReadParam(m, iter, &p->values) &&
ReadParam(m, iter, &p->submit);
diff --git a/chrome/renderer/form_manager.cc b/chrome/renderer/form_manager.cc
index 743d95c..9731e7a 100644
--- a/chrome/renderer/form_manager.cc
+++ b/chrome/renderer/form_manager.cc
@@ -7,15 +7,23 @@
#include "base/logging.h"
#include "base/string_util.h"
#include "base/stl_util-inl.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/WebFrame.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"
#include "third_party/WebKit/WebKit/chromium/public/WebString.h"
#include "third_party/WebKit/WebKit/chromium/public/WebVector.h"
+using WebKit::WebDocument;
+using WebKit::WebElement;
using WebKit::WebFormElement;
using WebKit::WebFrame;
using WebKit::WebInputElement;
+using WebKit::WebLabelElement;
using WebKit::WebNode;
+using WebKit::WebNodeList;
using WebKit::WebString;
using WebKit::WebVector;
@@ -175,6 +183,7 @@ void FormManager::FormElementToFormData(WebFrame* frame,
!input_element.isEnabledFormControl())
continue;
+ form->labels.push_back(LabelForElement(input_element));
form->elements.push_back(input_element.nameForAutofill());
form->values.push_back(input_element.value());
@@ -185,3 +194,17 @@ void FormManager::FormElementToFormData(WebFrame* frame,
form->submit = input_element.nameForAutofill();
}
}
+
+// static
+string16 FormManager::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();
+ }
+ }
+ return string16();
+}
diff --git a/chrome/renderer/form_manager.h b/chrome/renderer/form_manager.h
index 209c93c..9e7abf8 100644
--- a/chrome/renderer/form_manager.h
+++ b/chrome/renderer/form_manager.h
@@ -75,6 +75,9 @@ class FormManager {
RequirementsMask requirements,
FormData* form);
+ // Returns the corresponding label for |element|.
+ static string16 LabelForElement(const WebKit::WebInputElement& element);
+
// The map of form elements.
WebFrameFormElementMap form_elements_map_;
diff --git a/chrome/renderer/form_manager_unittest.cc b/chrome/renderer/form_manager_unittest.cc
index 4a5e52a..d819096 100644
--- a/chrome/renderer/form_manager_unittest.cc
+++ b/chrome/renderer/form_manager_unittest.cc
@@ -41,7 +41,12 @@ TEST_F(FormManagerTest, ExtractForms) {
EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
EXPECT_EQ(GURL(web_frame->url()), form.origin);
EXPECT_EQ(GURL("http://cnn.com"), form.action);
- EXPECT_EQ(ASCIIToUTF16("reply-send"), form.submit);
+
+ const std::vector<string16>& labels = form.labels;
+ ASSERT_EQ(3U, labels.size());
+ EXPECT_EQ(string16(), labels[0]);
+ EXPECT_EQ(string16(), labels[1]);
+ EXPECT_EQ(string16(), labels[2]);
const std::vector<string16>& elements = form.elements;
ASSERT_EQ(3U, elements.size());
@@ -81,7 +86,11 @@ TEST_F(FormManagerTest, ExtractMultipleForms) {
EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
EXPECT_EQ(GURL(web_frame->url()), form.origin);
EXPECT_EQ(GURL("http://cnn.com"), form.action);
- EXPECT_EQ(ASCIIToUTF16("reply-send"), form.submit);
+
+ const std::vector<string16>& labels = form.labels;
+ ASSERT_EQ(2U, labels.size());
+ EXPECT_EQ(string16(), labels[0]);
+ EXPECT_EQ(string16(), labels[1]);
const std::vector<string16>& elements = form.elements;
ASSERT_EQ(2U, elements.size());
@@ -98,7 +107,11 @@ TEST_F(FormManagerTest, ExtractMultipleForms) {
EXPECT_EQ(ASCIIToUTF16("TestForm2"), form2.name);
EXPECT_EQ(GURL(web_frame->url()), form2.origin);
EXPECT_EQ(GURL("http://zoo.com"), form2.action);
- EXPECT_EQ(ASCIIToUTF16("second"), form2.submit);
+
+ const std::vector<string16>& labels2 = form2.labels;
+ ASSERT_EQ(2U, labels2.size());
+ EXPECT_EQ(string16(), labels2[0]);
+ EXPECT_EQ(string16(), labels2[1]);
const std::vector<string16>& elements2 = form2.elements;
ASSERT_EQ(2U, elements2.size());
@@ -157,7 +170,11 @@ TEST_F(FormManagerTest, GetFormsAutocomplete) {
EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
EXPECT_EQ(GURL(web_frame->url()), form.origin);
EXPECT_EQ(GURL("http://abc.com"), form.action);
- EXPECT_EQ(ASCIIToUTF16("reply"), form.submit);
+
+ const std::vector<string16>& labels = form.labels;
+ ASSERT_EQ(2U, labels.size());
+ EXPECT_EQ(string16(), labels[0]);
+ EXPECT_EQ(string16(), labels[1]);
const std::vector<string16>& elements = form.elements;
ASSERT_EQ(2U, elements.size());
@@ -192,7 +209,11 @@ TEST_F(FormManagerTest, GetFormsElementsEnabled) {
EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
EXPECT_EQ(GURL(web_frame->url()), form.origin);
EXPECT_EQ(GURL("http://xyz.com"), form.action);
- EXPECT_EQ(ASCIIToUTF16("submit"), form.submit);
+
+ const std::vector<string16>& labels = form.labels;
+ ASSERT_EQ(2U, labels.size());
+ EXPECT_EQ(string16(), labels[0]);
+ EXPECT_EQ(string16(), labels[1]);
const std::vector<string16>& elements = form.elements;
ASSERT_EQ(2U, elements.size());
@@ -234,7 +255,12 @@ TEST_F(FormManagerTest, FindForm) {
EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
EXPECT_EQ(GURL(web_frame->url()), form.origin);
EXPECT_EQ(GURL("http://buh.com"), form.action);
- EXPECT_EQ(ASCIIToUTF16("reply-send"), form.submit);
+
+ const std::vector<string16>& labels = form.labels;
+ ASSERT_EQ(3U, labels.size());
+ EXPECT_EQ(string16(), labels[0]);
+ EXPECT_EQ(string16(), labels[1]);
+ EXPECT_EQ(string16(), labels[2]);
const std::vector<string16>& elements = form.elements;
ASSERT_EQ(3U, elements.size());
@@ -278,7 +304,12 @@ TEST_F(FormManagerTest, FillForm) {
EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
EXPECT_EQ(GURL(web_frame->url()), form.origin);
EXPECT_EQ(GURL("http://buh.com"), form.action);
- EXPECT_EQ(ASCIIToUTF16("reply-send"), form.submit);
+
+ const std::vector<string16>& labels = form.labels;
+ ASSERT_EQ(3U, labels.size());
+ EXPECT_EQ(string16(), labels[0]);
+ EXPECT_EQ(string16(), labels[1]);
+ EXPECT_EQ(string16(), labels[2]);
const std::vector<string16>& elements = form.elements;
ASSERT_EQ(3U, elements.size());
@@ -304,7 +335,12 @@ TEST_F(FormManagerTest, FillForm) {
EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
EXPECT_EQ(GURL(web_frame->url()), form2.origin);
EXPECT_EQ(GURL("http://buh.com"), form2.action);
- EXPECT_EQ(ASCIIToUTF16("reply-send"), form2.submit);
+
+ const std::vector<string16>& labels2 = form2.labels;
+ ASSERT_EQ(3U, labels2.size());
+ EXPECT_EQ(string16(), labels2[0]);
+ EXPECT_EQ(string16(), labels2[1]);
+ EXPECT_EQ(string16(), labels2[2]);
const std::vector<string16>& elements2 = form2.elements;
ASSERT_EQ(3U, elements2.size());
@@ -344,3 +380,46 @@ TEST_F(FormManagerTest, Reset) {
form_manager.GetForms(&forms, FormManager::REQUIRE_NONE);
ASSERT_EQ(0U, forms.size());
}
+
+TEST_F(FormManagerTest, Labels) {
+ LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
+ " <LABEL for=\"firstname\"> First name: </LABEL>"
+ " <INPUT type=\"text\" id=\"firstname\" value=\"John\">"
+ " <LABEL for=\"lastname\"> Last name: </LABEL>"
+ " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\">"
+ " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\">"
+ "</FORM>");
+
+ WebFrame* web_frame = GetMainFrame();
+ ASSERT_TRUE(web_frame);
+
+ FormManager form_manager;
+ form_manager.ExtractForms(web_frame);
+
+ std::vector<FormData> forms;
+ form_manager.GetForms(&forms, FormManager::REQUIRE_NONE);
+ ASSERT_EQ(1U, forms.size());
+
+ const FormData& form = forms[0];
+ EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
+ EXPECT_EQ(GURL(web_frame->url()), form.origin);
+ EXPECT_EQ(GURL("http://cnn.com"), form.action);
+
+ const std::vector<string16>& labels = form.labels;
+ ASSERT_EQ(3U, labels.size());
+ EXPECT_EQ(ASCIIToUTF16("First name:"), labels[0]);
+ EXPECT_EQ(ASCIIToUTF16("Last name:"), labels[1]);
+ EXPECT_EQ(string16(), labels[2]);
+
+ const std::vector<string16>& elements = form.elements;
+ ASSERT_EQ(3U, elements.size());
+ EXPECT_EQ(ASCIIToUTF16("firstname"), elements[0]);
+ EXPECT_EQ(ASCIIToUTF16("lastname"), elements[1]);
+ EXPECT_EQ(ASCIIToUTF16("reply-send"), elements[2]);
+
+ const std::vector<string16>& values = form.values;
+ ASSERT_EQ(3U, values.size());
+ EXPECT_EQ(ASCIIToUTF16("John"), values[0]);
+ EXPECT_EQ(ASCIIToUTF16("Smith"), values[1]);
+ EXPECT_EQ(ASCIIToUTF16("Send"), values[2]);
+}
diff --git a/webkit/glue/form_data.h b/webkit/glue/form_data.h
index 02e0096..f1a7911 100644
--- a/webkit/glue/form_data.h
+++ b/webkit/glue/form_data.h
@@ -11,26 +11,19 @@
// Holds information about a form to be filled and/or submitted.
struct FormData {
- FormData() {}
- FormData(const FormData& data)
- : name(data.name),
- origin(data.origin),
- action(data.action),
- elements(data.elements),
- values(data.values),
- submit(data.submit) {}
-
// The name of the form.
string16 name;
// The URL (minus query parameters) containing the form
GURL origin;
// The action target of the form
GURL action;
- // A list of element names to be filled
+ // A vector of element labels.
+ std::vector<string16> labels;
+ // A vector of element names.
std::vector<string16> elements;
- // A list of element values to be filled
+ // A vector of element values.
std::vector<string16> values;
- // The name of the submit button to be used to submit (optional)
+ // The name of the submit button to be used to submit (optional).
string16 submit;
};