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

Side by Side Diff: components/autofill/content/renderer/form_autofill_util.cc

Issue 136793007: Add Autofill preview support for <select> input fields (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 11 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 unified diff | Download patch
OLDNEW
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 552 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 563
564 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|. 564 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|.
565 // Also sets the "autofilled" attribute, causing the background to be yellow. 565 // Also sets the "autofilled" attribute, causing the background to be yellow.
566 void PreviewFormField(const FormFieldData& data, 566 void PreviewFormField(const FormFieldData& data,
567 bool is_initiating_node, 567 bool is_initiating_node,
568 blink::WebFormControlElement* field) { 568 blink::WebFormControlElement* field) {
569 // Nothing to preview. 569 // Nothing to preview.
570 if (data.value.empty()) 570 if (data.value.empty())
571 return; 571 return;
572 572
573 // Preview input and textarea fields. For input fields, excludes checkboxes 573 // Preview input, textarea and select fields. For input fields, excludes
574 // and radio buttons, as there is no provision for setSuggestedCheckedValue 574 // checkboxes and radio buttons, as there is no provision for
575 // in WebInputElement. 575 // setSuggestedCheckedValue in WebInputElement.
576 WebInputElement* input_element = toWebInputElement(field); 576 WebInputElement* input_element = toWebInputElement(field);
577 if (IsTextInput(input_element)) { 577 if (IsTextInput(input_element)) {
578 // If the maxlength attribute contains a negative value, maxLength() 578 // If the maxlength attribute contains a negative value, maxLength()
579 // returns the default maxlength value. 579 // returns the default maxlength value.
580 input_element->setSuggestedValue( 580 input_element->setSuggestedValue(
581 data.value.substr(0, input_element->maxLength())); 581 data.value.substr(0, input_element->maxLength()));
582 input_element->setAutofilled(true); 582 input_element->setAutofilled(true);
583 if (is_initiating_node) { 583 if (is_initiating_node) {
584 // Select the part of the text that the user didn't type. 584 // Select the part of the text that the user didn't type.
585 input_element->setSelectionRange( 585 input_element->setSelectionRange(
586 input_element->value().length(), 586 input_element->value().length(),
587 input_element->suggestedValue().length()); 587 input_element->suggestedValue().length());
588 } 588 }
589 } else if (IsTextAreaElement(*field)) { 589 } else if (IsTextAreaElement(*field)) {
590 WebTextAreaElement textarea = field->to<WebTextAreaElement>(); 590 WebTextAreaElement textarea = field->to<WebTextAreaElement>();
591 textarea.setSuggestedValue(data.value); 591 textarea.setSuggestedValue(data.value);
592 field->setAutofilled(true); 592 field->setAutofilled(true);
593 } else if (IsSelectElement(*field)) {
594 WebSelectElement select_element = field->to<WebSelectElement>();
595 // Check the original selected option value
596 select_element.setOriginalValue(select_element.value());
Ilya Sherman 2014/02/08 01:40:41 This should not be necessary; the select element s
597 select_element.setSuggestedValue(data.value);
598 field->setAutofilled(true);
593 } 599 }
594 } 600 }
595 601
596 std::string RetrievalMethodToString( 602 std::string RetrievalMethodToString(
597 const WebElementDescriptor::RetrievalMethod& method) { 603 const WebElementDescriptor::RetrievalMethod& method) {
598 switch (method) { 604 switch (method) {
599 case WebElementDescriptor::CSS_SELECTOR: 605 case WebElementDescriptor::CSS_SELECTOR:
600 return "CSS_SELECTOR"; 606 return "CSS_SELECTOR";
601 case WebElementDescriptor::ID: 607 case WebElementDescriptor::ID:
602 return "ID"; 608 return "ID";
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
1051 std::vector<WebFormControlElement> control_elements; 1057 std::vector<WebFormControlElement> control_elements;
1052 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, 1058 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE,
1053 &control_elements); 1059 &control_elements);
1054 for (size_t i = 0; i < control_elements.size(); ++i) { 1060 for (size_t i = 0; i < control_elements.size(); ++i) {
1055 // There might be unrelated elements in this form which have already been 1061 // There might be unrelated elements in this form which have already been
1056 // auto-filled. For example, the user might have already filled the address 1062 // auto-filled. For example, the user might have already filled the address
1057 // part of a form and now be dealing with the credit card section. We only 1063 // part of a form and now be dealing with the credit card section. We only
1058 // want to reset the auto-filled status for fields that were previewed. 1064 // want to reset the auto-filled status for fields that were previewed.
1059 WebFormControlElement control_element = control_elements[i]; 1065 WebFormControlElement control_element = control_elements[i];
1060 1066
1061 // Only text input and textarea elements can be previewed. 1067 // Only text input, textarea and Select elements can be previewed.
Ilya Sherman 2014/02/08 01:40:41 nit: "Select" -> "select"
1062 WebInputElement* input_element = toWebInputElement(&control_element); 1068 WebInputElement* input_element = toWebInputElement(&control_element);
1063 if (!IsTextInput(input_element) && !IsTextAreaElement(control_element)) 1069 if (!IsTextInput(input_element) &&
1070 !IsTextAreaElement(control_element) &&
1071 !IsSelectElement(control_element))
1064 continue; 1072 continue;
1065 1073
1066 // If the element is not auto-filled, we did not preview it, 1074 // If the element is not auto-filled, we did not preview it,
1067 // so there is nothing to reset. 1075 // so there is nothing to reset.
1068 if(!control_element.isAutofilled()) 1076 if(!control_element.isAutofilled())
1069 continue; 1077 continue;
1070 1078
1071 if ((IsTextInput(input_element) && 1079 if ((IsTextInput(input_element) &&
1072 input_element->suggestedValue().isEmpty()) || 1080 input_element->suggestedValue().isEmpty()) ||
1073 (IsTextAreaElement(control_element) && 1081 (IsTextAreaElement(control_element) &&
1074 control_element.to<WebTextAreaElement>().suggestedValue().isEmpty())) 1082 control_element.to<WebTextAreaElement>().suggestedValue().isEmpty()) ||
1083 (IsSelectElement(control_element) &&
1084 control_element.to<WebSelectElement>().suggestedValue().isEmpty()))
1075 continue; 1085 continue;
1076 1086
1077 // Clear the suggested value. For the initiating node, also restore the 1087 // Clear the suggested value. For the initiating node, also restore the
1078 // original value. 1088 // original value.
1079 if (IsTextInput(input_element)) { 1089 if (IsTextInput(input_element)) {
1080 input_element->setSuggestedValue(WebString()); 1090 input_element->setSuggestedValue(WebString());
1081 bool is_initiating_node = (element == *input_element); 1091 bool is_initiating_node = (element == *input_element);
1082 if (is_initiating_node) 1092 if (is_initiating_node)
1083 input_element->setAutofilled(was_autofilled); 1093 input_element->setAutofilled(was_autofilled);
1084 else 1094 else
1085 input_element->setAutofilled(false); 1095 input_element->setAutofilled(false);
1086 1096
1087 // Clearing the suggested value in the focused node (above) can cause 1097 // Clearing the suggested value in the focused node (above) can cause
1088 // selection to be lost. We force selection range to restore the text 1098 // selection to be lost. We force selection range to restore the text
1089 // cursor. 1099 // cursor.
1090 if (is_initiating_node) { 1100 if (is_initiating_node) {
1091 int length = input_element->value().length(); 1101 int length = input_element->value().length();
1092 input_element->setSelectionRange(length, length); 1102 input_element->setSelectionRange(length, length);
1093 } 1103 }
1094 } else if (IsTextAreaElement(control_element)) { 1104 } else if (IsTextAreaElement(control_element)) {
1095 WebTextAreaElement text_area = control_element.to<WebTextAreaElement>(); 1105 WebTextAreaElement text_area = control_element.to<WebTextAreaElement>();
1096 text_area.setSuggestedValue(WebString()); 1106 text_area.setSuggestedValue(WebString());
1097 bool is_initiating_node = (element == text_area); 1107 bool is_initiating_node = (element == text_area);
1098 if (is_initiating_node) 1108 if (is_initiating_node)
1099 control_element.setAutofilled(was_autofilled); 1109 control_element.setAutofilled(was_autofilled);
1100 else 1110 else
1101 control_element.setAutofilled(false); 1111 control_element.setAutofilled(false);
1112 } else if (IsSelectElement(control_element)) {
1113 WebSelectElement select_element = control_element.to<WebSelectElement>();
1114 select_element.setSuggestedValue(WebString());
1115 bool is_initiating_node = (element == select_element);
1116 if (is_initiating_node)
1117 control_element.setAutofilled(was_autofilled);
1118 else
1119 control_element.setAutofilled(false);
Ilya Sherman 2014/02/08 01:40:41 There's no need for handling the case where is_ini
1120
1121 // Set selected option back to the original one. Ideally this should
1122 // happen right in the end of PreviewForm() but doing so will overwrite
1123 // the preview value.
1124 select_element.setValue(WebString(select_element.originalValue()));
Ilya Sherman 2014/02/08 01:40:41 This should not be necessary; the <select> element
1102 } 1125 }
1103 } 1126 }
1104 1127
1105 return true; 1128 return true;
1106 } 1129 }
1107 1130
1108 bool FormWithElementIsAutofilled(const WebInputElement& element) { 1131 bool FormWithElementIsAutofilled(const WebInputElement& element) {
1109 WebFormElement form_element = element.form(); 1132 WebFormElement form_element = element.form();
1110 if (form_element.isNull()) 1133 if (form_element.isNull())
1111 return false; 1134 return false;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1169 break; 1192 break;
1170 } 1193 }
1171 } 1194 }
1172 if (!tag_is_allowed) 1195 if (!tag_is_allowed)
1173 return false; 1196 return false;
1174 } 1197 }
1175 return true; 1198 return true;
1176 } 1199 }
1177 1200
1178 } // namespace autofill 1201 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698