1/*
2 * Copyright (C) 2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "Autofill.h"
28
29#include "HTMLFormControlElement.h"
30#include "HTMLFormElement.h"
31#include "HTMLNames.h"
32#include <wtf/HashMap.h>
33#include <wtf/NeverDestroyed.h>
34#include <wtf/text/AtomicString.h>
35#include <wtf/text/AtomicStringHash.h>
36
37namespace WebCore {
38
39enum class AutofillCategory {
40 Off,
41 Automatic,
42 Normal,
43 Contact,
44};
45
46struct AutofillInfo {
47 AutofillFieldName fieldName;
48 AutofillCategory category;
49};
50
51static const HashMap<AtomicString, AutofillInfo>& fieldNameMap()
52{
53 static const auto map = makeNeverDestroyed([] {
54 struct MapEntry {
55 const char* name;
56 AutofillInfo value;
57 };
58 static const MapEntry entries[] = {
59 { "off", { AutofillFieldName::None, AutofillCategory::Off } },
60 { "on", { AutofillFieldName::None, AutofillCategory::Automatic } },
61 { "name", { AutofillFieldName::Name, AutofillCategory::Normal } },
62 { "honorific-prefix", { AutofillFieldName::HonorificPrefix, AutofillCategory::Normal } },
63 { "given-name", { AutofillFieldName::GivenName, AutofillCategory::Normal } },
64 { "additional-name", { AutofillFieldName::AdditionalName, AutofillCategory::Normal } },
65 { "family-name", { AutofillFieldName::FamilyName, AutofillCategory::Normal } },
66 { "honorific-suffix", { AutofillFieldName::HonorificSuffix, AutofillCategory::Normal } },
67 { "nickname", { AutofillFieldName::Nickname, AutofillCategory::Normal } },
68 { "username", { AutofillFieldName::Username, AutofillCategory::Normal } },
69 { "new-password", { AutofillFieldName::NewPassword, AutofillCategory::Normal } },
70 { "current-password", { AutofillFieldName::CurrentPassword, AutofillCategory::Normal } },
71 { "organization-title", { AutofillFieldName::OrganizationTitle, AutofillCategory::Normal } },
72 { "organization", { AutofillFieldName::Organization, AutofillCategory::Normal } },
73 { "street-address", { AutofillFieldName::StreetAddress, AutofillCategory::Normal } },
74 { "address-line1", { AutofillFieldName::AddressLine1, AutofillCategory::Normal } },
75 { "address-line2", { AutofillFieldName::AddressLine2, AutofillCategory::Normal } },
76 { "address-line3", { AutofillFieldName::AddressLine3, AutofillCategory::Normal } },
77 { "address-level4", { AutofillFieldName::AddressLevel4, AutofillCategory::Normal } },
78 { "address-level3", { AutofillFieldName::AddressLevel3, AutofillCategory::Normal } },
79 { "address-level2", { AutofillFieldName::AddressLevel2, AutofillCategory::Normal } },
80 { "address-level1", { AutofillFieldName::AddressLevel1, AutofillCategory::Normal } },
81 { "country", { AutofillFieldName::Country, AutofillCategory::Normal } },
82 { "country-name", { AutofillFieldName::CountryName, AutofillCategory::Normal } },
83 { "postal-code", { AutofillFieldName::PostalCode, AutofillCategory::Normal } },
84 { "cc-name", { AutofillFieldName::CcName, AutofillCategory::Normal } },
85 { "cc-given-name", { AutofillFieldName::CcGivenName, AutofillCategory::Normal } },
86 { "cc-additional-name", { AutofillFieldName::CcAdditionalName, AutofillCategory::Normal } },
87 { "cc-family-name", { AutofillFieldName::CcFamilyName, AutofillCategory::Normal } },
88 { "cc-number", { AutofillFieldName::CcNumber, AutofillCategory::Normal } },
89 { "cc-exp", { AutofillFieldName::CcExp, AutofillCategory::Normal } },
90 { "cc-exp-month", { AutofillFieldName::CcExpMonth, AutofillCategory::Normal } },
91 { "cc-exp-year", { AutofillFieldName::CcExpYear, AutofillCategory::Normal } },
92 { "cc-csc", { AutofillFieldName::CcCsc, AutofillCategory::Normal } },
93 { "cc-type", { AutofillFieldName::CcType, AutofillCategory::Normal } },
94 { "transaction-currency", { AutofillFieldName::TransactionCurrency, AutofillCategory::Normal } },
95 { "transaction-amount", { AutofillFieldName::TransactionAmount, AutofillCategory::Normal } },
96 { "language", { AutofillFieldName::Language, AutofillCategory::Normal } },
97 { "bday", { AutofillFieldName::Bday, AutofillCategory::Normal } },
98 { "bday-day", { AutofillFieldName::BdayDay, AutofillCategory::Normal } },
99 { "bday-month", { AutofillFieldName::BdayMonth, AutofillCategory::Normal } },
100 { "bday-year", { AutofillFieldName::BdayYear, AutofillCategory::Normal } },
101 { "sex", { AutofillFieldName::Sex, AutofillCategory::Normal } },
102 { "url", { AutofillFieldName::URL, AutofillCategory::Normal } },
103 { "photo", { AutofillFieldName::Photo, AutofillCategory::Normal } },
104
105 { "tel", { AutofillFieldName::Tel, AutofillCategory::Contact } },
106 { "tel-country-code", { AutofillFieldName::TelCountryCode, AutofillCategory::Contact } },
107 { "tel-national", { AutofillFieldName::TelNational, AutofillCategory::Contact } },
108 { "tel-area-code", { AutofillFieldName::TelAreaCode, AutofillCategory::Contact } },
109 { "tel-local", { AutofillFieldName::TelLocal, AutofillCategory::Contact } },
110 { "tel-local-prefix", { AutofillFieldName::TelLocalPrefix, AutofillCategory::Contact } },
111 { "tel-local-suffix", { AutofillFieldName::TelLocalSuffix, AutofillCategory::Contact } },
112 { "tel-extension", { AutofillFieldName::TelExtension, AutofillCategory::Contact } },
113 { "email", { AutofillFieldName::Email, AutofillCategory::Contact } },
114 { "impp", { AutofillFieldName::Impp, AutofillCategory::Contact } },
115 };
116 HashMap<AtomicString, AutofillInfo> map;
117 for (auto& entry : entries)
118 map.add(entry.name, entry.value);
119 return map;
120 }());
121 return map;
122}
123
124AutofillFieldName toAutofillFieldName(const AtomicString& value)
125{
126 auto map = fieldNameMap();
127 auto it = map.find(value);
128 if (it == map.end())
129 return AutofillFieldName::None;
130 return it->value.fieldName;
131}
132
133static inline bool isContactToken(const AtomicString& token)
134{
135 static NeverDestroyed<AtomicString> home("home", AtomicString::ConstructFromLiteral);
136 static NeverDestroyed<AtomicString> work("work", AtomicString::ConstructFromLiteral);
137 static NeverDestroyed<AtomicString> mobile("mobile", AtomicString::ConstructFromLiteral);
138 static NeverDestroyed<AtomicString> fax("fax", AtomicString::ConstructFromLiteral);
139 static NeverDestroyed<AtomicString> pager("pager", AtomicString::ConstructFromLiteral);
140
141 return token == home || token == work || token == mobile || token == fax || token == pager;
142}
143
144static unsigned maxTokensForAutofillFieldCategory(AutofillCategory category)
145{
146 switch (category) {
147 case AutofillCategory::Automatic:
148 case AutofillCategory::Off:
149 return 1;
150
151 case AutofillCategory::Normal:
152 return 3;
153
154 case AutofillCategory::Contact:
155 return 4;
156 }
157 ASSERT_NOT_REACHED();
158 return 0;
159}
160
161// https://html.spec.whatwg.org/multipage/forms.html#processing-model-3
162AutofillData AutofillData::createFromHTMLFormControlElement(const HTMLFormControlElement& element)
163{
164 static NeverDestroyed<AtomicString> on("on", AtomicString::ConstructFromLiteral);
165 static NeverDestroyed<AtomicString> off("off", AtomicString::ConstructFromLiteral);
166
167 // Label: Default
168 // 26. Let the element's IDL-exposed autofill value be the empty string, and its autofill hint set and autofill scope be empty.
169 // 27. If the element's autocomplete attribute is wearing the autofill anchor mantle, then let the element's autofill field name be the empty string and abort these steps.
170 // 28. Let form be the element's form owner, if any, or null otherwise.
171 // 29. If form is not null and form's autocomplete attribute is in the off state, then let the element's autofill field name be "off". Otherwise, let the element's autofill field name be "on".
172 auto defaultLabel = [&] () -> AutofillData {
173 if (element.autofillMantle() == AutofillMantle::Anchor)
174 return { emptyString(), emptyString() };
175
176 auto form = element.form();
177 if (form && form->autocomplete() == off)
178 return { off, emptyString() };
179 return { on, emptyString() };
180 };
181
182
183 const AtomicString& attributeValue = element.attributeWithoutSynchronization(HTMLNames::autocompleteAttr);
184
185 // 1. If the element has no autocomplete attribute, then jump to the step labeled default.
186 if (attributeValue.isNull())
187 return defaultLabel();
188
189 // 2. Let tokens be the result of splitting the attribute's value on spaces.
190 SpaceSplitString tokens(attributeValue, true);
191
192 // 3. If tokens is empty, then jump to the step labeled default.
193 if (tokens.isEmpty())
194 return defaultLabel();
195
196 // 4. Let index be the index of the last token in tokens
197 unsigned index = tokens.size() - 1;
198
199 // 5. If the indexth token in tokens is not an ASCII case-insensitive match for one of the
200 // tokens given in the first column of the following table, or if the number of tokens in
201 // tokens is greater than the maximum number given in the cell in the second column of that
202 // token's row, then jump to the step labeled default. Otherwise, let field be the string given
203 // in the cell of the first column of the matching row, and let category be the value of the
204 // cell in the third column of that same row.
205 auto& map = fieldNameMap();
206
207 auto it = map.find(tokens[index]);
208 if (it == map.end())
209 return defaultLabel();
210
211 auto category = it->value.category;
212
213 if (tokens.size() > maxTokensForAutofillFieldCategory(category))
214 return defaultLabel();
215
216 const auto& field = tokens[index];
217
218 // 6. If category is Off or Automatic but the element's autocomplete attribute is wearing the
219 // autofill anchor mantle, then jump to the step labeled default.
220 auto mantle = element.autofillMantle();
221 if ((category == AutofillCategory::Off || category == AutofillCategory::Automatic) && mantle == AutofillMantle::Anchor)
222 return defaultLabel();
223
224 // 7. If category is Off, let the element's autofill field name be the string "off", let its
225 // autofill hint set be empty, and let its IDL-exposed autofill value be the string "off".
226 // Then, abort these steps.
227 if (category == AutofillCategory::Off)
228 return { off, off.get().string() };
229
230 // 8. If category is Automatic, let the element's autofill field name be the string "on",
231 // let its autofill hint set be empty, and let its IDL-exposed autofill value be the string
232 // "on". Then, abort these steps.
233 if (category == AutofillCategory::Automatic)
234 return { on, on.get().string() };
235
236 // 9. Let scope tokens be an empty list.
237 // 10. Let hint tokens be an empty set.
238
239 // NOTE: We are ignoring these steps as we don't currently make use of scope tokens or hint tokens anywhere.
240
241 // 11. Let IDL value have the same value as field.
242 String idlValue = field;
243
244 // 12, If the indexth token in tokens is the first entry, then skip to the step labeled done.
245 if (index == 0)
246 return { field, idlValue };
247
248 // 13. Decrement index by one
249 index--;
250
251 // 14. If category is Contact and the indexth token in tokens is an ASCII case-insensitive match
252 // for one of the strings in the following list, then run the substeps that follow:
253 const auto& contactToken = tokens[index];
254 if (category == AutofillCategory::Contact && isContactToken(contactToken)) {
255 // 1. Let contact be the matching string from the list above.
256 const auto& contact = contactToken;
257
258 // 2. Insert contact at the start of scope tokens.
259 // 3. Add contact to hint tokens.
260
261 // NOTE: We are ignoring these steps as we don't currently make use of scope tokens or hint tokens anywhere.
262
263 // 4. Let IDL value be the concatenation of contact, a U+0020 SPACE character, and the previous
264 // value of IDL value (which at this point will always be field).
265 idlValue = WTF::makeString(contact, " ", idlValue);
266
267 // 5. If the indexth entry in tokens is the first entry, then skip to the step labeled done.
268 if (index == 0)
269 return { field, idlValue };
270
271 // 6. Decrement index by one.
272 index--;
273 }
274
275 // 15. If the indexth token in tokens is an ASCII case-insensitive match for one of the strings
276 // in the following list, then run the substeps that follow:
277 const auto& modeToken = tokens[index];
278 if (equalIgnoringASCIICase(modeToken, "shipping") || equalIgnoringASCIICase(modeToken, "billing")) {
279 // 1. Let mode be the matching string from the list above.
280 const auto& mode = modeToken;
281
282 // 2. Insert mode at the start of scope tokens.
283 // 3. Add mode to hint tokens.
284
285 // NOTE: We are ignoring these steps as we don't currently make use of scope tokens or hint tokens anywhere.
286
287 // 4. Let IDL value be the concatenation of mode, a U+0020 SPACE character, and the previous
288 // value of IDL value (which at this point will either be field or the concatenation of contact,
289 // a space, and field).
290 idlValue = WTF::makeString(mode, " ", idlValue);
291
292 // 5. If the indexth entry in tokens is the first entry, then skip to the step labeled done.
293 if (index == 0)
294 return { field, idlValue };
295
296 // 6. Decrement index by one.
297 index--;
298 }
299
300 // 16. If the indexth entry in tokens is not the first entry, then jump to the step labeled default.
301 if (index != 0)
302 return defaultLabel();
303
304 // 17. If the first eight characters of the indexth token in tokens are not an ASCII case-insensitive
305 // match for the string "section-", then jump to the step labeled default.
306 const auto& sectionToken = tokens[index];
307 if (!startsWithLettersIgnoringASCIICase(sectionToken, "section-"))
308 return defaultLabel();
309
310 // 18. Let section be the indexth token in tokens, converted to ASCII lowercase.
311 const auto& section = sectionToken;
312
313 // 19. Insert section at the start of scope tokens.
314
315 // NOTE: We are ignoring this step as we don't currently make use of scope tokens or hint tokens anywhere.
316
317 // 20. Let IDL value be the concatenation of section, a U+0020 SPACE character, and the previous
318 // value of IDL value.
319 idlValue = WTF::makeString(section, " ", idlValue);
320
321 return { field, idlValue };
322}
323
324} // namespace WebCore
325