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

Unified Diff: components/autofill/core/browser/autofill_field.cc

Issue 361833004: autofill: be more open minded about filling in states in <option>s. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: base:: Created 6 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | components/autofill/core/browser/autofill_field_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/autofill/core/browser/autofill_field.cc
diff --git a/components/autofill/core/browser/autofill_field.cc b/components/autofill/core/browser/autofill_field.cc
index bad3b5b680c23c43149930ab8782e3cc08793dd6..3fec668f3c76a5ac2fafe459bbaf27fb767da343 100644
--- a/components/autofill/core/browser/autofill_field.cc
+++ b/components/autofill/core/browser/autofill_field.cc
@@ -66,6 +66,65 @@ bool SetSelectControlValue(const base::string16& value,
return true;
}
+// Like SetSelectControlValue, but searches within the field values and options
+// for |value|. For example, "NC - North Carolina" would match "north carolina".
+bool SetSelectControlValueSubstringMatch(const base::string16& value,
+ FormFieldData* field) {
+ base::string16 value_lowercase = StringToLowerASCII(value);
+ DCHECK_EQ(field->option_values.size(), field->option_contents.size());
+ int best_match = -1;
+
+ for (size_t i = 0; i < field->option_values.size(); ++i) {
+ if (StringToLowerASCII(field->option_values[i]).find(value_lowercase) !=
+ std::string::npos ||
+ StringToLowerASCII(field->option_contents[i]).find(value_lowercase) !=
+ std::string::npos) {
+ // The best match is the shortest one.
+ if (best_match == -1 ||
+ field->option_values[best_match].size() >
+ field->option_values[i].size()) {
+ best_match = i;
+ }
+ }
+ }
+
+ if (best_match >= 0) {
+ field->value = field->option_values[best_match];
+ return true;
+ }
+
+ return false;
+}
+
+// Like SetSelectControlValue, but searches within the field values and options
+// for |value|. First it tokenizes the options, then tries to match against
+// tokens. For example, "NC - North Carolina" would match "nc" but not "ca".
+bool SetSelectControlValueTokenMatch(const base::string16& value,
+ FormFieldData* field) {
+ base::string16 value_lowercase = StringToLowerASCII(value);
+ std::vector<base::string16> tokenized;
+ DCHECK_EQ(field->option_values.size(), field->option_contents.size());
+
+ for (size_t i = 0; i < field->option_values.size(); ++i) {
+ base::SplitStringAlongWhitespace(
+ StringToLowerASCII(field->option_values[i]), &tokenized);
+ if (std::find(tokenized.begin(), tokenized.end(), value_lowercase) !=
+ tokenized.end()) {
+ field->value = field->option_values[i];
+ return true;
+ }
+
+ base::SplitStringAlongWhitespace(
+ StringToLowerASCII(field->option_contents[i]), &tokenized);
+ if (std::find(tokenized.begin(), tokenized.end(), value_lowercase) !=
+ tokenized.end()) {
+ field->value = field->option_values[i];
+ return true;
+ }
+ }
+
+ return false;
+}
// Try to fill a numeric |value| into the given |field|.
bool FillNumericSelectControl(int value,
@@ -88,11 +147,24 @@ bool FillStateSelectControl(const base::string16& value,
base::string16 full, abbreviation;
state_names::GetNameAndAbbreviation(value, &full, &abbreviation);
- // Try the abbreviation first.
- if (!abbreviation.empty() && SetSelectControlValue(abbreviation, field))
+ // Try an exact match of the abbreviation first.
+ if (!abbreviation.empty() && SetSelectControlValue(abbreviation, field)) {
+ return true;
+ }
+
+ // Try an exact match of the full name.
+ if (!full.empty() && SetSelectControlValue(full, field)) {
return true;
+ }
+
+ // Then try an inexact match of the full name.
+ if (!full.empty() && SetSelectControlValueSubstringMatch(full, field)) {
+ return true;
+ }
- return !full.empty() && SetSelectControlValue(full, field);
+ // Then try an inexact match of the abbreviation name.
+ return !abbreviation.empty() &&
+ SetSelectControlValueTokenMatch(abbreviation, field);
}
bool FillCountrySelectControl(const base::string16& value,
« no previous file with comments | « no previous file | components/autofill/core/browser/autofill_field_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698