| 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/content/renderer/form_autofill_util.h" | 5 #include "components/autofill/content/renderer/form_autofill_util.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 if (node.isElementNode()) { | 222 if (node.isElementNode()) { |
| 223 const WebElement element = node.toConst<WebElement>(); | 223 const WebElement element = node.toConst<WebElement>(); |
| 224 if (IsOptionElement(element) || | 224 if (IsOptionElement(element) || |
| 225 IsScriptElement(element) || | 225 IsScriptElement(element) || |
| 226 IsNoScriptElement(element) || | 226 IsNoScriptElement(element) || |
| 227 (element.isFormControlElement() && | 227 (element.isFormControlElement() && |
| 228 IsAutofillableElement(element.toConst<WebFormControlElement>()))) { | 228 IsAutofillableElement(element.toConst<WebFormControlElement>()))) { |
| 229 return base::string16(); | 229 return base::string16(); |
| 230 } | 230 } |
| 231 | 231 |
| 232 if (element.hasHTMLTagName("div") && ContainsKey(divs_to_skip, node)) | 232 if (element.hasHTMLTagName("div") && base::ContainsKey(divs_to_skip, node)) |
| 233 return base::string16(); | 233 return base::string16(); |
| 234 } | 234 } |
| 235 | 235 |
| 236 // Extract the text exactly at this node. | 236 // Extract the text exactly at this node. |
| 237 base::string16 node_text = node.nodeValue(); | 237 base::string16 node_text = node.nodeValue(); |
| 238 | 238 |
| 239 // Recursively compute the children's text. | 239 // Recursively compute the children's text. |
| 240 // Preserve inter-element whitespace separation. | 240 // Preserve inter-element whitespace separation. |
| 241 base::string16 child_text = | 241 base::string16 child_text = |
| 242 FindChildTextInner(node.firstChild(), depth - 1, divs_to_skip); | 242 FindChildTextInner(node.firstChild(), depth - 1, divs_to_skip); |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 668 continue; | 668 continue; |
| 669 | 669 |
| 670 tag_names.push_back(parent_node.to<WebElement>().tagName().utf8()); | 670 tag_names.push_back(parent_node.to<WebElement>().tagName().utf8()); |
| 671 } | 671 } |
| 672 return tag_names; | 672 return tag_names; |
| 673 } | 673 } |
| 674 | 674 |
| 675 bool IsLabelValid(base::StringPiece16 inferred_label, | 675 bool IsLabelValid(base::StringPiece16 inferred_label, |
| 676 const std::vector<base::char16>& stop_words) { | 676 const std::vector<base::char16>& stop_words) { |
| 677 // If |inferred_label| has any character other than those in |stop_words|. | 677 // If |inferred_label| has any character other than those in |stop_words|. |
| 678 auto* first_non_stop_word = std::find_if( | 678 auto* first_non_stop_word = |
| 679 inferred_label.begin(), inferred_label.end(), | 679 std::find_if(inferred_label.begin(), inferred_label.end(), |
| 680 [&stop_words](base::char16 c) { return !ContainsValue(stop_words, c); }); | 680 [&stop_words](base::char16 c) { |
| 681 return !base::ContainsValue(stop_words, c); |
| 682 }); |
| 681 return first_non_stop_word != inferred_label.end(); | 683 return first_non_stop_word != inferred_label.end(); |
| 682 } | 684 } |
| 683 | 685 |
| 684 // Infers corresponding label for |element| from surrounding context in the DOM, | 686 // Infers corresponding label for |element| from surrounding context in the DOM, |
| 685 // e.g. the contents of the preceding <p> tag or text element. | 687 // e.g. the contents of the preceding <p> tag or text element. |
| 686 base::string16 InferLabelForElement(const WebFormControlElement& element, | 688 base::string16 InferLabelForElement(const WebFormControlElement& element, |
| 687 const std::vector<base::char16>& stop_words) { | 689 const std::vector<base::char16>& stop_words) { |
| 688 base::string16 inferred_label; | 690 base::string16 inferred_label; |
| 689 | 691 |
| 690 if (IsCheckableElement(toWebInputElement(&element))) { | 692 if (IsCheckableElement(toWebInputElement(&element))) { |
| 691 inferred_label = InferLabelFromNext(element); | 693 inferred_label = InferLabelFromNext(element); |
| 692 if (IsLabelValid(inferred_label, stop_words)) | 694 if (IsLabelValid(inferred_label, stop_words)) |
| 693 return inferred_label; | 695 return inferred_label; |
| 694 } | 696 } |
| 695 | 697 |
| 696 inferred_label = InferLabelFromPrevious(element); | 698 inferred_label = InferLabelFromPrevious(element); |
| 697 if (IsLabelValid(inferred_label, stop_words)) | 699 if (IsLabelValid(inferred_label, stop_words)) |
| 698 return inferred_label; | 700 return inferred_label; |
| 699 | 701 |
| 700 // If we didn't find a label, check for placeholder text. | 702 // If we didn't find a label, check for placeholder text. |
| 701 inferred_label = InferLabelFromPlaceholder(element); | 703 inferred_label = InferLabelFromPlaceholder(element); |
| 702 if (IsLabelValid(inferred_label, stop_words)) | 704 if (IsLabelValid(inferred_label, stop_words)) |
| 703 return inferred_label; | 705 return inferred_label; |
| 704 | 706 |
| 705 // For all other searches that involve traversing up the tree, the search | 707 // For all other searches that involve traversing up the tree, the search |
| 706 // order is based on which tag is the closest ancestor to |element|. | 708 // order is based on which tag is the closest ancestor to |element|. |
| 707 std::vector<std::string> tag_names = AncestorTagNames(element); | 709 std::vector<std::string> tag_names = AncestorTagNames(element); |
| 708 std::set<std::string> seen_tag_names; | 710 std::set<std::string> seen_tag_names; |
| 709 for (const std::string& tag_name : tag_names) { | 711 for (const std::string& tag_name : tag_names) { |
| 710 if (ContainsKey(seen_tag_names, tag_name)) | 712 if (base::ContainsKey(seen_tag_names, tag_name)) |
| 711 continue; | 713 continue; |
| 712 | 714 |
| 713 seen_tag_names.insert(tag_name); | 715 seen_tag_names.insert(tag_name); |
| 714 if (tag_name == "LABEL") { | 716 if (tag_name == "LABEL") { |
| 715 inferred_label = InferLabelFromEnclosingLabel(element); | 717 inferred_label = InferLabelFromEnclosingLabel(element); |
| 716 } else if (tag_name == "DIV") { | 718 } else if (tag_name == "DIV") { |
| 717 inferred_label = InferLabelFromDivTable(element); | 719 inferred_label = InferLabelFromDivTable(element); |
| 718 } else if (tag_name == "TD") { | 720 } else if (tag_name == "TD") { |
| 719 inferred_label = InferLabelFromTableColumn(element); | 721 inferred_label = InferLabelFromTableColumn(element); |
| 720 if (!IsLabelValid(inferred_label, stop_words)) | 722 if (!IsLabelValid(inferred_label, stop_words)) |
| (...skipping 1088 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1809 // Zero selection start is for password manager, which can show usernames | 1811 // Zero selection start is for password manager, which can show usernames |
| 1810 // that do not begin with the user input value. | 1812 // that do not begin with the user input value. |
| 1811 selection_start = (offset == base::string16::npos) ? 0 : offset; | 1813 selection_start = (offset == base::string16::npos) ? 0 : offset; |
| 1812 } | 1814 } |
| 1813 | 1815 |
| 1814 input_element->setSelectionRange(selection_start, suggestion.length()); | 1816 input_element->setSelectionRange(selection_start, suggestion.length()); |
| 1815 } | 1817 } |
| 1816 | 1818 |
| 1817 } // namespace form_util | 1819 } // namespace form_util |
| 1818 } // namespace autofill | 1820 } // namespace autofill |
| OLD | NEW |