| OLD | NEW |
| 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/form_structure.h" | 5 #include "components/autofill/core/browser/form_structure.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 879 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 890 // To prevent potential section name collisions, add a default suffix for | 890 // To prevent potential section name collisions, add a default suffix for |
| 891 // other fields. Without this, 'autocomplete' attribute values | 891 // other fields. Without this, 'autocomplete' attribute values |
| 892 // "section--shipping street-address" and "shipping street-address" would be | 892 // "section--shipping street-address" and "shipping street-address" would be |
| 893 // parsed identically, given the section handling code below. We do this | 893 // parsed identically, given the section handling code below. We do this |
| 894 // before any validation so that fields with invalid attributes still end up | 894 // before any validation so that fields with invalid attributes still end up |
| 895 // in the default section. These default section names will be overridden | 895 // in the default section. These default section names will be overridden |
| 896 // by subsequent heuristic parsing steps if there are no author-specified | 896 // by subsequent heuristic parsing steps if there are no author-specified |
| 897 // section names. | 897 // section names. |
| 898 field->set_section(kDefaultSection); | 898 field->set_section(kDefaultSection); |
| 899 | 899 |
| 900 // Canonicalize the attribute value by trimming whitespace, collapsing | 900 std::vector<std::string> tokens = |
| 901 // non-space characters (e.g. tab) to spaces, and converting to lowercase. | 901 LowercaseAndTokenizeAttributeString(field->autocomplete_attribute); |
| 902 std::string autocomplete_attribute = | |
| 903 base::CollapseWhitespaceASCII(field->autocomplete_attribute, false); | |
| 904 autocomplete_attribute = base::ToLowerASCII(autocomplete_attribute); | |
| 905 | 902 |
| 906 // The autocomplete attribute is overloaded: it can specify either a field | 903 // The autocomplete attribute is overloaded: it can specify either a field |
| 907 // type hint or whether autocomplete should be enabled at all. Ignore the | 904 // type hint or whether autocomplete should be enabled at all. Ignore the |
| 908 // latter type of attribute value. | 905 // latter type of attribute value. |
| 909 if (autocomplete_attribute.empty() || autocomplete_attribute == "on" || | 906 if (tokens.empty() || |
| 910 autocomplete_attribute == "off") { | 907 (tokens.size() == 1 && (tokens[0] == "on" || tokens[0] == "off"))) { |
| 911 continue; | 908 continue; |
| 912 } | 909 } |
| 913 | 910 |
| 914 // Any other value, even it is invalid, is considered to be a type hint. | 911 // Any other value, even it is invalid, is considered to be a type hint. |
| 915 // This allows a website's author to specify an attribute like | 912 // This allows a website's author to specify an attribute like |
| 916 // autocomplete="other" on a field to disable all Autofill heuristics for | 913 // autocomplete="other" on a field to disable all Autofill heuristics for |
| 917 // the form. | 914 // the form. |
| 918 has_author_specified_types_ = true; | 915 has_author_specified_types_ = true; |
| 919 | 916 |
| 920 // Tokenize the attribute value. Per the spec, the tokens are parsed in | |
| 921 // reverse order. | |
| 922 std::vector<std::string> tokens = | |
| 923 base::SplitString(autocomplete_attribute, " ", base::KEEP_WHITESPACE, | |
| 924 base::SPLIT_WANT_NONEMPTY); | |
| 925 | |
| 926 // The final token must be the field type. | 917 // The final token must be the field type. |
| 927 // If it is not one of the known types, abort. | 918 // If it is not one of the known types, abort. |
| 928 DCHECK(!tokens.empty()); | 919 DCHECK(!tokens.empty()); |
| 920 |
| 921 // Per the spec, the tokens are parsed in reverse order. |
| 929 std::string field_type_token = tokens.back(); | 922 std::string field_type_token = tokens.back(); |
| 930 tokens.pop_back(); | 923 tokens.pop_back(); |
| 931 HtmlFieldType field_type = | 924 HtmlFieldType field_type = |
| 932 FieldTypeFromAutocompleteAttributeValue(field_type_token, *field); | 925 FieldTypeFromAutocompleteAttributeValue(field_type_token, *field); |
| 933 if (field_type == HTML_TYPE_UNSPECIFIED) | 926 if (field_type == HTML_TYPE_UNSPECIFIED) |
| 934 continue; | 927 continue; |
| 935 | 928 |
| 936 // The preceding token, if any, may be a type hint. | 929 // The preceding token, if any, may be a type hint. |
| 937 if (!tokens.empty() && IsContactTypeHint(tokens.back())) { | 930 if (!tokens.empty() && IsContactTypeHint(tokens.back())) { |
| 938 // If it is, it must match the field type; otherwise, abort. | 931 // If it is, it must match the field type; otherwise, abort. |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1322 filtered_strings[0].at(prefix_len)) { | 1315 filtered_strings[0].at(prefix_len)) { |
| 1323 // Mismatch found. | 1316 // Mismatch found. |
| 1324 return filtered_strings[i].substr(0, prefix_len); | 1317 return filtered_strings[i].substr(0, prefix_len); |
| 1325 } | 1318 } |
| 1326 } | 1319 } |
| 1327 } | 1320 } |
| 1328 return filtered_strings[0]; | 1321 return filtered_strings[0]; |
| 1329 } | 1322 } |
| 1330 | 1323 |
| 1331 } // namespace autofill | 1324 } // namespace autofill |
| OLD | NEW |