Chromium Code Reviews| 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 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 565 | 565 |
| 566 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|. | 566 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|. |
| 567 // Also sets the "autofilled" attribute, causing the background to be yellow. | 567 // Also sets the "autofilled" attribute, causing the background to be yellow. |
| 568 void PreviewFormField(const FormFieldData& data, | 568 void PreviewFormField(const FormFieldData& data, |
| 569 bool is_initiating_node, | 569 bool is_initiating_node, |
| 570 blink::WebFormControlElement* field) { | 570 blink::WebFormControlElement* field) { |
| 571 // Nothing to preview. | 571 // Nothing to preview. |
| 572 if (data.value.empty()) | 572 if (data.value.empty()) |
| 573 return; | 573 return; |
| 574 | 574 |
| 575 // Only preview input fields. Excludes checkboxes and radio buttons, as there | 575 // Preview input and textarea fields. For input fields, excludes checkboxes |
| 576 // is no provision for setSuggestedCheckedValue in WebInputElement. | 576 // and radio buttons, as there is no provision for setSuggestedCheckedValue |
| 577 // in WebInputElement. | |
| 577 WebInputElement* input_element = toWebInputElement(field); | 578 WebInputElement* input_element = toWebInputElement(field); |
| 578 if (!IsTextInput(input_element)) | 579 if (IsTextInput(input_element)) { |
| 579 return; | 580 // If the maxlength attribute contains a negative value, maxLength() |
| 580 | 581 // returns the default maxlength value. |
| 581 // If the maxlength attribute contains a negative value, maxLength() | 582 input_element->setSuggestedValue( |
| 582 // returns the default maxlength value. | |
| 583 input_element->setSuggestedValue( | |
| 584 data.value.substr(0, input_element->maxLength())); | 583 data.value.substr(0, input_element->maxLength())); |
| 585 input_element->setAutofilled(true); | 584 input_element->setAutofilled(true); |
|
Ilya Sherman
2014/01/07 01:09:38
nit: Please de-indent this line two spaces.
ziran.sun
2014/01/07 16:40:41
Done.
| |
| 586 if (is_initiating_node) { | 585 if (is_initiating_node) { |
| 587 // Select the part of the text that the user didn't type. | 586 // Select the part of the text that the user didn't type. |
| 588 input_element->setSelectionRange(input_element->value().length(), | 587 input_element->setSelectionRange(input_element->value().length(), |
| 589 input_element->suggestedValue().length()); | 588 input_element->suggestedValue().length()); |
|
Ilya Sherman
2014/01/07 01:09:38
nit: Indentation is off here. Please format this
ziran.sun
2014/01/07 16:40:41
Done.
| |
| 589 } | |
| 590 } else if (IsTextAreaElement(*field)) { | |
| 591 WebTextAreaElement textarea = field->to<WebTextAreaElement>(); | |
| 592 textarea.setSuggestedValue(data.value); | |
| 593 field->setAutofilled(true); | |
| 590 } | 594 } |
| 591 } | 595 } |
| 592 | 596 |
| 593 std::string RetrievalMethodToString( | 597 std::string RetrievalMethodToString( |
| 594 const WebElementDescriptor::RetrievalMethod& method) { | 598 const WebElementDescriptor::RetrievalMethod& method) { |
| 595 switch (method) { | 599 switch (method) { |
| 596 case WebElementDescriptor::CSS_SELECTOR: | 600 case WebElementDescriptor::CSS_SELECTOR: |
| 597 return "CSS_SELECTOR"; | 601 return "CSS_SELECTOR"; |
| 598 case WebElementDescriptor::ID: | 602 case WebElementDescriptor::ID: |
| 599 return "ID"; | 603 return "ID"; |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1042 bool ClearPreviewedFormWithElement(const WebInputElement& element, | 1046 bool ClearPreviewedFormWithElement(const WebInputElement& element, |
| 1043 bool was_autofilled) { | 1047 bool was_autofilled) { |
| 1044 WebFormElement form_element = element.form(); | 1048 WebFormElement form_element = element.form(); |
| 1045 if (form_element.isNull()) | 1049 if (form_element.isNull()) |
| 1046 return false; | 1050 return false; |
| 1047 | 1051 |
| 1048 std::vector<WebFormControlElement> control_elements; | 1052 std::vector<WebFormControlElement> control_elements; |
| 1049 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, | 1053 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, |
| 1050 &control_elements); | 1054 &control_elements); |
| 1051 for (size_t i = 0; i < control_elements.size(); ++i) { | 1055 for (size_t i = 0; i < control_elements.size(); ++i) { |
| 1052 // Only text input elements can be previewed. | 1056 // Only text input and textarea elements can be previewed. |
| 1053 WebInputElement* input_element = toWebInputElement(&control_elements[i]); | 1057 WebInputElement* input_element = toWebInputElement(&control_elements[i]); |
| 1054 if (!IsTextInput(input_element)) | 1058 if (!IsTextInput(input_element) && !IsTextAreaElement(control_elements[i])) |
| 1055 continue; | 1059 continue; |
| 1056 | 1060 |
| 1057 // If the input element is not auto-filled, we did not preview it, so there | 1061 // If the element is not auto-filled, we did not preview it, |
| 1058 // is nothing to reset. | 1062 // so there is nothing to reset. |
| 1059 if (!input_element->isAutofilled()) | 1063 if(!control_elements[i].isAutofilled()) |
| 1060 continue; | 1064 continue; |
| 1061 | 1065 |
| 1062 // There might be unrelated elements in this form which have already been | 1066 // There might be unrelated elements in this form which have already been |
| 1063 // auto-filled. For example, the user might have already filled the address | 1067 // auto-filled. For example, the user might have already filled the address |
| 1064 // part of a form and now be dealing with the credit card section. We only | 1068 // part of a form and now be dealing with the credit card section. We only |
| 1065 // want to reset the auto-filled status for fields that were previewed. | 1069 // want to reset the auto-filled status for fields that were previewed. |
| 1066 if (input_element->suggestedValue().isEmpty()) | 1070 WebFormControlElement control_element = control_elements[i]; |
|
Ilya Sherman
2014/01/07 01:09:38
nit: I'd recommend moving this to the top of the l
ziran.sun
2014/01/07 16:40:41
Done.
| |
| 1071 if ((IsTextInput(input_element) && | |
| 1072 input_element->suggestedValue().isEmpty()) | |
| 1073 || (IsTextAreaElement(control_element) && | |
| 1074 control_element.to<WebTextAreaElement>().suggestedValue().isEmpty())) | |
|
Ilya Sherman
2014/01/07 01:09:38
This is still not wrapped correctly. Specifically
ziran.sun
2014/01/07 16:40:41
Done.
| |
| 1067 continue; | 1075 continue; |
| 1068 | 1076 |
| 1069 // Clear the suggested value. For the initiating node, also restore the | 1077 // Clear the suggested value. For the initiating node, also restore the |
| 1070 // original value. | 1078 // original value. |
| 1071 input_element->setSuggestedValue(WebString()); | 1079 if (IsTextInput(input_element)) { |
| 1072 bool is_initiating_node = (element == *input_element); | 1080 input_element->setSuggestedValue(WebString()); |
| 1073 if (is_initiating_node) | 1081 bool is_initiating_node = (element == *input_element); |
| 1074 input_element->setAutofilled(was_autofilled); | 1082 if (is_initiating_node) |
| 1075 else | 1083 input_element->setAutofilled(was_autofilled); |
| 1076 input_element->setAutofilled(false); | 1084 else |
| 1085 input_element->setAutofilled(false); | |
|
Ilya Sherman
2014/01/07 01:09:38
Lines 1081-1085 are essentially duplicated on line
ziran.sun
2014/01/07 16:40:41
This could look ugly. Check other functions, e.g.
Ilya Sherman
2014/01/08 04:13:47
Alright, I guess it's ok as is. Thanks.
| |
| 1077 | 1086 |
| 1078 // Clearing the suggested value in the focused node (above) can cause | 1087 // Clearing the suggested value in the focused node (above) can cause |
| 1079 // 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 |
| 1080 // cursor. | 1089 // cursor. |
| 1081 if (is_initiating_node) { | 1090 if (is_initiating_node) { |
| 1082 int length = input_element->value().length(); | 1091 int length = input_element->value().length(); |
| 1083 input_element->setSelectionRange(length, length); | 1092 input_element->setSelectionRange(length, length); |
| 1093 } | |
| 1094 } else if (IsTextAreaElement(control_element)) { | |
| 1095 WebTextAreaElement text_area = control_element.to<WebTextAreaElement>(); | |
| 1096 text_area.setSuggestedValue(WebString()); | |
| 1097 bool is_initiating_node = (element == text_area); | |
| 1098 if (is_initiating_node) | |
| 1099 control_element.setAutofilled(was_autofilled); | |
| 1100 else | |
| 1101 control_element.setAutofilled(false); | |
| 1084 } | 1102 } |
| 1085 } | 1103 } |
| 1086 | 1104 |
| 1087 return true; | 1105 return true; |
| 1088 } | 1106 } |
| 1089 | 1107 |
| 1090 bool FormWithElementIsAutofilled(const WebInputElement& element) { | 1108 bool FormWithElementIsAutofilled(const WebInputElement& element) { |
| 1091 WebFormElement form_element = element.form(); | 1109 WebFormElement form_element = element.form(); |
| 1092 if (form_element.isNull()) | 1110 if (form_element.isNull()) |
| 1093 return false; | 1111 return false; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1151 break; | 1169 break; |
| 1152 } | 1170 } |
| 1153 } | 1171 } |
| 1154 if (!tag_is_allowed) | 1172 if (!tag_is_allowed) |
| 1155 return false; | 1173 return false; |
| 1156 } | 1174 } |
| 1157 return true; | 1175 return true; |
| 1158 } | 1176 } |
| 1159 | 1177 |
| 1160 } // namespace autofill | 1178 } // namespace autofill |
| OLD | NEW |