| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/autofill/content/renderer/form_autofill_util.h" | 5 #include "components/autofill/content/renderer/form_autofill_util.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 | 560 |
| 561 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|. | 561 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|. |
| 562 // Also sets the "autofilled" attribute, causing the background to be yellow. | 562 // Also sets the "autofilled" attribute, causing the background to be yellow. |
| 563 void PreviewFormField(const FormFieldData& data, | 563 void PreviewFormField(const FormFieldData& data, |
| 564 bool is_initiating_node, | 564 bool is_initiating_node, |
| 565 blink::WebFormControlElement* field) { | 565 blink::WebFormControlElement* field) { |
| 566 // Nothing to preview. | 566 // Nothing to preview. |
| 567 if (data.value.empty()) | 567 if (data.value.empty()) |
| 568 return; | 568 return; |
| 569 | 569 |
| 570 // Preview input and textarea fields. For input fields, excludes checkboxes | 570 // Preview input, textarea and select fields. For input fields, excludes |
| 571 // and radio buttons, as there is no provision for setSuggestedCheckedValue | 571 // checkboxes and radio buttons, as there is no provision for |
| 572 // in WebInputElement. | 572 // setSuggestedCheckedValue in WebInputElement. |
| 573 WebInputElement* input_element = toWebInputElement(field); | 573 WebInputElement* input_element = toWebInputElement(field); |
| 574 if (IsTextInput(input_element) || IsMonthInput(input_element)) { | 574 if (IsTextInput(input_element) || IsMonthInput(input_element)) { |
| 575 // If the maxlength attribute contains a negative value, maxLength() | 575 // If the maxlength attribute contains a negative value, maxLength() |
| 576 // returns the default maxlength value. | 576 // returns the default maxlength value. |
| 577 input_element->setSuggestedValue( | 577 input_element->setSuggestedValue( |
| 578 data.value.substr(0, input_element->maxLength())); | 578 data.value.substr(0, input_element->maxLength())); |
| 579 input_element->setAutofilled(true); | 579 input_element->setAutofilled(true); |
| 580 } else if (IsTextAreaElement(*field)) { | 580 } else if (IsTextAreaElement(*field) || IsSelectElement(*field)) { |
| 581 field->setSuggestedValue(data.value); | 581 field->setSuggestedValue(data.value); |
| 582 field->setAutofilled(true); | 582 field->setAutofilled(true); |
| 583 } | 583 } |
| 584 | 584 |
| 585 if (is_initiating_node && | 585 if (is_initiating_node && |
| 586 (IsTextInput(input_element) || IsTextAreaElement(*field))) { | 586 (IsTextInput(input_element) || IsTextAreaElement(*field))) { |
| 587 // Select the part of the text that the user didn't type. | 587 // Select the part of the text that the user didn't type. |
| 588 int start = field->value().length(); | 588 int start = field->value().length(); |
| 589 int end = field->suggestedValue().length(); | 589 int end = field->suggestedValue().length(); |
| 590 field->setSelectionRange(start, end); | 590 field->setSelectionRange(start, end); |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1049 std::vector<WebFormControlElement> control_elements; | 1049 std::vector<WebFormControlElement> control_elements; |
| 1050 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, | 1050 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, |
| 1051 &control_elements); | 1051 &control_elements); |
| 1052 for (size_t i = 0; i < control_elements.size(); ++i) { | 1052 for (size_t i = 0; i < control_elements.size(); ++i) { |
| 1053 // There might be unrelated elements in this form which have already been | 1053 // There might be unrelated elements in this form which have already been |
| 1054 // auto-filled. For example, the user might have already filled the address | 1054 // auto-filled. For example, the user might have already filled the address |
| 1055 // part of a form and now be dealing with the credit card section. We only | 1055 // part of a form and now be dealing with the credit card section. We only |
| 1056 // want to reset the auto-filled status for fields that were previewed. | 1056 // want to reset the auto-filled status for fields that were previewed. |
| 1057 WebFormControlElement control_element = control_elements[i]; | 1057 WebFormControlElement control_element = control_elements[i]; |
| 1058 | 1058 |
| 1059 // Only text input and textarea elements can be previewed. | 1059 // Only text input, textarea and select elements can be previewed. |
| 1060 WebInputElement* input_element = toWebInputElement(&control_element); | 1060 WebInputElement* input_element = toWebInputElement(&control_element); |
| 1061 if (!IsTextInput(input_element) && | 1061 if (!IsTextInput(input_element) && |
| 1062 !IsMonthInput(input_element) && | 1062 !IsMonthInput(input_element) && |
| 1063 !IsTextAreaElement(control_element)) | 1063 !IsTextAreaElement(control_element) && |
| 1064 !IsSelectElement(control_element)) |
| 1064 continue; | 1065 continue; |
| 1065 | 1066 |
| 1066 // If the element is not auto-filled, we did not preview it, | 1067 // If the element is not auto-filled, we did not preview it, |
| 1067 // so there is nothing to reset. | 1068 // so there is nothing to reset. |
| 1068 if(!control_element.isAutofilled()) | 1069 if(!control_element.isAutofilled()) |
| 1069 continue; | 1070 continue; |
| 1070 | 1071 |
| 1071 if ((IsTextInput(input_element) || | 1072 if ((IsTextInput(input_element) || |
| 1072 IsMonthInput(input_element) || | 1073 IsMonthInput(input_element) || |
| 1073 IsTextAreaElement(control_element)) && | 1074 IsTextAreaElement(control_element) || |
| 1075 IsSelectElement(control_element)) && |
| 1074 control_element.suggestedValue().isEmpty()) | 1076 control_element.suggestedValue().isEmpty()) |
| 1075 continue; | 1077 continue; |
| 1076 | 1078 |
| 1077 // Clear the suggested value. For the initiating node, also restore the | 1079 // Clear the suggested value. For the initiating node, also restore the |
| 1078 // original value. | 1080 // original value. |
| 1079 if (IsTextInput(input_element) || IsMonthInput(input_element) || | 1081 if (IsTextInput(input_element) || IsMonthInput(input_element) || |
| 1080 IsTextAreaElement(control_element)) { | 1082 IsTextAreaElement(control_element)) { |
| 1081 control_element.setSuggestedValue(WebString()); | 1083 control_element.setSuggestedValue(WebString()); |
| 1082 bool is_initiating_node = (element == control_element); | 1084 bool is_initiating_node = (element == control_element); |
| 1083 if (is_initiating_node) { | 1085 if (is_initiating_node) { |
| 1084 control_element.setAutofilled(was_autofilled); | 1086 control_element.setAutofilled(was_autofilled); |
| 1085 // Clearing the suggested value in the focused node (above) can cause | 1087 // Clearing the suggested value in the focused node (above) can cause |
| 1086 // selection to be lost. We force selection range to restore the text | 1088 // selection to be lost. We force selection range to restore the text |
| 1087 // cursor. | 1089 // cursor. |
| 1088 int length = control_element.value().length(); | 1090 int length = control_element.value().length(); |
| 1089 control_element.setSelectionRange(length, length); | 1091 control_element.setSelectionRange(length, length); |
| 1090 } else { | 1092 } else { |
| 1091 control_element.setAutofilled(false); | 1093 control_element.setAutofilled(false); |
| 1092 } | 1094 } |
| 1095 } else if (IsSelectElement(control_element)) { |
| 1096 control_element.setSuggestedValue(WebString()); |
| 1097 control_element.setAutofilled(false); |
| 1093 } | 1098 } |
| 1094 } | 1099 } |
| 1095 | 1100 |
| 1096 return true; | 1101 return true; |
| 1097 } | 1102 } |
| 1098 | 1103 |
| 1099 bool FormWithElementIsAutofilled(const WebInputElement& element) { | 1104 bool FormWithElementIsAutofilled(const WebInputElement& element) { |
| 1100 WebFormElement form_element = element.form(); | 1105 WebFormElement form_element = element.form(); |
| 1101 if (form_element.isNull()) | 1106 if (form_element.isNull()) |
| 1102 return false; | 1107 return false; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1169 | 1174 |
| 1170 gfx::RectF GetScaledBoundingBox(float scale, WebFormControlElement* element) { | 1175 gfx::RectF GetScaledBoundingBox(float scale, WebFormControlElement* element) { |
| 1171 gfx::Rect bounding_box(element->boundsInViewportSpace()); | 1176 gfx::Rect bounding_box(element->boundsInViewportSpace()); |
| 1172 return gfx::RectF(bounding_box.x() * scale, | 1177 return gfx::RectF(bounding_box.x() * scale, |
| 1173 bounding_box.y() * scale, | 1178 bounding_box.y() * scale, |
| 1174 bounding_box.width() * scale, | 1179 bounding_box.width() * scale, |
| 1175 bounding_box.height() * scale); | 1180 bounding_box.height() * scale); |
| 1176 } | 1181 } |
| 1177 | 1182 |
| 1178 } // namespace autofill | 1183 } // namespace autofill |
| OLD | NEW |