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); |
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( |
589 input_element->suggestedValue().length()); | 588 input_element->value().length(), |
| 589 input_element->suggestedValue().length()); |
| 590 } |
| 591 } else if (IsTextAreaElement(*field)) { |
| 592 WebTextAreaElement textarea = field->to<WebTextAreaElement>(); |
| 593 textarea.setSuggestedValue(data.value); |
| 594 field->setAutofilled(true); |
590 } | 595 } |
591 } | 596 } |
592 | 597 |
593 std::string RetrievalMethodToString( | 598 std::string RetrievalMethodToString( |
594 const WebElementDescriptor::RetrievalMethod& method) { | 599 const WebElementDescriptor::RetrievalMethod& method) { |
595 switch (method) { | 600 switch (method) { |
596 case WebElementDescriptor::CSS_SELECTOR: | 601 case WebElementDescriptor::CSS_SELECTOR: |
597 return "CSS_SELECTOR"; | 602 return "CSS_SELECTOR"; |
598 case WebElementDescriptor::ID: | 603 case WebElementDescriptor::ID: |
599 return "ID"; | 604 return "ID"; |
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1042 bool ClearPreviewedFormWithElement(const WebInputElement& element, | 1047 bool ClearPreviewedFormWithElement(const WebInputElement& element, |
1043 bool was_autofilled) { | 1048 bool was_autofilled) { |
1044 WebFormElement form_element = element.form(); | 1049 WebFormElement form_element = element.form(); |
1045 if (form_element.isNull()) | 1050 if (form_element.isNull()) |
1046 return false; | 1051 return false; |
1047 | 1052 |
1048 std::vector<WebFormControlElement> control_elements; | 1053 std::vector<WebFormControlElement> control_elements; |
1049 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, | 1054 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, |
1050 &control_elements); | 1055 &control_elements); |
1051 for (size_t i = 0; i < control_elements.size(); ++i) { | 1056 for (size_t i = 0; i < control_elements.size(); ++i) { |
1052 // Only text input elements can be previewed. | |
1053 WebInputElement* input_element = toWebInputElement(&control_elements[i]); | |
1054 if (!IsTextInput(input_element)) | |
1055 continue; | |
1056 | |
1057 // If the input element is not auto-filled, we did not preview it, so there | |
1058 // is nothing to reset. | |
1059 if (!input_element->isAutofilled()) | |
1060 continue; | |
1061 | |
1062 // There might be unrelated elements in this form which have already been | 1057 // 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 | 1058 // 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 | 1059 // 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. | 1060 // want to reset the auto-filled status for fields that were previewed. |
1066 if (input_element->suggestedValue().isEmpty()) | 1061 WebFormControlElement control_element = control_elements[i]; |
| 1062 |
| 1063 // Only text input and textarea elements can be previewed. |
| 1064 WebInputElement* input_element = toWebInputElement(&control_element); |
| 1065 if (!IsTextInput(input_element) && !IsTextAreaElement(control_element)) |
| 1066 continue; |
| 1067 |
| 1068 // If the element is not auto-filled, we did not preview it, |
| 1069 // so there is nothing to reset. |
| 1070 if(!control_element.isAutofilled()) |
| 1071 continue; |
| 1072 |
| 1073 if ((IsTextInput(input_element) && |
| 1074 input_element->suggestedValue().isEmpty()) || |
| 1075 (IsTextAreaElement(control_element) && |
| 1076 control_element.to<WebTextAreaElement>().suggestedValue().isEmpty())) |
1067 continue; | 1077 continue; |
1068 | 1078 |
1069 // Clear the suggested value. For the initiating node, also restore the | 1079 // Clear the suggested value. For the initiating node, also restore the |
1070 // original value. | 1080 // original value. |
1071 input_element->setSuggestedValue(WebString()); | 1081 if (IsTextInput(input_element)) { |
1072 bool is_initiating_node = (element == *input_element); | 1082 input_element->setSuggestedValue(WebString()); |
1073 if (is_initiating_node) | 1083 bool is_initiating_node = (element == *input_element); |
1074 input_element->setAutofilled(was_autofilled); | 1084 if (is_initiating_node) |
1075 else | 1085 input_element->setAutofilled(was_autofilled); |
1076 input_element->setAutofilled(false); | 1086 else |
| 1087 input_element->setAutofilled(false); |
1077 | 1088 |
1078 // Clearing the suggested value in the focused node (above) can cause | 1089 // Clearing the suggested value in the focused node (above) can cause |
1079 // selection to be lost. We force selection range to restore the text | 1090 // selection to be lost. We force selection range to restore the text |
1080 // cursor. | 1091 // cursor. |
1081 if (is_initiating_node) { | 1092 if (is_initiating_node) { |
1082 int length = input_element->value().length(); | 1093 int length = input_element->value().length(); |
1083 input_element->setSelectionRange(length, length); | 1094 input_element->setSelectionRange(length, length); |
| 1095 } |
| 1096 } else if (IsTextAreaElement(control_element)) { |
| 1097 WebTextAreaElement text_area = control_element.to<WebTextAreaElement>(); |
| 1098 text_area.setSuggestedValue(WebString()); |
| 1099 bool is_initiating_node = (element == text_area); |
| 1100 if (is_initiating_node) |
| 1101 control_element.setAutofilled(was_autofilled); |
| 1102 else |
| 1103 control_element.setAutofilled(false); |
1084 } | 1104 } |
1085 } | 1105 } |
1086 | 1106 |
1087 return true; | 1107 return true; |
1088 } | 1108 } |
1089 | 1109 |
1090 bool FormWithElementIsAutofilled(const WebInputElement& element) { | 1110 bool FormWithElementIsAutofilled(const WebInputElement& element) { |
1091 WebFormElement form_element = element.form(); | 1111 WebFormElement form_element = element.form(); |
1092 if (form_element.isNull()) | 1112 if (form_element.isNull()) |
1093 return false; | 1113 return false; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1151 break; | 1171 break; |
1152 } | 1172 } |
1153 } | 1173 } |
1154 if (!tag_is_allowed) | 1174 if (!tag_is_allowed) |
1155 return false; | 1175 return false; |
1156 } | 1176 } |
1157 return true; | 1177 return true; |
1158 } | 1178 } |
1159 | 1179 |
1160 } // namespace autofill | 1180 } // namespace autofill |
OLD | NEW |