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

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

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

Powered by Google App Engine
This is Rietveld 408576698