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)) { |
580 // If the maxlength attribute contains a negative value, maxLength() | |
581 // returns the default maxlength value. | |
582 input_element->setSuggestedValue( | |
583 data.value.substr(0, input_element->maxLength())); | |
584 input_element->setAutofilled(true); | |
585 if (is_initiating_node) { | |
586 // Select the part of the text that the user didn't type. | |
587 input_element->setSelectionRange(input_element->value().length(), | |
588 input_element->suggestedValue().length()); | |
589 } | |
Ilya Sherman
2013/12/29 03:36:02
Can this be shared across the two branches?
ziran.sun
2014/01/02 15:22:54
Do you mean merge "if(IsTextInput(input_element))"
Ilya Sherman
2014/01/07 01:09:38
It seems like the "if (is_initiating_node)" code o
ziran.sun
2014/01/07 16:40:41
I am not sure if we need to do "setSelectionRange"
| |
590 } else if (IsTextAreaElement(*field)) { | |
591 WebTextAreaElement textarea = field->to<WebTextAreaElement>(); | |
592 textarea.setSuggestedValue(data.value); | |
593 field->setAutofilled(true); | |
594 } else if (IsSelectElement(*field)) { | |
595 //Handle selectElement here | |
596 } else { | |
579 return; | 597 return; |
580 | |
581 // If the maxlength attribute contains a negative value, maxLength() | |
582 // returns the default maxlength value. | |
583 input_element->setSuggestedValue( | |
584 data.value.substr(0, input_element->maxLength())); | |
585 input_element->setAutofilled(true); | |
586 if (is_initiating_node) { | |
587 // Select the part of the text that the user didn't type. | |
588 input_element->setSelectionRange(input_element->value().length(), | |
589 input_element->suggestedValue().length()); | |
590 } | 598 } |
Ilya Sherman
2013/12/29 03:36:02
nit: No need for the final "else if" nor for the "
ziran.sun
2014/01/07 16:40:41
Done.
| |
591 } | 599 } |
592 | 600 |
593 std::string RetrievalMethodToString( | 601 std::string RetrievalMethodToString( |
594 const WebElementDescriptor::RetrievalMethod& method) { | 602 const WebElementDescriptor::RetrievalMethod& method) { |
595 switch (method) { | 603 switch (method) { |
596 case WebElementDescriptor::CSS_SELECTOR: | 604 case WebElementDescriptor::CSS_SELECTOR: |
597 return "CSS_SELECTOR"; | 605 return "CSS_SELECTOR"; |
598 case WebElementDescriptor::ID: | 606 case WebElementDescriptor::ID: |
599 return "ID"; | 607 return "ID"; |
600 case WebElementDescriptor::NONE: | 608 case WebElementDescriptor::NONE: |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
959 for (ScopedVector<FormFieldData>::const_iterator iter = form_fields.begin(); | 967 for (ScopedVector<FormFieldData>::const_iterator iter = form_fields.begin(); |
960 iter != form_fields.end(); ++iter) { | 968 iter != form_fields.end(); ++iter) { |
961 form->fields.push_back(**iter); | 969 form->fields.push_back(**iter); |
962 } | 970 } |
963 | 971 |
964 return true; | 972 return true; |
965 } | 973 } |
966 | 974 |
967 bool FindFormAndFieldForInputElement(const WebInputElement& element, | 975 bool FindFormAndFieldForInputElement(const WebInputElement& element, |
968 FormData* form, | 976 FormData* form, |
969 FormFieldData* field, | 977 FormFieldData* field, |
Ilya Sherman
2013/12/29 03:36:02
nit: Spurious diff, please revert.
ziran.sun
2014/01/07 16:40:41
Done.
| |
970 RequirementsMask requirements) { | 978 RequirementsMask requirements) { |
971 if (!IsAutofillableElement(element)) | 979 if (!IsAutofillableElement(element)) |
972 return false; | 980 return false; |
973 | 981 |
974 const WebFormElement form_element = element.form(); | 982 const WebFormElement form_element = element.form(); |
975 if (form_element.isNull()) | 983 if (form_element.isNull()) |
976 return false; | 984 return false; |
977 | 985 |
978 ExtractMask extract_mask = | 986 ExtractMask extract_mask = |
979 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS); | 987 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1042 bool ClearPreviewedFormWithElement(const WebInputElement& element, | 1050 bool ClearPreviewedFormWithElement(const WebInputElement& element, |
1043 bool was_autofilled) { | 1051 bool was_autofilled) { |
1044 WebFormElement form_element = element.form(); | 1052 WebFormElement form_element = element.form(); |
1045 if (form_element.isNull()) | 1053 if (form_element.isNull()) |
1046 return false; | 1054 return false; |
1047 | 1055 |
1048 std::vector<WebFormControlElement> control_elements; | 1056 std::vector<WebFormControlElement> control_elements; |
1049 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, | 1057 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, |
1050 &control_elements); | 1058 &control_elements); |
1051 for (size_t i = 0; i < control_elements.size(); ++i) { | 1059 for (size_t i = 0; i < control_elements.size(); ++i) { |
1052 // Only text input elements can be previewed. | 1060 // text input and textarea elements can be previewed. |
Ilya Sherman
2013/12/29 03:36:02
nit: Please keep the word "only"
ziran.sun
2014/01/07 16:40:41
Done.
| |
1053 WebInputElement* input_element = toWebInputElement(&control_elements[i]); | 1061 WebInputElement* input_element = toWebInputElement(&control_elements[i]); |
1054 if (!IsTextInput(input_element)) | 1062 if (!IsTextInput(input_element) && !IsTextAreaElement(control_elements[i])) |
1055 continue; | 1063 continue; |
1056 | 1064 |
1057 // If the input element is not auto-filled, we did not preview it, so there | 1065 // If the control element is not auto-filled, we did not preview it, |
Ilya Sherman
2013/12/29 03:36:02
nit: I'd omit "control", i.e. simply "If the eleme
ziran.sun
2014/01/07 16:40:41
Done.
| |
1058 // is nothing to reset. | 1066 // so there is nothing to reset. |
1059 if (!input_element->isAutofilled()) | 1067 if(!control_elements[i].isAutofilled()) |
1060 continue; | 1068 continue; |
1061 | 1069 |
1062 // There might be unrelated elements in this form which have already been | 1070 // 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 | 1071 // 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 | 1072 // 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. | 1073 // want to reset the auto-filled status for fields that were previewed. |
1066 if (input_element->suggestedValue().isEmpty()) | 1074 if ((IsTextInput(input_element) && |
1075 input_element->suggestedValue().isEmpty()) | |
1076 || (IsTextAreaElement(control_elements[i]) && | |
1077 control_elements[i].to<WebTextAreaElement>().suggestedValue().isEmpty())) | |
Ilya Sherman
2013/12/29 03:36:02
nit: The text wrapping here doesn't match the Chro
ziran.sun
2014/01/07 16:40:41
Done.
| |
1067 continue; | 1078 continue; |
1068 | 1079 |
1069 // Clear the suggested value. For the initiating node, also restore the | 1080 // Clear the suggested value. For the initiating node, also restore the |
1070 // original value. | 1081 // original value. |
1071 input_element->setSuggestedValue(WebString()); | 1082 if (IsTextInput(input_element)) { |
1072 bool is_initiating_node = (element == *input_element); | 1083 input_element->setSuggestedValue(WebString()); |
1073 if (is_initiating_node) | 1084 bool is_initiating_node = (element == *input_element); |
1074 input_element->setAutofilled(was_autofilled); | 1085 if (is_initiating_node) |
1075 else | 1086 input_element->setAutofilled(was_autofilled); |
1076 input_element->setAutofilled(false); | 1087 else |
1088 input_element->setAutofilled(false); | |
Ilya Sherman
2013/12/29 03:36:02
Can this code be shared between the two branches?
| |
1077 | 1089 |
1078 // Clearing the suggested value in the focused node (above) can cause | 1090 // Clearing the suggested value in the focused node (above) can cause |
1079 // selection to be lost. We force selection range to restore the text | 1091 // selection to be lost. We force selection range to restore the text |
1080 // cursor. | 1092 // cursor. |
1081 if (is_initiating_node) { | 1093 if (is_initiating_node) { |
1082 int length = input_element->value().length(); | 1094 int length = input_element->value().length(); |
1083 input_element->setSelectionRange(length, length); | 1095 input_element->setSelectionRange(length, length); |
1096 } | |
1097 } else if (IsTextAreaElement(control_elements[i])) { | |
1098 WebTextAreaElement txtarea = control_elements[i].to<WebTextAreaElement>(); | |
Ilya Sherman
2013/12/29 03:36:02
nit: Please avoid abbreviations in names. The way
ziran.sun
2014/01/07 16:40:41
Done.
| |
1099 txtarea.setSuggestedValue(WebString()); | |
1100 bool is_initiating_node = (element == txtarea); | |
1101 if (is_initiating_node) | |
1102 control_elements[i].setAutofilled(was_autofilled); | |
1103 else | |
1104 control_elements[i].setAutofilled(false); | |
1084 } | 1105 } |
1085 } | 1106 } |
1086 | 1107 |
1087 return true; | 1108 return true; |
1088 } | 1109 } |
1089 | 1110 |
1090 bool FormWithElementIsAutofilled(const WebInputElement& element) { | 1111 bool FormWithElementIsAutofilled(const WebInputElement& element) { |
1091 WebFormElement form_element = element.form(); | 1112 WebFormElement form_element = element.form(); |
1092 if (form_element.isNull()) | 1113 if (form_element.isNull()) |
1093 return false; | 1114 return false; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1151 break; | 1172 break; |
1152 } | 1173 } |
1153 } | 1174 } |
1154 if (!tag_is_allowed) | 1175 if (!tag_is_allowed) |
1155 return false; | 1176 return false; |
1156 } | 1177 } |
1157 return true; | 1178 return true; |
1158 } | 1179 } |
1159 | 1180 |
1160 } // namespace autofill | 1181 } // namespace autofill |
OLD | NEW |