OLD | NEW |
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/name_field.h" | 5 #include "chrome/browser/autofill/name_field.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 // Exclude labels containing the string "username", which typically | 32 // Exclude labels containing the string "username", which typically |
33 // denotes a login ID rather than the user's actual name. | 33 // denotes a login ID rather than the user's actual name. |
34 const AutofillField* field = scanner->Cursor(); | 34 const AutofillField* field = scanner->Cursor(); |
35 if (Match(field, l10n_util::GetStringUTF16(IDS_AUTOFILL_USERNAME_RE), false)) | 35 if (Match(field, l10n_util::GetStringUTF16(IDS_AUTOFILL_USERNAME_RE), false)) |
36 return NULL; | 36 return NULL; |
37 | 37 |
38 // Searching for any label containing the word "name" is too general; | 38 // Searching for any label containing the word "name" is too general; |
39 // for example, Travelocity_Edit travel profile.html contains a field | 39 // for example, Travelocity_Edit travel profile.html contains a field |
40 // "Travel Profile Name". | 40 // "Travel Profile Name". |
41 if (ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_NAME_RE), | 41 if (ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_NAME_RE), |
| 42 MATCH_NAME | MATCH_LABEL | MATCH_TEXT, |
42 &field)) | 43 &field)) |
43 return new FullNameField(field); | 44 return new FullNameField(field); |
44 | 45 |
45 return NULL; | 46 return NULL; |
46 } | 47 } |
47 | 48 |
48 FullNameField::FullNameField(const AutofillField* field) | 49 FullNameField::FullNameField(const AutofillField* field) |
49 : field_(field) { | 50 : field_(field) { |
50 } | 51 } |
51 | 52 |
52 bool FirstLastNameField::GetFieldInfo(FieldTypeMap* field_type_map) const { | 53 bool FirstLastNameField::GetFieldInfo(FieldTypeMap* field_type_map) const { |
53 bool ok = Add(field_type_map, first_name_, NAME_FIRST); | 54 bool ok = Add(field_type_map, first_name_, NAME_FIRST); |
54 ok = ok && Add(field_type_map, last_name_, NAME_LAST); | 55 ok = ok && Add(field_type_map, last_name_, NAME_LAST); |
55 AutofillFieldType type = middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE; | 56 AutofillFieldType type = middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE; |
56 ok = ok && Add(field_type_map, middle_name_, type); | 57 ok = ok && Add(field_type_map, middle_name_, type); |
57 return ok; | 58 return ok; |
58 } | 59 } |
59 | 60 |
60 FirstLastNameField* FirstLastNameField::ParseSpecificName( | 61 FirstLastNameField* FirstLastNameField::ParseSpecificName( |
61 AutofillScanner* scanner) { | 62 AutofillScanner* scanner) { |
62 // Some pages (e.g. Overstock_comBilling.html, SmithsonianCheckout.html) | 63 // Some pages (e.g. Overstock_comBilling.html, SmithsonianCheckout.html) |
63 // have the label "Name" followed by two or three text fields. | 64 // have the label "Name" followed by two or three text fields. |
64 scoped_ptr<FirstLastNameField> v(new FirstLastNameField); | 65 scoped_ptr<FirstLastNameField> v(new FirstLastNameField); |
65 scanner->SaveCursor(); | 66 scanner->SaveCursor(); |
66 | 67 |
67 const AutofillField* next; | 68 const AutofillField* next; |
68 if (ParseText(scanner, | 69 if (ParseText(scanner, |
69 l10n_util::GetStringUTF16(IDS_AUTOFILL_NAME_SPECIFIC_RE), | 70 l10n_util::GetStringUTF16(IDS_AUTOFILL_NAME_SPECIFIC_RE), |
| 71 MATCH_NAME | MATCH_LABEL | MATCH_TEXT, |
70 &v->first_name_) && | 72 &v->first_name_) && |
71 ParseEmptyText(scanner, &next)) { | 73 ParseEmptyText(scanner, &next)) { |
72 if (ParseEmptyText(scanner, &v->last_name_)) { | 74 if (ParseEmptyText(scanner, &v->last_name_)) { |
73 // There are three name fields; assume that the middle one is a | 75 // There are three name fields; assume that the middle one is a |
74 // middle initial (it is, at least, on SmithsonianCheckout.html). | 76 // middle initial (it is, at least, on SmithsonianCheckout.html). |
75 v->middle_name_ = next; | 77 v->middle_name_ = next; |
76 v->middle_initial_ = true; | 78 v->middle_initial_ = true; |
77 } else { // only two name fields | 79 } else { // only two name fields |
78 v->last_name_ = next; | 80 v->last_name_ = next; |
79 } | 81 } |
(...skipping 12 matching lines...) Expand all Loading... |
92 | 94 |
93 // A fair number of pages use the names "fname" and "lname" for naming | 95 // A fair number of pages use the names "fname" and "lname" for naming |
94 // first and last name fields (examples from the test suite: | 96 // first and last name fields (examples from the test suite: |
95 // BESTBUY_COM - Sign In2.html; Crate and Barrel Check Out.html; | 97 // BESTBUY_COM - Sign In2.html; Crate and Barrel Check Out.html; |
96 // dell_checkout1.html). At least one UK page (The China Shop2.html) | 98 // dell_checkout1.html). At least one UK page (The China Shop2.html) |
97 // asks, in stuffy English style, for just initials and a surname, | 99 // asks, in stuffy English style, for just initials and a surname, |
98 // so we match "initials" here (and just fill in a first name there, | 100 // so we match "initials" here (and just fill in a first name there, |
99 // American-style). | 101 // American-style). |
100 // The ".*first$" matches fields ending in "first" (example in sample8.html). | 102 // The ".*first$" matches fields ending in "first" (example in sample8.html). |
101 if (!ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_FIRST_NAME_RE), | 103 if (!ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_FIRST_NAME_RE), |
102 &v->first_name_)) | 104 MATCH_NAME | MATCH_LABEL | MATCH_TEXT, &v->first_name_)) |
103 return NULL; | 105 return NULL; |
104 | 106 |
105 // We check for a middle initial before checking for a middle name | 107 // We check for a middle initial before checking for a middle name |
106 // because at least one page (PC Connection.html) has a field marked | 108 // because at least one page (PC Connection.html) has a field marked |
107 // as both (the label text is "MI" and the element name is | 109 // as both (the label text is "MI" and the element name is |
108 // "txtmiddlename"); such a field probably actually represents a | 110 // "txtmiddlename"); such a field probably actually represents a |
109 // middle initial. | 111 // middle initial. |
110 if (ParseText(scanner, | 112 if (ParseText(scanner, |
111 l10n_util::GetStringUTF16(IDS_AUTOFILL_MIDDLE_INITIAL_RE), | 113 l10n_util::GetStringUTF16(IDS_AUTOFILL_MIDDLE_INITIAL_RE), |
| 114 MATCH_NAME | MATCH_LABEL | MATCH_TEXT, |
112 &v->middle_name_)) { | 115 &v->middle_name_)) { |
113 v->middle_initial_ = true; | 116 v->middle_initial_ = true; |
114 } else { | 117 } else { |
115 ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_MIDDLE_NAME_RE), | 118 ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_MIDDLE_NAME_RE), |
116 &v->middle_name_); | 119 MATCH_NAME | MATCH_LABEL | MATCH_TEXT, &v->middle_name_); |
117 } | 120 } |
118 | 121 |
119 // The ".*last$" matches fields ending in "last" (example in sample8.html). | 122 // The ".*last$" matches fields ending in "last" (example in sample8.html). |
120 if (ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_LAST_NAME_RE), | 123 if (ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_LAST_NAME_RE), |
121 &v->last_name_)) { | 124 MATCH_NAME | MATCH_LABEL | MATCH_TEXT, &v->last_name_)) { |
122 return v.release(); | 125 return v.release(); |
123 } | 126 } |
124 | 127 |
125 scanner->Rewind(); | 128 scanner->Rewind(); |
126 return NULL; | 129 return NULL; |
127 } | 130 } |
128 | 131 |
129 FirstLastNameField* FirstLastNameField::ParseEcmlName( | 132 FirstLastNameField* FirstLastNameField::ParseEcmlName( |
130 AutofillScanner* scanner) { | 133 AutofillScanner* scanner) { |
131 scoped_ptr<FirstLastNameField> field(new FirstLastNameField); | 134 scoped_ptr<FirstLastNameField> field(new FirstLastNameField); |
132 scanner->SaveCursor(); | 135 scanner->SaveCursor(); |
133 | 136 |
134 string16 pattern = GetEcmlPattern(kEcmlShipToFirstName, | 137 string16 pattern = GetEcmlPattern(kEcmlShipToFirstName, |
135 kEcmlBillToFirstName, '|'); | 138 kEcmlBillToFirstName, '|'); |
136 if (!ParseText(scanner, pattern, &field->first_name_)) | 139 if (!ParseText(scanner, pattern, MATCH_NAME | MATCH_LABEL | MATCH_TEXT, |
| 140 &field->first_name_)) |
137 return NULL; | 141 return NULL; |
138 | 142 |
139 pattern = GetEcmlPattern(kEcmlShipToMiddleName, kEcmlBillToMiddleName, '|'); | 143 pattern = GetEcmlPattern(kEcmlShipToMiddleName, kEcmlBillToMiddleName, '|'); |
140 ParseText(scanner, pattern, &field->middle_name_); | 144 ParseText(scanner, pattern, MATCH_NAME | MATCH_LABEL | MATCH_TEXT, |
| 145 &field->middle_name_); |
141 | 146 |
142 pattern = GetEcmlPattern(kEcmlShipToLastName, kEcmlBillToLastName, '|'); | 147 pattern = GetEcmlPattern(kEcmlShipToLastName, kEcmlBillToLastName, '|'); |
143 if (ParseText(scanner, pattern, &field->last_name_)) | 148 if (ParseText(scanner, pattern, MATCH_NAME | MATCH_LABEL | MATCH_TEXT, |
| 149 &field->last_name_)) |
144 return field.release(); | 150 return field.release(); |
145 | 151 |
146 scanner->Rewind(); | 152 scanner->Rewind(); |
147 return NULL; | 153 return NULL; |
148 } | 154 } |
149 | 155 |
150 FirstLastNameField* FirstLastNameField::Parse(AutofillScanner* scanner, | 156 FirstLastNameField* FirstLastNameField::Parse(AutofillScanner* scanner, |
151 bool is_ecml) { | 157 bool is_ecml) { |
152 if (is_ecml) | 158 if (is_ecml) |
153 return ParseEcmlName(scanner); | 159 return ParseEcmlName(scanner); |
154 | 160 |
155 FirstLastNameField* field = ParseSpecificName(scanner); | 161 FirstLastNameField* field = ParseSpecificName(scanner); |
156 if (!field) | 162 if (!field) |
157 field = ParseComponentNames(scanner); | 163 field = ParseComponentNames(scanner); |
158 return field; | 164 return field; |
159 } | 165 } |
160 | 166 |
161 FirstLastNameField::FirstLastNameField() | 167 FirstLastNameField::FirstLastNameField() |
162 : first_name_(NULL), | 168 : first_name_(NULL), |
163 middle_name_(NULL), | 169 middle_name_(NULL), |
164 last_name_(NULL), | 170 last_name_(NULL), |
165 middle_initial_(false) { | 171 middle_initial_(false) { |
166 } | 172 } |
OLD | NEW |