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/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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 | 74 |
75 field = CreditCardField::Parse(scanner, is_ecml); | 75 field = CreditCardField::Parse(scanner, is_ecml); |
76 if (field) | 76 if (field) |
77 return field; | 77 return field; |
78 | 78 |
79 // We search for a |NameField| last since it matches the word "name", which is | 79 // We search for a |NameField| last since it matches the word "name", which is |
80 // relatively general. | 80 // relatively general. |
81 return NameField::Parse(scanner, is_ecml); | 81 return NameField::Parse(scanner, is_ecml); |
82 } | 82 } |
83 | 83 |
84 bool IsTextField(const string16& type) { | |
85 return type == ASCIIToUTF16("text"); | |
86 } | |
87 | |
88 bool IsEmailField(const string16& type) { | |
89 return type == ASCIIToUTF16("email"); | |
90 } | |
91 | |
92 bool IsMonthField(const string16& type) { | |
93 return type == ASCIIToUTF16("month"); | |
94 } | |
95 | |
96 bool IsTelephoneField(const string16& type) { | |
97 return type == ASCIIToUTF16("tel"); | |
98 } | |
99 | |
100 bool IsSelectField(const string16& type) { | |
101 return type == ASCIIToUTF16("select-one"); | |
102 } | |
103 | |
84 } // namespace | 104 } // namespace |
85 | 105 |
86 // static | 106 // static |
87 void FormField::ParseFormFields(const std::vector<AutofillField*>& fields, | 107 void FormField::ParseFormFields(const std::vector<AutofillField*>& fields, |
88 FieldTypeMap* map) { | 108 FieldTypeMap* map) { |
89 // First, check if there is at least one form field with an ECML name. | 109 // First, check if there is at least one form field with an ECML name. |
90 // If there is, then we will match an element only if it is in the standard. | 110 // If there is, then we will match an element only if it is in the standard. |
91 bool is_ecml = HasECMLField(fields); | 111 bool is_ecml = HasECMLField(fields); |
92 | 112 |
93 // Parse fields. | 113 // Parse fields. |
94 AutofillScanner scanner(fields); | 114 AutofillScanner scanner(fields); |
95 while (!scanner.IsEnd()) { | 115 while (!scanner.IsEnd()) { |
96 scoped_ptr<FormField> form_field(ParseFormField(&scanner, is_ecml)); | 116 scoped_ptr<FormField> form_field(ParseFormField(&scanner, is_ecml)); |
97 if (!form_field.get()) { | 117 if (!form_field.get()) { |
98 scanner.Advance(); | 118 scanner.Advance(); |
99 continue; | 119 continue; |
100 } | 120 } |
101 | 121 |
102 // Add entries into the map for each field type found in |form_field|. | 122 // Add entries into the map for each field type found in |form_field|. |
103 bool ok = form_field->ClassifyField(map); | 123 bool ok = form_field->ClassifyField(map); |
104 DCHECK(ok); | 124 DCHECK(ok); |
105 } | 125 } |
106 } | 126 } |
107 | 127 |
108 // static | 128 // static |
109 bool FormField::ParseField(AutofillScanner* scanner, | 129 bool FormField::ParseField(AutofillScanner* scanner, |
110 const string16& pattern, | 130 const string16& pattern, |
111 const AutofillField** match) { | 131 const AutofillField** match) { |
112 return ParseFieldSpecifics(scanner, pattern, MATCH_ALL, match); | 132 return ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT, match); |
113 } | 133 } |
114 | 134 |
115 // static | 135 // static |
116 bool FormField::ParseFieldSpecifics(AutofillScanner* scanner, | 136 bool FormField::ParseFieldSpecifics(AutofillScanner* scanner, |
117 const string16& pattern, | 137 const string16& pattern, |
118 int match_type, | 138 int match_type, |
119 const AutofillField** match) { | 139 const AutofillField** match) { |
120 if (scanner->IsEnd()) | 140 if (scanner->IsEnd()) |
121 return false; | 141 return false; |
122 | 142 |
123 const AutofillField* field = scanner->Cursor(); | 143 const AutofillField* field = scanner->Cursor(); |
124 if (Match(field, pattern, match_type)) { | 144 |
125 if (match) | 145 if ((match_type & MATCH_TEXT) && IsTextField(field->form_control_type)) |
126 *match = field; | 146 return MatchAndAdvance(scanner, pattern, match_type, match); |
127 scanner->Advance(); | 147 |
128 return true; | 148 if ((match_type & MATCH_EMAIL) && IsEmailField(field->form_control_type)) |
149 return MatchAndAdvance(scanner, pattern, match_type, match); | |
150 | |
151 if ((match_type & MATCH_MONTH) && IsMonthField(field->form_control_type)) | |
152 return MatchAndAdvance(scanner, pattern, match_type, match); | |
Ilya Sherman
2011/05/25 03:52:02
There don't seem to be any clients who ever call i
dhollowa
2011/05/25 15:29:47
The credit card code currently explicitly looks fo
| |
153 | |
154 if ((match_type & MATCH_TELEPHONE) | |
155 && IsTelephoneField(field->form_control_type)) { | |
156 return MatchAndAdvance(scanner, pattern, match_type, match); | |
129 } | 157 } |
130 | 158 |
159 if ((match_type & MATCH_SELECT) && IsSelectField(field->form_control_type)) | |
160 return MatchAndAdvance(scanner, pattern, match_type, match); | |
161 | |
131 return false; | 162 return false; |
132 } | 163 } |
133 | 164 |
134 // static | 165 // static |
135 bool FormField::ParseEmptyLabel(AutofillScanner* scanner, | 166 bool FormField::ParseEmptyLabel(AutofillScanner* scanner, |
136 const AutofillField** match) { | 167 const AutofillField** match) { |
137 return ParseFieldSpecifics(scanner, ASCIIToUTF16("^$"), MATCH_LABEL, match); | 168 return ParseFieldSpecifics(scanner, |
169 ASCIIToUTF16("^$"), | |
170 MATCH_LABEL | MATCH_ALL_INPUTS, | |
171 match); | |
138 } | 172 } |
139 | 173 |
140 // static | 174 // static |
141 bool FormField::AddClassification(const AutofillField* field, | 175 bool FormField::AddClassification(const AutofillField* field, |
142 AutofillFieldType type, | 176 AutofillFieldType type, |
143 FieldTypeMap* map) { | 177 FieldTypeMap* map) { |
144 // Several fields are optional. | 178 // Several fields are optional. |
145 if (!field) | 179 if (!field) |
146 return true; | 180 return true; |
147 | 181 |
148 return map->insert(make_pair(field->unique_name(), type)).second; | 182 return map->insert(make_pair(field->unique_name(), type)).second; |
149 } | 183 } |
150 | 184 |
185 // static. | |
186 bool FormField::MatchAndAdvance(AutofillScanner* scanner, | |
187 const string16& pattern, | |
188 int match_type, | |
189 const AutofillField** match) { | |
190 const AutofillField* field = scanner->Cursor(); | |
191 if (FormField::Match(field, pattern, match_type)) { | |
192 if (match) | |
193 *match = field; | |
194 scanner->Advance(); | |
195 return true; | |
196 } | |
197 | |
198 return false; | |
199 } | |
200 | |
151 // static | 201 // static |
152 bool FormField::Match(const AutofillField* field, | 202 bool FormField::Match(const AutofillField* field, |
153 const string16& pattern, | 203 const string16& pattern, |
154 int match_type) { | 204 int match_type) { |
155 if ((match_type & FormField::MATCH_LABEL) && MatchLabel(field, pattern)) | 205 if ((match_type & FormField::MATCH_LABEL) && MatchLabel(field, pattern)) |
156 return true; | 206 return true; |
157 | 207 |
158 if ((match_type & FormField::MATCH_NAME) && MatchName(field, pattern)) | 208 if ((match_type & FormField::MATCH_NAME) && MatchName(field, pattern)) |
159 return true; | 209 return true; |
160 | 210 |
161 return false; | 211 return false; |
162 } | 212 } |
OLD | NEW |