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_TELEPHONE) && |
| 152 IsTelephoneField(field->form_control_type)) { |
| 153 return MatchAndAdvance(scanner, pattern, match_type, match); |
129 } | 154 } |
130 | 155 |
| 156 if ((match_type & MATCH_SELECT) && IsSelectField(field->form_control_type)) |
| 157 return MatchAndAdvance(scanner, pattern, match_type, match); |
| 158 |
131 return false; | 159 return false; |
132 } | 160 } |
133 | 161 |
134 // static | 162 // static |
135 bool FormField::ParseEmptyLabel(AutofillScanner* scanner, | 163 bool FormField::ParseEmptyLabel(AutofillScanner* scanner, |
136 const AutofillField** match) { | 164 const AutofillField** match) { |
137 return ParseFieldSpecifics(scanner, ASCIIToUTF16("^$"), MATCH_LABEL, match); | 165 return ParseFieldSpecifics(scanner, |
| 166 ASCIIToUTF16("^$"), |
| 167 MATCH_LABEL | MATCH_ALL_INPUTS, |
| 168 match); |
138 } | 169 } |
139 | 170 |
140 // static | 171 // static |
141 bool FormField::AddClassification(const AutofillField* field, | 172 bool FormField::AddClassification(const AutofillField* field, |
142 AutofillFieldType type, | 173 AutofillFieldType type, |
143 FieldTypeMap* map) { | 174 FieldTypeMap* map) { |
144 // Several fields are optional. | 175 // Several fields are optional. |
145 if (!field) | 176 if (!field) |
146 return true; | 177 return true; |
147 | 178 |
148 return map->insert(make_pair(field->unique_name(), type)).second; | 179 return map->insert(make_pair(field->unique_name(), type)).second; |
149 } | 180 } |
150 | 181 |
| 182 // static. |
| 183 bool FormField::MatchAndAdvance(AutofillScanner* scanner, |
| 184 const string16& pattern, |
| 185 int match_type, |
| 186 const AutofillField** match) { |
| 187 const AutofillField* field = scanner->Cursor(); |
| 188 if (FormField::Match(field, pattern, match_type)) { |
| 189 if (match) |
| 190 *match = field; |
| 191 scanner->Advance(); |
| 192 return true; |
| 193 } |
| 194 |
| 195 return false; |
| 196 } |
| 197 |
151 // static | 198 // static |
152 bool FormField::Match(const AutofillField* field, | 199 bool FormField::Match(const AutofillField* field, |
153 const string16& pattern, | 200 const string16& pattern, |
154 int match_type) { | 201 int match_type) { |
155 if ((match_type & FormField::MATCH_LABEL) && MatchLabel(field, pattern)) | 202 if ((match_type & FormField::MATCH_LABEL) && MatchLabel(field, pattern)) |
156 return true; | 203 return true; |
157 | 204 |
158 if ((match_type & FormField::MATCH_NAME) && MatchName(field, pattern)) | 205 if ((match_type & FormField::MATCH_NAME) && MatchName(field, pattern)) |
159 return true; | 206 return true; |
160 | 207 |
161 return false; | 208 return false; |
162 } | 209 } |
OLD | NEW |