diff options
author | dhollowa@chromium.org <dhollowa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-11 04:18:19 +0000 |
---|---|---|
committer | dhollowa@chromium.org <dhollowa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-11 04:18:19 +0000 |
commit | aa1182d062df9f2c1e0e549b7e15c3f58b574768 (patch) | |
tree | a61a032c63bf69cc6ad88094b6383087a7a532b2 | |
parent | ee1adf4b505e1c93d7141d976070aec601e25f21 (diff) | |
download | chromium_src-aa1182d062df9f2c1e0e549b7e15c3f58b574768.zip chromium_src-aa1182d062df9f2c1e0e549b7e15c3f58b574768.tar.gz chromium_src-aa1182d062df9f2c1e0e549b7e15c3f58b574768.tar.bz2 |
Autofill - "Clear form" failed to clear the select popup values
Enhances Autofill "Clear form" functionality to reset select-one input elements to their original values. The initial values are captured during form load and saved in the FormManager cache.
BUG=50412
TEST=FormManagerTest.ClearFormWithNodeContainingSelectOne
Review URL: http://codereview.chromium.org/3707001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@62133 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/renderer/form_manager.cc | 30 | ||||
-rw-r--r-- | chrome/renderer/form_manager.h | 3 | ||||
-rw-r--r-- | chrome/renderer/form_manager_browsertest.cc | 79 | ||||
-rw-r--r-- | webkit/glue/form_field.h | 2 |
4 files changed, 105 insertions, 9 deletions
diff --git a/chrome/renderer/form_manager.cc b/chrome/renderer/form_manager.cc index 2de7f0f..fc0db89 100644 --- a/chrome/renderer/form_manager.cc +++ b/chrome/renderer/form_manager.cc @@ -456,6 +456,17 @@ void FormManager::ExtractForms(const WebFrame* frame) { for (size_t j = 0; j < control_elements.size(); ++j) { WebFormControlElement element = control_elements[j]; form_elements->control_elements.push_back(element); + + // Save original values of "select-one" inputs so we can restore them + // when |ClearFormWithNode()| is invoked. + if (element.formControlType() == WebString::fromUTF8("select-one")) { + WebFormControlElement& e = const_cast<WebFormControlElement&>(element); + WebSelectElement select_element = e.to<WebSelectElement>(); + string16 value = select_element.value(); + form_elements->control_values.push_back(value); + } else { + form_elements->control_values.push_back(string16()); + } } form_elements_.push_back(form_elements); @@ -563,17 +574,20 @@ bool FormManager::ClearFormWithNode(const WebNode& node) { for (size_t i = 0; i < form_element->control_elements.size(); ++i) { WebFormControlElement element = form_element->control_elements[i]; - if (element.formControlType() != WebString::fromUTF8("text")) - continue; + if (element.formControlType() == WebString::fromUTF8("text")) { - WebInputElement input_element = element.to<WebInputElement>(); + WebInputElement input_element = element.to<WebInputElement>(); - // We don't modify the value of disabled fields. - if (!input_element.isEnabled()) - continue; + // We don't modify the value of disabled fields. + if (!input_element.isEnabled()) + continue; - input_element.setValue(string16()); - input_element.setAutofilled(false); + input_element.setValue(string16()); + input_element.setAutofilled(false); + } else if (element.formControlType() == WebString::fromUTF8("select-one")) { + WebSelectElement select_element = element.to<WebSelectElement>(); + select_element.setValue(form_element->control_values[i]); + } } return true; diff --git a/chrome/renderer/form_manager.h b/chrome/renderer/form_manager.h index 7d09d02..a8643d9 100644 --- a/chrome/renderer/form_manager.h +++ b/chrome/renderer/form_manager.h @@ -113,9 +113,12 @@ class FormManager { private: // Stores the WebFormElement and the form control elements for a form. + // Original form values are stored so when we clear a form we can reset + // "select-one" values to their original state. struct FormElement { WebKit::WebFormElement form_element; std::vector<WebKit::WebFormControlElement> control_elements; + std::vector<string16> control_values; }; // Type for cache of FormElement objects. diff --git a/chrome/renderer/form_manager_browsertest.cc b/chrome/renderer/form_manager_browsertest.cc index a82dbe6..a4fe748 100644 --- a/chrome/renderer/form_manager_browsertest.cc +++ b/chrome/renderer/form_manager_browsertest.cc @@ -12,6 +12,7 @@ #include "third_party/WebKit/WebKit/chromium/public/WebFormControlElement.h" #include "third_party/WebKit/WebKit/chromium/public/WebFormElement.h" #include "third_party/WebKit/WebKit/chromium/public/WebInputElement.h" +#include "third_party/WebKit/WebKit/chromium/public/WebSelectElement.h" #include "third_party/WebKit/WebKit/chromium/public/WebNode.h" #include "third_party/WebKit/WebKit/chromium/public/WebString.h" #include "third_party/WebKit/WebKit/chromium/public/WebVector.h" @@ -24,6 +25,7 @@ using WebKit::WebFormControlElement; using WebKit::WebFormElement; using WebKit::WebFrame; using WebKit::WebInputElement; +using WebKit::WebSelectElement; using WebKit::WebNode; using WebKit::WebString; using WebKit::WebVector; @@ -2341,6 +2343,83 @@ TEST_F(FormManagerTest, ClearFormWithNode) { 0))); } +TEST_F(FormManagerTest, ClearFormWithNodeContainingSelectOne) { + LoadHTML( + "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">" + " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>" + " <INPUT type=\"text\" id=\"lastname\" value=\"Earp\"/>" + " <SELECT id=\"state\" name=\"state\">" + " <OPTION selected>?</OPTION>" + " <OPTION>AA</OPTION>" + " <OPTION>AE</OPTION>" + " <OPTION>AK</OPTION>" + " </SELECT>" + " <INPUT type=\"submit\" value=\"Send\"/>" + "</FORM>"); + + WebFrame* web_frame = GetMainFrame(); + ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame); + + FormManager form_manager; + form_manager.ExtractForms(web_frame); + + // Verify that we have the form. + std::vector<FormData> forms; + form_manager.GetFormsInFrame(web_frame, FormManager::REQUIRE_NONE, &forms); + ASSERT_EQ(1U, forms.size()); + + // Set the auto-filled attribute on the firstname element. + WebInputElement firstname = + web_frame->document().getElementById("firstname").to<WebInputElement>(); + firstname.setAutofilled(true); + + // Set the value of the select-one. + WebSelectElement select_element = + web_frame->document().getElementById("state").to<WebSelectElement>(); + select_element.setValue(WebString::fromUTF8("AK")); + + // Clear the form. + EXPECT_TRUE(form_manager.ClearFormWithNode(firstname)); + + // Verify that the auto-filled attribute has been turned off. + EXPECT_FALSE(firstname.isAutofilled()); + + // Verify the form is cleared. + FormData form2; + EXPECT_TRUE(form_manager.FindFormWithFormControlElement( + firstname, FormManager::REQUIRE_NONE, &form2)); + EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name); + EXPECT_EQ(GURL(web_frame->url()), form2.origin); + EXPECT_EQ(GURL("http://buh.com"), form2.action); + + const std::vector<FormField>& fields2 = form2.fields; + ASSERT_EQ(4U, fields2.size()); + EXPECT_TRUE(fields2[0].StrictlyEqualsHack( + FormField(string16(), + ASCIIToUTF16("firstname"), + string16(), + ASCIIToUTF16("text"), + 20))); + EXPECT_TRUE(fields2[1].StrictlyEqualsHack( + FormField(string16(), + ASCIIToUTF16("lastname"), + string16(), + ASCIIToUTF16("text"), + 20))); + EXPECT_TRUE(fields2[2].StrictlyEqualsHack( + FormField(string16(), + ASCIIToUTF16("state"), + ASCIIToUTF16("?"), + ASCIIToUTF16("select-one"), + 0))); + EXPECT_TRUE(fields2[3].StrictlyEqualsHack( + FormField(string16(), + string16(), + string16(), + ASCIIToUTF16("submit"), + 0))); +} + TEST_F(FormManagerTest, ClearPreviewedFormWithNode) { LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">" " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>" diff --git a/webkit/glue/form_field.h b/webkit/glue/form_field.h index fe7aa69..1ed8d68 100644 --- a/webkit/glue/form_field.h +++ b/webkit/glue/form_field.h @@ -67,7 +67,7 @@ class FormField { }; // So we can compare FormFields with EXPECT_EQ(). -std::ostream& operator<<(std::ostream& os, const FormField& profile); +std::ostream& operator<<(std::ostream& os, const FormField& field); } // namespace webkit_glue |