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

Side by Side Diff: chrome/browser/autofill/form_field.cc

Issue 7038003: Refactor autofill form fields. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/autofill/form_field.h" 5 #include "chrome/browser/autofill/form_field.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 scanner.Advance(); 163 scanner.Advance();
164 continue; 164 continue;
165 } 165 }
166 166
167 bool ok = form_field->GetFieldInfo(field_type_map); 167 bool ok = form_field->GetFieldInfo(field_type_map);
168 DCHECK(ok); 168 DCHECK(ok);
169 } 169 }
170 } 170 }
171 171
172 // static 172 // static
173 bool FormField::MatchWithType(const AutofillField* field,
174 const string16& pattern,
175 int match_type) {
176 if ((match_type & MATCH_LABEL) && MatchLabel(field, pattern))
177 return true;
178
179 // For now, we apply the same pattern to the field's label and the field's
180 // name. Matching the name is a bit of a long shot for many patterns, but
181 // it generally doesn't hurt to try.
182 if ((match_type & MATCH_NAME) && MatchName(field, pattern))
183 return true;
184
185 return false;
186 }
187
188 // static
173 bool FormField::Match(const AutofillField* field, 189 bool FormField::Match(const AutofillField* field,
174 const string16& pattern, 190 const string16& pattern,
175 bool match_label_only) { 191 bool match_label_only) {
176 if (MatchLabel(field, pattern)) 192 return Match(field, pattern,
177 return true; 193 MATCH_NAME | (match_label_only ? 0 : MATCH_NAME));
178
179 // For now, we apply the same pattern to the field's label and the field's
180 // name. Matching the name is a bit of a long shot for many patterns, but
181 // it generally doesn't hurt to try.
182 if (!match_label_only && MatchName(field, pattern))
183 return true;
184
185 return false;
186 } 194 }
187 195
188 // static 196 // static
189 bool FormField::MatchName(const AutofillField* field, const string16& pattern) { 197 bool FormField::MatchName(const AutofillField* field, const string16& pattern) {
190 // TODO(jhawkins): Remove StringToLowerASCII. WebRegularExpression needs to 198 // TODO(jhawkins): Remove StringToLowerASCII. WebRegularExpression needs to
191 // be fixed to take WebTextCaseInsensitive into account. 199 // be fixed to take WebTextCaseInsensitive into account.
192 WebKit::WebRegularExpression re(WebKit::WebString(pattern), 200 WebKit::WebRegularExpression re(WebKit::WebString(pattern),
193 WebKit::WebTextCaseInsensitive); 201 WebKit::WebTextCaseInsensitive);
194 bool match = re.match( 202 bool match = re.match(
195 WebKit::WebString(StringToLowerASCII(field->name))) != -1; 203 WebKit::WebString(StringToLowerASCII(field->name))) != -1;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 field = CreditCardField::Parse(scanner, is_ecml); 236 field = CreditCardField::Parse(scanner, is_ecml);
229 if (field) 237 if (field)
230 return field; 238 return field;
231 239
232 // We search for a |NameField| last since it matches the word "name", which is 240 // We search for a |NameField| last since it matches the word "name", which is
233 // relatively general. 241 // relatively general.
234 return NameField::Parse(scanner, is_ecml); 242 return NameField::Parse(scanner, is_ecml);
235 } 243 }
236 244
237 // static 245 // static
238 bool FormField::ParseText(AutofillScanner* scanner, const string16& pattern) {
239 const AutofillField* field;
240 return ParseText(scanner, pattern, &field);
241 }
242
243 // static
244 bool FormField::ParseText(AutofillScanner* scanner, 246 bool FormField::ParseText(AutofillScanner* scanner,
245 const string16& pattern, 247 const string16& pattern,
248 int match_type) {
249 const AutofillField* field;
250 return ParseText(scanner, pattern, match_type, &field);
251 }
252
253 // static
254 bool FormField::ParseText(AutofillScanner* scanner,
255 const string16& pattern,
256 int match_type,
246 const AutofillField** dest) { 257 const AutofillField** dest) {
247 return ParseText(scanner, pattern, dest, false); 258 return ParseText(scanner, pattern, dest, match_type);
248 } 259 }
249 260
250 // static 261 // static
251 bool FormField::ParseEmptyText(AutofillScanner* scanner, 262 bool FormField::ParseEmptyText(AutofillScanner* scanner,
252 const AutofillField** dest) { 263 const AutofillField** dest) {
253 return ParseLabelText(scanner, ASCIIToUTF16("^$"), dest); 264 return ParseText(scanner, ASCIIToUTF16("^$"),
254 } 265 MATCH_LABEL | MATCH_TEXT | MATCH_SELECT, dest);
255
256 // static
257 bool FormField::ParseLabelText(AutofillScanner* scanner,
258 const string16& pattern,
259 const AutofillField** dest) {
260 return ParseText(scanner, pattern, dest, true);
261 } 266 }
262 267
263 // static 268 // static
264 bool FormField::ParseText(AutofillScanner* scanner, 269 bool FormField::ParseText(AutofillScanner* scanner,
265 const string16& pattern, 270 const string16& pattern,
266 const AutofillField** dest, 271 const AutofillField** dest,
267 bool match_label_only) { 272 int match_type) {
268 if (scanner->IsEnd()) 273 if (scanner->IsEnd())
269 return false; 274 return false;
270 275
271 const AutofillField* field = scanner->Cursor(); 276 const AutofillField* field = scanner->Cursor();
272 if (Match(field, pattern, match_label_only)) { 277
278 if (!(match_type & MATCH_SELECT) &&
279 field->form_control_type == ASCIIToUTF16("select-one"))
280 return false;
281
282 if (!(match_type & MATCH_TEXT) && field->is_text_input)
283 return false;
284
285 if (MatchWithType(field, pattern, match_type)) {
273 if (dest) 286 if (dest)
274 *dest = field; 287 *dest = field;
275 scanner->Advance(); 288 scanner->Advance();
276 return true; 289 return true;
277 } 290 }
278 291
279 return false; 292 return false;
280 } 293 }
281 294
282 // static 295 // static
283 bool FormField::ParseLabelAndName(AutofillScanner* scanner, 296 bool FormField::ParseEmpty(AutofillScanner* scanner) {
284 const string16& pattern, 297 // TODO(jhawkins): Handle select fields.
285 const AutofillField** dest) { 298 const string16 pattern(ASCIIToUTF16("^$"));
286 const AutofillField* field = scanner->Cursor(); 299 const AutofillField* field = scanner->Cursor();
287 if (MatchLabel(field, pattern) && MatchName(field, pattern)) { 300 if (MatchLabel(field, pattern) && MatchName(field, pattern)) {
288 if (dest)
289 *dest = field;
290 scanner->Advance(); 301 scanner->Advance();
291 return true; 302 return true;
292 } 303 }
293 304
294 return false; 305 return false;
295 } 306 }
296 307
297 // static 308 // static
298 bool FormField::ParseEmpty(AutofillScanner* scanner) {
299 // TODO(jhawkins): Handle select fields.
300 return ParseLabelAndName(scanner, ASCIIToUTF16("^$"), NULL);
301 }
302
303 // static
304 bool FormField::Add(FieldTypeMap* field_type_map, 309 bool FormField::Add(FieldTypeMap* field_type_map,
305 const AutofillField* field, 310 const AutofillField* field,
306 AutofillFieldType type) { 311 AutofillFieldType type) {
307 // Several fields are optional. 312 // Several fields are optional.
308 if (!field) 313 if (!field)
309 return true; 314 return true;
310 315
311 // TODO(isherman): Is this the intent? 316 // TODO(isherman): Is this the intent?
312 return field_type_map->insert(make_pair(field->unique_name(), type)).second; 317 return field_type_map->insert(make_pair(field->unique_name(), type)).second;
313 } 318 }
314 319
315 string16 FormField::GetEcmlPattern(const char* ecml_name) { 320 string16 FormField::GetEcmlPattern(const char* ecml_name) {
316 return ASCIIToUTF16(std::string("^") + ecml_name); 321 return ASCIIToUTF16(std::string("^") + ecml_name);
317 } 322 }
318 323
319 string16 FormField::GetEcmlPattern(const char* ecml_name1, 324 string16 FormField::GetEcmlPattern(const char* ecml_name1,
320 const char* ecml_name2, 325 const char* ecml_name2,
321 char pattern_operator) { 326 char pattern_operator) {
322 return ASCIIToUTF16(StringPrintf("^%s%c^%s", 327 return ASCIIToUTF16(StringPrintf("^%s%c^%s",
323 ecml_name1, pattern_operator, ecml_name2)); 328 ecml_name1, pattern_operator, ecml_name2));
324 } 329 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698