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/core/browser/autofill_field.h" | 5 #include "components/autofill/core/browser/autofill_field.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/sha1.h" | 8 #include "base/sha1.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
62 if (best_match.empty()) | 62 if (best_match.empty()) |
63 return false; | 63 return false; |
64 | 64 |
65 field->value = best_match; | 65 field->value = best_match; |
66 return true; | 66 return true; |
67 } | 67 } |
68 | 68 |
69 // Like SetSelectControlValue, but searches within the field values and options | |
70 // for |value|. For example, "NC - North Carolina" would match "north carolina". | |
Ilya Sherman
2014/07/01 00:59:08
Would "WV - West Virginia" match "virginia"? It s
Evan Stade
2014/07/01 01:04:56
hmm, yes I suppose it would. Perhaps I could make
Ilya Sherman
2014/07/01 01:17:47
Yeah, maybe. Alternately, we could just kinda ass
| |
71 bool SetSelectControlValueSubstringMatch(const base::string16& value, | |
72 FormFieldData* field) { | |
73 base::string16 value_lowercase = StringToLowerASCII(value); | |
74 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); | |
75 | |
76 for (size_t i = 0; i < field->option_values.size(); ++i) { | |
77 if (StringToLowerASCII(field->option_values[i]).find(value_lowercase) != | |
78 std::string::npos || | |
79 StringToLowerASCII(field->option_contents[i]).find(value_lowercase) != | |
80 std::string::npos) { | |
81 field->value = field->option_values[i]; | |
82 return true; | |
83 } | |
84 } | |
85 | |
86 return false; | |
87 } | |
88 | |
89 // Like SetSelectControlValue, but searches within the field values and options | |
90 // for |value|. First it tokenizes the options, then tries to match against | |
91 // tokens. For example, "NC - North Carolina" would match "nc" but not "ca". | |
92 bool SetSelectControlValueTokenMatch(const base::string16& value, | |
93 FormFieldData* field) { | |
94 base::string16 value_lowercase = StringToLowerASCII(value); | |
95 std::vector<base::string16> tokenized; | |
96 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); | |
97 | |
98 for (size_t i = 0; i < field->option_values.size(); ++i) { | |
99 SplitStringAlongWhitespace(StringToLowerASCII(field->option_values[i]), | |
100 &tokenized); | |
101 if (std::find(tokenized.begin(), tokenized.end(), value_lowercase) != | |
102 tokenized.end()) { | |
103 field->value = field->option_values[i]; | |
104 return true; | |
105 } | |
106 | |
107 SplitStringAlongWhitespace(StringToLowerASCII(field->option_contents[i]), | |
108 &tokenized); | |
109 if (std::find(tokenized.begin(), tokenized.end(), value_lowercase) != | |
110 tokenized.end()) { | |
111 field->value = field->option_values[i]; | |
112 return true; | |
113 } | |
114 } | |
115 | |
116 return false; | |
117 } | |
69 | 118 |
70 // Try to fill a numeric |value| into the given |field|. | 119 // Try to fill a numeric |value| into the given |field|. |
71 bool FillNumericSelectControl(int value, | 120 bool FillNumericSelectControl(int value, |
72 FormFieldData* field) { | 121 FormFieldData* field) { |
73 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); | 122 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); |
74 for (size_t i = 0; i < field->option_values.size(); ++i) { | 123 for (size_t i = 0; i < field->option_values.size(); ++i) { |
75 int option; | 124 int option; |
76 if ((StringToInt(field->option_values[i], &option) && option == value) || | 125 if ((StringToInt(field->option_values[i], &option) && option == value) || |
77 (StringToInt(field->option_contents[i], &option) && option == value)) { | 126 (StringToInt(field->option_contents[i], &option) && option == value)) { |
78 field->value = field->option_values[i]; | 127 field->value = field->option_values[i]; |
79 return true; | 128 return true; |
80 } | 129 } |
81 } | 130 } |
82 | 131 |
83 return false; | 132 return false; |
84 } | 133 } |
85 | 134 |
86 bool FillStateSelectControl(const base::string16& value, | 135 bool FillStateSelectControl(const base::string16& value, |
87 FormFieldData* field) { | 136 FormFieldData* field) { |
88 base::string16 full, abbreviation; | 137 base::string16 full, abbreviation; |
89 state_names::GetNameAndAbbreviation(value, &full, &abbreviation); | 138 state_names::GetNameAndAbbreviation(value, &full, &abbreviation); |
90 | 139 |
91 // Try the abbreviation first. | 140 // Try an exact match of the abbreviation first. |
92 if (!abbreviation.empty() && SetSelectControlValue(abbreviation, field)) | 141 if (!abbreviation.empty() && SetSelectControlValue(abbreviation, field)) { |
93 return true; | 142 return true; |
143 } | |
Dan Beam
2014/07/01 00:34:32
why so curly?
| |
94 | 144 |
95 return !full.empty() && SetSelectControlValue(full, field); | 145 // Try an exact match of the full name. |
146 if (!full.empty() && SetSelectControlValue(full, field)) { | |
147 return true; | |
148 } | |
149 | |
150 // Then try an inexact match of the full name. | |
151 if (!full.empty() && SetSelectControlValueSubstringMatch(full, field)) { | |
152 return true; | |
153 } | |
154 | |
155 // Then try an inexact match of the abbreviation name. | |
156 return !abbreviation.empty() && | |
157 SetSelectControlValueTokenMatch(abbreviation, field); | |
96 } | 158 } |
97 | 159 |
98 bool FillCountrySelectControl(const base::string16& value, | 160 bool FillCountrySelectControl(const base::string16& value, |
99 const std::string& app_locale, | 161 const std::string& app_locale, |
100 FormFieldData* field_data) { | 162 FormFieldData* field_data) { |
101 std::string country_code = AutofillCountry::GetCountryCode(value, app_locale); | 163 std::string country_code = AutofillCountry::GetCountryCode(value, app_locale); |
102 if (country_code.empty()) | 164 if (country_code.empty()) |
103 return false; | 165 return false; |
104 | 166 |
105 DCHECK_EQ(field_data->option_values.size(), | 167 DCHECK_EQ(field_data->option_values.size(), |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
404 } else if (type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS) { | 466 } else if (type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS) { |
405 FillStreetAddress(value, field_data); | 467 FillStreetAddress(value, field_data); |
406 return true; | 468 return true; |
407 } | 469 } |
408 | 470 |
409 field_data->value = value; | 471 field_data->value = value; |
410 return true; | 472 return true; |
411 } | 473 } |
412 | 474 |
413 } // namespace autofill | 475 } // namespace autofill |
OLD | NEW |