Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2240)

Unified Diff: components/autofill/content/renderer/form_autofill_util.cc

Issue 140093005: Add supports that allow Autofill to be initiated from textarea field (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update code as per Ilya's 3rd set of review comments - all comments addressed Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/autofill/content/renderer/form_autofill_util.cc
diff --git a/components/autofill/content/renderer/form_autofill_util.cc b/components/autofill/content/renderer/form_autofill_util.cc
index 0657f78a4f64bdd7ba7056470281269ed797bb4a..8dbebb275ed692c18d643fbf775c2dab43a98923 100644
--- a/components/autofill/content/renderer/form_autofill_util.cc
+++ b/components/autofill/content/renderer/form_autofill_util.cc
@@ -438,6 +438,64 @@ base::string16 InferLabelForElement(const WebFormControlElement& element) {
return InferLabelFromDivTable(element);
}
+// A simple wrapper to get an element's value.
+WebString GetValue(const WebFormControlElement& element) {
+ const WebInputElement* input_element = toWebInputElement(&element);
+ if (IsAutofillableInputElement(input_element))
+ return input_element->value();
+
+ if (IsTextAreaElement(element))
+ return element.toConst<WebTextAreaElement>().value();
+
+ DCHECK(IsSelectElement(element));
+ return element.toConst<WebSelectElement>().value();
+}
+
+// A simple wrapper to get an element's suggested value.
+WebString GetSuggestedValue(const WebFormControlElement& element) {
+ const WebInputElement* input_element = toWebInputElement(&element);
+ if (IsAutofillableInputElement(input_element))
+ return input_element->suggestedValue();
+
+ DCHECK(IsTextAreaElement(element));
+ return element.toConst<WebTextAreaElement>().suggestedValue();
+}
+
+// A simple wrapper to get an element's character selection range.
+void SetSelectionRange(WebFormControlElement element, int start, int end) {
+ WebInputElement* input_element = toWebInputElement(&element);
+ if (IsAutofillableInputElement(input_element)) {
+ input_element->setSelectionRange(start, end);
+ } else {
+ DCHECK(IsTextAreaElement(element));
+ element.to<WebTextAreaElement>().setSelectionRange(start, end);
+ }
+}
+
+// A helper function to set common attributes shared among elements.
+void SetCommonAttributes(const WebFormControlElement& element,
+ FormFieldData* field) {
+ const WebInputElement* input_element = toWebInputElement(&element);
+ if (!IsAutofillableInputElement(input_element) &&
+ !IsTextAreaElement(element))
+ return;
+
+ field->is_autofilled = element.isAutofilled();
+ field->is_focusable = element.isFocusable();
+ if (IsAutofillableInputElement(input_element)) {
+ field->should_autocomplete = input_element->autoComplete();
+ field->text_direction = input_element->directionForFormData() ==
+ "rtl" ? base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
+ } else {
+ DCHECK(IsTextAreaElement(element));
+ const WebTextAreaElement textarea_element =
+ element.toConst<WebTextAreaElement>();
+ field->should_autocomplete = textarea_element.autoComplete();
+ field->text_direction = textarea_element.directionForFormData() ==
+ "rtl" ? base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
+ }
+}
+
// Fills |option_strings| with the values of the <option> elements present in
// |select_element|.
void GetOptionStringsFromElement(const WebSelectElement& select_element,
@@ -539,12 +597,6 @@ void FillFormField(const FormFieldData& data,
// returns the default maxlength value.
input_element->setValue(
data.value.substr(0, input_element->maxLength()), true);
- if (is_initiating_node) {
- int length = input_element->value().length();
- input_element->setSelectionRange(length, length);
- // Clear the current IME composition (the underline), if there is one.
- input_element->document().frame()->unmarkText();
- }
} else if (IsTextAreaElement(*field)) {
WebTextAreaElement text_area = field->to<WebTextAreaElement>();
if (text_area.value() != data.value) {
@@ -561,6 +613,15 @@ void FillFormField(const FormFieldData& data,
DCHECK(IsCheckableElement(input_element));
input_element->setChecked(data.is_checked, true);
}
+
+ if (is_initiating_node &&
+ ((IsTextInput(input_element) || IsMonthInput(input_element)) ||
+ IsTextAreaElement(*field))) {
+ int length = GetValue(*field).length();
+ SetSelectionRange(*field, length, length);
+ // Clear the current IME composition (the underline), if there is one.
+ field->document().frame()->unmarkText();
+ }
}
// Sets the |field|'s "suggested" (non JS visible) value to the value in |data|.
@@ -582,17 +643,19 @@ void PreviewFormField(const FormFieldData& data,
input_element->setSuggestedValue(
data.value.substr(0, input_element->maxLength()));
input_element->setAutofilled(true);
- if (is_initiating_node) {
- // Select the part of the text that the user didn't type.
- input_element->setSelectionRange(
- input_element->value().length(),
- input_element->suggestedValue().length());
- }
} else if (IsTextAreaElement(*field)) {
WebTextAreaElement textarea = field->to<WebTextAreaElement>();
textarea.setSuggestedValue(data.value);
field->setAutofilled(true);
}
+
+ if (is_initiating_node &&
+ (IsTextInput(input_element) || IsTextAreaElement(*field))) {
+ // Select the part of the text that the user didn't type.
+ int start = GetValue(*field).length();
+ int end = GetSuggestedValue(*field).length();
+ SetSelectionRange(*field, start, end);
+ }
}
std::string RetrievalMethodToString(
@@ -771,18 +834,15 @@ void WebFormControlElementToFormField(const WebFormControlElement& element,
if (!IsAutofillableElement(element))
return;
+ SetCommonAttributes(element, field);
+
const WebInputElement* input_element = toWebInputElement(&element);
if (IsAutofillableInputElement(input_element)) {
if (IsTextInput(input_element))
field->max_length = input_element->maxLength();
- field->is_autofilled = input_element->isAutofilled();
- field->is_focusable = input_element->isFocusable();
field->is_checkable = IsCheckableElement(input_element);
field->is_checked = input_element->isChecked();
- field->should_autocomplete = input_element->autoComplete();
- field->text_direction = input_element->directionForFormData() == "rtl" ?
- base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
} else if (IsTextAreaElement(element)) {
// Nothing more to do in this case.
} else if (extract_mask & EXTRACT_OPTIONS) {
@@ -797,16 +857,10 @@ void WebFormControlElementToFormField(const WebFormControlElement& element,
if (!(extract_mask & EXTRACT_VALUE))
return;
- base::string16 value;
- if (IsAutofillableInputElement(input_element)) {
- value = input_element->value();
- } else if (IsTextAreaElement(element)) {
- value = element.toConst<WebTextAreaElement>().value();
- } else {
- DCHECK(IsSelectElement(element));
- const WebSelectElement select_element = element.toConst<WebSelectElement>();
- value = select_element.value();
+ base::string16 value = GetValue(element);
+ if (IsSelectElement(element)) {
+ const WebSelectElement select_element = element.toConst<WebSelectElement>();
// Convert the |select_element| value to text if requested.
if (extract_mask & EXTRACT_OPTION_TEXT) {
WebVector<WebElement> list_items = select_element.listItems();
@@ -971,10 +1025,10 @@ bool WebFormElementToFormData(
return true;
}
-bool FindFormAndFieldForInputElement(const WebInputElement& element,
- FormData* form,
- FormFieldData* field,
- RequirementsMask requirements) {
+bool FindFormAndFieldForFormControlElement(const WebFormControlElement& element,
+ FormData* form,
+ FormFieldData* field,
+ RequirementsMask requirements) {
if (!IsAutofillableElement(element))
return false;
@@ -992,7 +1046,7 @@ bool FindFormAndFieldForInputElement(const WebInputElement& element,
field);
}
-void FillForm(const FormData& form, const WebInputElement& element) {
+void FillForm(const FormData& form, const WebFormControlElement& element) {
WebFormElement form_element = element.form();
if (form_element.isNull())
return;
@@ -1033,7 +1087,7 @@ void FillFormForAllElements(const FormData& form_data,
&FillFormField);
}
-void PreviewForm(const FormData& form, const WebInputElement& element) {
+void PreviewForm(const FormData& form, const WebFormControlElement& element) {
WebFormElement form_element = element.form();
if (form_element.isNull())
return;
@@ -1046,7 +1100,7 @@ void PreviewForm(const FormData& form, const WebInputElement& element) {
&PreviewFormField);
}
-bool ClearPreviewedFormWithElement(const WebInputElement& element,
+bool ClearPreviewedFormWithElement(const WebFormControlElement& element,
bool was_autofilled) {
WebFormElement form_element = element.form();
if (form_element.isNull())
@@ -1080,30 +1134,31 @@ bool ClearPreviewedFormWithElement(const WebInputElement& element,
// Clear the suggested value. For the initiating node, also restore the
// original value.
+ bool is_initiating_node;
Ilya Sherman 2014/03/01 02:50:53 Please make sure to initialize this variable here,
ziran.sun 2014/03/03 19:23:32 Done.
if (IsTextInput(input_element)) {
input_element->setSuggestedValue(WebString());
- bool is_initiating_node = (element == *input_element);
+ is_initiating_node = (element == *input_element);
if (is_initiating_node)
input_element->setAutofilled(was_autofilled);
else
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);
- }
} else if (IsTextAreaElement(control_element)) {
WebTextAreaElement text_area = control_element.to<WebTextAreaElement>();
text_area.setSuggestedValue(WebString());
- bool is_initiating_node = (element == text_area);
+ is_initiating_node = (element == text_area);
if (is_initiating_node)
control_element.setAutofilled(was_autofilled);
else
control_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 = GetValue(control_element).length();
+ SetSelectionRange(control_element, length, length);
+ }
}
return true;
@@ -1179,7 +1234,7 @@ bool IsWebElementEmpty(const blink::WebElement& element) {
return true;
}
-gfx::RectF GetScaledBoundingBox(float scale, WebInputElement* element) {
+gfx::RectF GetScaledBoundingBox(float scale, WebFormControlElement* element) {
gfx::Rect bounding_box(element->boundsInViewportSpace());
return gfx::RectF(bounding_box.x() * scale,
bounding_box.y() * scale,

Powered by Google App Engine
This is Rietveld 408576698