summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/autofill
diff options
context:
space:
mode:
authordhollowa@chromium.org <dhollowa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-02-03 20:52:28 +0000
committerdhollowa@chromium.org <dhollowa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-02-03 20:52:28 +0000
commita1107c5d54db86a52e2aa04ed090112e569442ae (patch)
tree1610a68124ff3a9375619a0c381975c20d8cebfc /chrome/renderer/autofill
parent93120fea1fb7a895ef0469bb95540aa39520c691 (diff)
downloadchromium_src-a1107c5d54db86a52e2aa04ed090112e569442ae.zip
chromium_src-a1107c5d54db86a52e2aa04ed090112e569442ae.tar.gz
chromium_src-a1107c5d54db86a52e2aa04ed090112e569442ae.tar.bz2
Support autocompletion for HTMl5 tags:"email", "month" and "tel".
BUG=65654 TEST=1.make HTML5 forms with the following input types: - Email input: <input type="email"> - Date picker: <input type="month"> for credit card expiration. - Telephone input: <input type="tel"> - Placeholder text: <input name="address" placeholder="1 Main Street"/> 2.Try to fill. Implement tests for email, month and tel input type. Also this patch needs WebKit patch, please refer to https://bugs.webkit.org/show_bug.cgi?id=51809 Actually, original issue item says placeholder problem, but I cannot reproduce manually. Review URL: http://codereview.chromium.org/6033010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@73660 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/autofill')
-rw-r--r--chrome/renderer/autofill/form_manager.cc116
-rw-r--r--chrome/renderer/autofill/form_manager_browsertest.cc51
2 files changed, 109 insertions, 58 deletions
diff --git a/chrome/renderer/autofill/form_manager.cc b/chrome/renderer/autofill/form_manager.cc
index cd24300..3e2b482 100644
--- a/chrome/renderer/autofill/form_manager.cc
+++ b/chrome/renderer/autofill/form_manager.cc
@@ -52,10 +52,13 @@ const size_t kRequiredAutoFillFields = 3;
// The maximum length allowed for form data.
const size_t kMaxDataLength = 1024;
-// TODO(isherman): Replace calls to this with IsTextInput() once
-// http://codereview.chromium.org/6033010/ lands.
-bool IsTextElement(const WebFormControlElement& element) {
- return element.formControlType() == WebString::fromUTF8("text");
+// In HTML5, all text fields except password are text input fields to
+// autocomplete.
+bool IsTextInput(const WebInputElement* element) {
+ if (!element)
+ return false;
+
+ return element->isTextField() && !element->isPasswordField();
}
bool IsSelectElement(const WebFormControlElement& element) {
@@ -335,7 +338,8 @@ void FormManager::WebFormControlElementToFormField(
field->set_name(element.nameForAutofill());
field->set_form_control_type(element.formControlType());
- if (IsTextElement(element)) {
+ const WebInputElement* input_element = toWebInputElement(&element);
+ if (IsTextInput(input_element)) {
const WebInputElement& input_element = element.toConst<WebInputElement>();
field->set_max_length(input_element.maxLength());
field->set_autofilled(input_element.isAutofilled());
@@ -350,10 +354,9 @@ void FormManager::WebFormControlElementToFormField(
return;
string16 value;
- if (IsTextElement(element) ||
+ if (IsTextInput(input_element) ||
element.formControlType() == WebString::fromUTF8("hidden")) {
- const WebInputElement& input_element = element.toConst<WebInputElement>();
- value = input_element.value();
+ value = input_element->value();
} else if (IsSelectElement(element)) {
const WebSelectElement select_element = element.toConst<WebSelectElement>();
value = select_element.value();
@@ -444,12 +447,10 @@ bool FormManager::WebFormElementToFormData(const WebFormElement& element,
for (size_t i = 0; i < control_elements.size(); ++i) {
const WebFormControlElement& control_element = control_elements[i];
- if (requirements & REQUIRE_AUTOCOMPLETE && IsTextElement(control_element)) {
- const WebInputElement& input_element =
- control_element.toConst<WebInputElement>();
- if (!input_element.autoComplete())
- continue;
- }
+ const WebInputElement* input_element = toWebInputElement(&control_element);
+ if (requirements & REQUIRE_AUTOCOMPLETE && IsTextInput(input_element) &&
+ !input_element->autoComplete())
+ continue;
if (requirements & REQUIRE_ENABLED && !control_element.isEnabled())
continue;
@@ -654,21 +655,21 @@ 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 (IsTextElement(element)) {
- WebInputElement input_element = element.to<WebInputElement>();
+ WebInputElement* input_element = toWebInputElement(&element);
+ if (IsTextInput(input_element)) {
// We don't modify the value of disabled fields.
- if (!input_element.isEnabled())
+ if (!input_element->isEnabled())
continue;
- input_element.setValue(string16());
- input_element.setAutofilled(false);
+ input_element->setValue(string16());
+ input_element->setAutofilled(false);
// Clearing the value in the focused node (above) can cause selection
// to be lost. We force selection range to restore the text cursor.
- if (node == input_element) {
- int length = input_element.value().length();
- input_element.setSelectionRange(length, length);
+ if (node == *input_element) {
+ int length = input_element->value().length();
+ input_element->setSelectionRange(length, length);
}
} else if (IsSelectElement(element)) {
WebSelectElement select_element = element.to<WebSelectElement>();
@@ -686,44 +687,43 @@ bool FormManager::ClearPreviewedFormWithNode(const WebNode& node,
return false;
for (size_t i = 0; i < form_element->control_elements.size(); ++i) {
- WebFormControlElement* element = &form_element->control_elements[i];
-
+ WebInputElement* input_element =
+ toWebInputElement(&form_element->control_elements[i]);
// Only text input elements can be previewed.
- if (!IsTextElement(*element))
+ if (!IsTextInput(input_element))
continue;
// If the input element has not been auto-filled, FormManager has not
// previewed this field, so we have nothing to reset.
- WebInputElement input_element = element->to<WebInputElement>();
- if (!input_element.isAutofilled())
+ if (!input_element->isAutofilled())
continue;
// There might be unrelated elements in this form which have already been
// auto-filled. For example, the user might have already filled the address
// part of a form and now be dealing with the credit card section. We only
// want to reset the auto-filled status for fields that were previewed.
- if (input_element.suggestedValue().isEmpty())
+ if (input_element->suggestedValue().isEmpty())
continue;
// Clear the suggested value. For the initiating node, also restore the
// original value.
- input_element.setSuggestedValue(WebString());
- bool is_initiating_node = (node == input_element);
+ input_element->setSuggestedValue(WebString());
+ bool is_initiating_node = (node == *input_element);
if (is_initiating_node) {
// Call |setValue()| to force the renderer to update the field's displayed
// value.
- input_element.setValue(input_element.value());
- input_element.setAutofilled(was_autofilled);
+ input_element->setValue(input_element->value());
+ input_element->setAutofilled(was_autofilled);
} else {
- input_element.setAutofilled(false);
+ input_element->setAutofilled(false);
}
// Clearing the suggested value in the focused node (above) can cause
// selection to be lost. We force selection range to restore the text
// cursor.
if (is_initiating_node) {
- int length = input_element.value().length();
- input_element.setSelectionRange(length, length);
+ int length = input_element->value().length();
+ input_element->setSelectionRange(length, length);
}
}
@@ -750,12 +750,12 @@ bool FormManager::FormWithNodeIsAutoFilled(const WebNode& node) {
return false;
for (size_t i = 0; i < form_element->control_elements.size(); ++i) {
- WebFormControlElement element = form_element->control_elements[i];
- if (!IsTextElement(element))
+ WebInputElement* input_element =
+ toWebInputElement(&form_element->control_elements[i]);
+ if (!IsTextInput(input_element))
continue;
- const WebInputElement& input_element = element.to<WebInputElement>();
- if (input_element.isAutofilled())
+ if (input_element->isAutofilled())
return true;
}
@@ -836,22 +836,21 @@ void FormManager::ForEachMatchingFormField(FormElement* form,
// More than likely |requirements| will contain REQUIRE_AUTOCOMPLETE and/or
// REQUIRE_EMPTY, which both require text form control elements, so special-
// case this type of element.
- if (IsTextElement(*element)) {
- const WebInputElement& input_element =
- element->toConst<WebInputElement>();
+ const WebInputElement* input_element = toWebInputElement(element);
+ if (IsTextInput(input_element)) {
// TODO(jhawkins): WebKit currently doesn't handle the autocomplete
// attribute for select control elements, but it probably should.
- if (requirements & REQUIRE_AUTOCOMPLETE && !input_element.autoComplete())
+ if (requirements & REQUIRE_AUTOCOMPLETE && !input_element->autoComplete())
continue;
- is_initiating_node = (input_element == node);
+ is_initiating_node = (*input_element == node);
// Don't require the node that initiated the auto-fill process to be
// empty. The user is typing in this field and we should complete the
// value when the user selects a value to fill out.
if (requirements & REQUIRE_EMPTY &&
!is_initiating_node &&
- !input_element.value().isEmpty())
+ !input_element->value().isEmpty())
continue;
}
@@ -874,16 +873,17 @@ void FormManager::FillFormField(WebFormControlElement* field,
if (data->value().empty())
return;
- if (IsTextElement(*field)) {
- WebInputElement input_element = field->to<WebInputElement>();
+ WebInputElement* input_element = toWebInputElement(field);
+ if (IsTextInput(input_element)) {
// If the maxlength attribute contains a negative value, maxLength()
// returns the default maxlength value.
- input_element.setValue(data->value().substr(0, input_element.maxLength()));
- input_element.setAutofilled(true);
+ input_element->setValue(
+ data->value().substr(0, input_element->maxLength()));
+ input_element->setAutofilled(true);
if (is_initiating_node) {
- int length = input_element.value().length();
- input_element.setSelectionRange(length, length);
+ int length = input_element->value().length();
+ input_element->setSelectionRange(length, length);
}
} else if (IsSelectElement(*field)) {
WebSelectElement select_element = field->to<WebSelectElement>();
@@ -899,18 +899,18 @@ void FormManager::PreviewFormField(WebFormControlElement* field,
return;
// Only preview input fields.
- if (!IsTextElement(*field))
+ WebInputElement* input_element = toWebInputElement(field);
+ if (!IsTextInput(input_element))
return;
- WebInputElement input_element = field->to<WebInputElement>();
-
// If the maxlength attribute contains a negative value, maxLength()
// returns the default maxlength value.
- input_element.setSuggestedValue(
- data->value().substr(0, input_element.maxLength()));
- input_element.setAutofilled(true);
+ input_element->setSuggestedValue(
+ data->value().substr(0, input_element->maxLength()));
+ input_element->setAutofilled(true);
if (is_initiating_node)
- input_element.setSelectionRange(0, input_element.suggestedValue().length());
+ input_element->setSelectionRange(0,
+ input_element->suggestedValue().length());
}
} // namespace autofill
diff --git a/chrome/renderer/autofill/form_manager_browsertest.cc b/chrome/renderer/autofill/form_manager_browsertest.cc
index ca71894..d6243fa 100644
--- a/chrome/renderer/autofill/form_manager_browsertest.cc
+++ b/chrome/renderer/autofill/form_manager_browsertest.cc
@@ -2846,6 +2846,8 @@ TEST_F(FormManagerTest, ClearPreviewedFormWithNode) {
" <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
" <INPUT type=\"text\" id=\"lastname\"/>"
" <INPUT type=\"text\" id=\"email\"/>"
+ " <INPUT type=\"email\" id=\"email2\"/>"
+ " <INPUT type=\"tel\" id=\"phone\"/>"
" <INPUT type=\"submit\" value=\"Send\"/>"
"</FORM>");
@@ -2870,10 +2872,18 @@ TEST_F(FormManagerTest, ClearPreviewedFormWithNode) {
WebInputElement email =
web_frame->document().getElementById("email").to<WebInputElement>();
email.setAutofilled(true);
+ WebInputElement email2 =
+ web_frame->document().getElementById("email2").to<WebInputElement>();
+ email2.setAutofilled(true);
+ WebInputElement phone =
+ web_frame->document().getElementById("phone").to<WebInputElement>();
+ phone.setAutofilled(true);
// Set the suggested values on two of the elements.
lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
+ email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
+ phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
// Clear the previewed fields.
EXPECT_TRUE(form_manager.ClearPreviewedFormWithNode(lastname, false));
@@ -2890,6 +2900,12 @@ TEST_F(FormManagerTest, ClearPreviewedFormWithNode) {
EXPECT_TRUE(email.value().isEmpty());
EXPECT_TRUE(email.suggestedValue().isEmpty());
EXPECT_FALSE(email.isAutofilled());
+ EXPECT_TRUE(email2.value().isEmpty());
+ EXPECT_TRUE(email2.suggestedValue().isEmpty());
+ EXPECT_FALSE(email2.isAutofilled());
+ EXPECT_TRUE(phone.value().isEmpty());
+ EXPECT_TRUE(phone.suggestedValue().isEmpty());
+ EXPECT_FALSE(phone.isAutofilled());
// Verify that the cursor position has been updated.
EXPECT_EQ(0, lastname.selectionStart());
@@ -2901,6 +2917,8 @@ TEST_F(FormManagerTest, ClearPreviewedFormWithNonEmptyInitiatingNode) {
" <INPUT type=\"text\" id=\"firstname\" value=\"W\"/>"
" <INPUT type=\"text\" id=\"lastname\"/>"
" <INPUT type=\"text\" id=\"email\"/>"
+ " <INPUT type=\"email\" id=\"email2\"/>"
+ " <INPUT type=\"tel\" id=\"phone\"/>"
" <INPUT type=\"submit\" value=\"Send\"/>"
"</FORM>");
@@ -2925,11 +2943,20 @@ TEST_F(FormManagerTest, ClearPreviewedFormWithNonEmptyInitiatingNode) {
WebInputElement email =
web_frame->document().getElementById("email").to<WebInputElement>();
email.setAutofilled(true);
+ WebInputElement email2 =
+ web_frame->document().getElementById("email2").to<WebInputElement>();
+ email2.setAutofilled(true);
+ WebInputElement phone =
+ web_frame->document().getElementById("phone").to<WebInputElement>();
+ phone.setAutofilled(true);
+
// Set the suggested values on all of the elements.
firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
+ email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
+ phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
// Clear the previewed fields.
EXPECT_TRUE(form_manager.ClearPreviewedFormWithNode(firstname, false));
@@ -2948,6 +2975,12 @@ TEST_F(FormManagerTest, ClearPreviewedFormWithNonEmptyInitiatingNode) {
EXPECT_TRUE(email.value().isEmpty());
EXPECT_TRUE(email.suggestedValue().isEmpty());
EXPECT_FALSE(email.isAutofilled());
+ EXPECT_TRUE(email2.value().isEmpty());
+ EXPECT_TRUE(email2.suggestedValue().isEmpty());
+ EXPECT_FALSE(email2.isAutofilled());
+ EXPECT_TRUE(phone.value().isEmpty());
+ EXPECT_TRUE(phone.suggestedValue().isEmpty());
+ EXPECT_FALSE(phone.isAutofilled());
}
TEST_F(FormManagerTest, ClearPreviewedFormWithAutofilledInitiatingNode) {
@@ -2955,6 +2988,8 @@ TEST_F(FormManagerTest, ClearPreviewedFormWithAutofilledInitiatingNode) {
" <INPUT type=\"text\" id=\"firstname\" value=\"W\"/>"
" <INPUT type=\"text\" id=\"lastname\"/>"
" <INPUT type=\"text\" id=\"email\"/>"
+ " <INPUT type=\"email\" id=\"email2\"/>"
+ " <INPUT type=\"tel\" id=\"phone\"/>"
" <INPUT type=\"submit\" value=\"Send\"/>"
"</FORM>");
@@ -2979,11 +3014,19 @@ TEST_F(FormManagerTest, ClearPreviewedFormWithAutofilledInitiatingNode) {
WebInputElement email =
web_frame->document().getElementById("email").to<WebInputElement>();
email.setAutofilled(true);
+ WebInputElement email2 =
+ web_frame->document().getElementById("email2").to<WebInputElement>();
+ email2.setAutofilled(true);
+ WebInputElement phone =
+ web_frame->document().getElementById("phone").to<WebInputElement>();
+ phone.setAutofilled(true);
// Set the suggested values on all of the elements.
firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
+ email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
+ phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
// Clear the previewed fields.
EXPECT_TRUE(form_manager.ClearPreviewedFormWithNode(firstname, true));
@@ -3002,6 +3045,12 @@ TEST_F(FormManagerTest, ClearPreviewedFormWithAutofilledInitiatingNode) {
EXPECT_TRUE(email.value().isEmpty());
EXPECT_TRUE(email.suggestedValue().isEmpty());
EXPECT_FALSE(email.isAutofilled());
+ EXPECT_TRUE(email2.value().isEmpty());
+ EXPECT_TRUE(email2.suggestedValue().isEmpty());
+ EXPECT_FALSE(email2.isAutofilled());
+ EXPECT_TRUE(phone.value().isEmpty());
+ EXPECT_TRUE(phone.suggestedValue().isEmpty());
+ EXPECT_FALSE(phone.isAutofilled());
}
TEST_F(FormManagerTest, FormWithNodeIsAutoFilled) {
@@ -3009,6 +3058,8 @@ TEST_F(FormManagerTest, FormWithNodeIsAutoFilled) {
" <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
" <INPUT type=\"text\" id=\"lastname\"/>"
" <INPUT type=\"text\" id=\"email\"/>"
+ " <INPUT type=\"email\" id=\"email2\"/>"
+ " <INPUT type=\"tel\" id=\"phone\"/>"
" <INPUT type=\"submit\" value=\"Send\"/>"
"</FORM>");