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

Side by Side Diff: components/autofill/core/browser/name_field.cc

Issue 1453193002: autofill: switch autofill_regexes to RE2 library (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address reviews Created 5 years 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/core/browser/name_field.h" 5 #include "components/autofill/core/browser/name_field.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/autofill/core/browser/autofill_regex_constants.h" 9 #include "components/autofill/core/browser/autofill_regex_constants.h"
11 #include "components/autofill/core/browser/autofill_scanner.h" 10 #include "components/autofill/core/browser/autofill_scanner.h"
12 #include "components/autofill/core/browser/autofill_type.h" 11 #include "components/autofill/core/browser/autofill_type.h"
13 12
14 using base::UTF8ToUTF16;
15
16 namespace autofill { 13 namespace autofill {
17 namespace { 14 namespace {
18 15
19 // A form field that can parse a full name field. 16 // A form field that can parse a full name field.
20 class FullNameField : public NameField { 17 class FullNameField : public NameField {
21 public: 18 public:
22 static scoped_ptr<FullNameField> Parse(AutofillScanner* scanner); 19 static scoped_ptr<FullNameField> Parse(AutofillScanner* scanner);
23 20
24 protected: 21 protected:
25 // FormField: 22 // FormField:
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 70
74 // This is overriden in concrete subclasses. 71 // This is overriden in concrete subclasses.
75 bool NameField::ClassifyField(ServerFieldTypeMap* map) const { 72 bool NameField::ClassifyField(ServerFieldTypeMap* map) const {
76 return false; 73 return false;
77 } 74 }
78 75
79 // static 76 // static
80 scoped_ptr<FullNameField> FullNameField::Parse(AutofillScanner* scanner) { 77 scoped_ptr<FullNameField> FullNameField::Parse(AutofillScanner* scanner) {
81 // Exclude e.g. "username" or "nickname" fields. 78 // Exclude e.g. "username" or "nickname" fields.
82 scanner->SaveCursor(); 79 scanner->SaveCursor();
83 bool should_ignore = ParseField(scanner, UTF8ToUTF16(kNameIgnoredRe), NULL); 80 bool should_ignore = ParseField(scanner, kNameIgnoredRe, NULL);
84 scanner->Rewind(); 81 scanner->Rewind();
85 if (should_ignore) 82 if (should_ignore)
86 return NULL; 83 return NULL;
87 84
88 // Searching for any label containing the word "name" is too general; 85 // Searching for any label containing the word "name" is too general;
89 // for example, Travelocity_Edit travel profile.html contains a field 86 // for example, Travelocity_Edit travel profile.html contains a field
90 // "Travel Profile Name". 87 // "Travel Profile Name".
91 AutofillField* field = NULL; 88 AutofillField* field = NULL;
92 if (ParseField(scanner, UTF8ToUTF16(kNameRe), &field)) 89 if (ParseField(scanner, kNameRe, &field))
93 return make_scoped_ptr(new FullNameField(field)); 90 return make_scoped_ptr(new FullNameField(field));
94 91
95 return NULL; 92 return NULL;
96 } 93 }
97 94
98 bool FullNameField::ClassifyField(ServerFieldTypeMap* map) const { 95 bool FullNameField::ClassifyField(ServerFieldTypeMap* map) const {
99 return AddClassification(field_, NAME_FULL, map); 96 return AddClassification(field_, NAME_FULL, map);
100 } 97 }
101 98
102 FullNameField::FullNameField(AutofillField* field) : field_(field) { 99 FullNameField::FullNameField(AutofillField* field) : field_(field) {
103 } 100 }
104 101
105 scoped_ptr<FirstLastNameField> FirstLastNameField::ParseSpecificName( 102 scoped_ptr<FirstLastNameField> FirstLastNameField::ParseSpecificName(
106 AutofillScanner* scanner) { 103 AutofillScanner* scanner) {
107 // Some pages (e.g. Overstock_comBilling.html, SmithsonianCheckout.html) 104 // Some pages (e.g. Overstock_comBilling.html, SmithsonianCheckout.html)
108 // have the label "Name" followed by two or three text fields. 105 // have the label "Name" followed by two or three text fields.
109 scoped_ptr<FirstLastNameField> v(new FirstLastNameField); 106 scoped_ptr<FirstLastNameField> v(new FirstLastNameField);
110 scanner->SaveCursor(); 107 scanner->SaveCursor();
111 108
112 AutofillField* next = NULL; 109 AutofillField* next = NULL;
113 if (ParseField(scanner, UTF8ToUTF16(kNameSpecificRe), &v->first_name_) && 110 if (ParseField(scanner, kNameSpecificRe, &v->first_name_) &&
114 ParseEmptyLabel(scanner, &next)) { 111 ParseEmptyLabel(scanner, &next)) {
115 if (ParseEmptyLabel(scanner, &v->last_name_)) { 112 if (ParseEmptyLabel(scanner, &v->last_name_)) {
116 // There are three name fields; assume that the middle one is a 113 // There are three name fields; assume that the middle one is a
117 // middle initial (it is, at least, on SmithsonianCheckout.html). 114 // middle initial (it is, at least, on SmithsonianCheckout.html).
118 v->middle_name_ = next; 115 v->middle_name_ = next;
119 v->middle_initial_ = true; 116 v->middle_initial_ = true;
120 } else { // only two name fields 117 } else { // only two name fields
121 v->last_name_ = next; 118 v->last_name_ = next;
122 } 119 }
123 120
(...skipping 16 matching lines...) Expand all
140 // dell_checkout1.html). At least one UK page (The China Shop2.html) 137 // dell_checkout1.html). At least one UK page (The China Shop2.html)
141 // asks, in stuffy English style, for just initials and a surname, 138 // asks, in stuffy English style, for just initials and a surname,
142 // so we match "initials" here (and just fill in a first name there, 139 // so we match "initials" here (and just fill in a first name there,
143 // American-style). 140 // American-style).
144 // The ".*first$" matches fields ending in "first" (example in sample8.html). 141 // The ".*first$" matches fields ending in "first" (example in sample8.html).
145 // The ".*last$" matches fields ending in "last" (example in sample8.html). 142 // The ".*last$" matches fields ending in "last" (example in sample8.html).
146 143
147 // Allow name fields to appear in any order. 144 // Allow name fields to appear in any order.
148 while (!scanner->IsEnd()) { 145 while (!scanner->IsEnd()) {
149 // Skip over any unrelated fields, e.g. "username" or "nickname". 146 // Skip over any unrelated fields, e.g. "username" or "nickname".
150 if (ParseFieldSpecifics(scanner, UTF8ToUTF16(kNameIgnoredRe), 147 if (ParseFieldSpecifics(scanner, kNameIgnoredRe,
151 MATCH_DEFAULT | MATCH_SELECT, NULL)) { 148 MATCH_DEFAULT | MATCH_SELECT, NULL)) {
152 continue; 149 continue;
153 } 150 }
154 151
155 if (!v->first_name_ && 152 if (!v->first_name_ && ParseField(scanner, kFirstNameRe, &v->first_name_)) {
156 ParseField(scanner, UTF8ToUTF16(kFirstNameRe), &v->first_name_)) {
157 continue; 153 continue;
158 } 154 }
159 155
160 // We check for a middle initial before checking for a middle name 156 // We check for a middle initial before checking for a middle name
161 // because at least one page (PC Connection.html) has a field marked 157 // because at least one page (PC Connection.html) has a field marked
162 // as both (the label text is "MI" and the element name is 158 // as both (the label text is "MI" and the element name is
163 // "txtmiddlename"); such a field probably actually represents a 159 // "txtmiddlename"); such a field probably actually represents a
164 // middle initial. 160 // middle initial.
165 if (!v->middle_name_ && 161 if (!v->middle_name_ &&
166 ParseField(scanner, UTF8ToUTF16(kMiddleInitialRe), &v->middle_name_)) { 162 ParseField(scanner, kMiddleInitialRe, &v->middle_name_)) {
167 v->middle_initial_ = true; 163 v->middle_initial_ = true;
168 continue; 164 continue;
169 } 165 }
170 166
171 if (!v->middle_name_ && 167 if (!v->middle_name_ &&
172 ParseField(scanner, UTF8ToUTF16(kMiddleNameRe), &v->middle_name_)) { 168 ParseField(scanner, kMiddleNameRe, &v->middle_name_)) {
173 continue; 169 continue;
174 } 170 }
175 171
176 if (!v->last_name_ && 172 if (!v->last_name_ && ParseField(scanner, kLastNameRe, &v->last_name_)) {
177 ParseField(scanner, UTF8ToUTF16(kLastNameRe), &v->last_name_)) {
178 continue; 173 continue;
179 } 174 }
180 175
181 break; 176 break;
182 } 177 }
183 178
184 // Consider the match to be successful if we detected both first and last name 179 // Consider the match to be successful if we detected both first and last name
185 // fields. 180 // fields.
186 if (v->first_name_ && v->last_name_) 181 if (v->first_name_ && v->last_name_)
187 return v.Pass(); 182 return v.Pass();
(...skipping 20 matching lines...) Expand all
208 203
209 bool FirstLastNameField::ClassifyField(ServerFieldTypeMap* map) const { 204 bool FirstLastNameField::ClassifyField(ServerFieldTypeMap* map) const {
210 bool ok = AddClassification(first_name_, NAME_FIRST, map); 205 bool ok = AddClassification(first_name_, NAME_FIRST, map);
211 ok = ok && AddClassification(last_name_, NAME_LAST, map); 206 ok = ok && AddClassification(last_name_, NAME_LAST, map);
212 ServerFieldType type = middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE; 207 ServerFieldType type = middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE;
213 ok = ok && AddClassification(middle_name_, type, map); 208 ok = ok && AddClassification(middle_name_, type, map);
214 return ok; 209 return ok;
215 } 210 }
216 211
217 } // namespace autofill 212 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698