| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/renderer/form_manager.h" | 5 #include "chrome/renderer/form_manager.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/scoped_vector.h" | 8 #include "base/scoped_vector.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 return string16(); | 101 return string16(); |
| 102 | 102 |
| 103 if (previous.isTextNode()) { | 103 if (previous.isTextNode()) { |
| 104 inferred_label = previous.nodeValue(); | 104 inferred_label = previous.nodeValue(); |
| 105 TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label); | 105 TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label); |
| 106 } | 106 } |
| 107 | 107 |
| 108 // If we didn't find text, check for previous paragraph. | 108 // If we didn't find text, check for previous paragraph. |
| 109 // Eg. <p>Some Text</p><input ...> | 109 // Eg. <p>Some Text</p><input ...> |
| 110 // Note the lack of whitespace between <p> and <input> elements. | 110 // Note the lack of whitespace between <p> and <input> elements. |
| 111 if (inferred_label.empty()) { | 111 if (inferred_label.empty() && previous.isElementNode()) { |
| 112 if (previous.isElementNode()) { | 112 WebElement element = previous.to<WebElement>(); |
| 113 WebElement element = previous.to<WebElement>(); | 113 if (element.hasTagName("p")) { |
| 114 if (element.hasTagName("p")) { | 114 inferred_label = FindChildText(element); |
| 115 inferred_label = FindChildText(element); | |
| 116 } | |
| 117 } | 115 } |
| 118 } | 116 } |
| 119 | 117 |
| 120 // If we didn't find paragraph, check for previous paragraph to this. | 118 // If we didn't find paragraph, check for previous paragraph to this. |
| 121 // Eg. <p>Some Text</p> <input ...> | 119 // Eg. <p>Some Text</p> <input ...> |
| 122 // Note the whitespace between <p> and <input> elements. | 120 // Note the whitespace between <p> and <input> elements. |
| 123 if (inferred_label.empty()) { | 121 if (inferred_label.empty()) { |
| 124 previous = previous.previousSibling(); | 122 WebNode sibling = previous.previousSibling(); |
| 125 if (!previous.isNull() && previous.isElementNode()) { | 123 if (!sibling.isNull() && sibling.isElementNode()) { |
| 126 WebElement element = previous.to<WebElement>(); | 124 WebElement element = sibling.to<WebElement>(); |
| 127 if (element.hasTagName("p")) { | 125 if (element.hasTagName("p")) { |
| 128 inferred_label = FindChildText(element); | 126 inferred_label = FindChildText(element); |
| 129 } | 127 } |
| 130 } | 128 } |
| 131 } | 129 } |
| 132 | 130 |
| 133 // Look for text node prior to <img> tag. | 131 // Look for text node prior to <img> tag. |
| 134 // Eg. Some Text<img/><input ...> | 132 // Eg. Some Text<img/><input ...> |
| 135 if (inferred_label.empty()) { | 133 if (inferred_label.empty()) { |
| 136 while (inferred_label.empty() && !previous.isNull()) { | 134 while (inferred_label.empty() && !previous.isNull()) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 // Eg. <tr><td>Some Text</td><td><input ...></td></tr> | 176 // Eg. <tr><td>Some Text</td><td><input ...></td></tr> |
| 179 // Eg. <tr><td><b>Some Text</b></td><td><b><input ...></b></td></tr> | 177 // Eg. <tr><td><b>Some Text</b></td><td><b><input ...></b></td></tr> |
| 180 string16 InferLabelFromTable( | 178 string16 InferLabelFromTable( |
| 181 const WebFormControlElement& element) { | 179 const WebFormControlElement& element) { |
| 182 string16 inferred_label; | 180 string16 inferred_label; |
| 183 WebNode parent = element.parentNode(); | 181 WebNode parent = element.parentNode(); |
| 184 while (!parent.isNull() && parent.isElementNode() && | 182 while (!parent.isNull() && parent.isElementNode() && |
| 185 !parent.to<WebElement>().hasTagName("td")) | 183 !parent.to<WebElement>().hasTagName("td")) |
| 186 parent = parent.parentNode(); | 184 parent = parent.parentNode(); |
| 187 | 185 |
| 188 if (parent.isNull() || !parent.isElementNode()) | |
| 189 return string16(); | |
| 190 | |
| 191 WebElement e = parent.to<WebElement>(); | |
| 192 if (e.isNull() || !e.hasTagName("td")) | |
| 193 return string16(); | |
| 194 | |
| 195 // Check all previous siblings, skipping non-element nodes, until we find a | 186 // Check all previous siblings, skipping non-element nodes, until we find a |
| 196 // non-empty text block. | 187 // non-empty text block. |
| 197 WebNode previous = parent.previousSibling(); | 188 WebNode previous = parent; |
| 198 while (!previous.isNull()) { | 189 while (!previous.isNull()) { |
| 199 if (previous.isElementNode()) { | 190 if (previous.isElementNode()) { |
| 200 e = previous.to<WebElement>(); | 191 WebElement e = previous.to<WebElement>(); |
| 201 if (e.hasTagName("td")) { | 192 if (e.hasTagName("td")) { |
| 202 inferred_label = FindChildText(e); | 193 inferred_label = FindChildText(e); |
| 203 if (!inferred_label.empty()) | 194 if (!inferred_label.empty()) |
| 204 break; | 195 break; |
| 205 } | 196 } |
| 206 } | 197 } |
| 207 | 198 |
| 208 previous = previous.previousSibling(); | 199 previous = previous.previousSibling(); |
| 209 } | 200 } |
| 210 | 201 |
| (...skipping 694 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 905 WebInputElement input_element = field->to<WebInputElement>(); | 896 WebInputElement input_element = field->to<WebInputElement>(); |
| 906 | 897 |
| 907 // If the maxlength attribute contains a negative value, maxLength() | 898 // If the maxlength attribute contains a negative value, maxLength() |
| 908 // returns the default maxlength value. | 899 // returns the default maxlength value. |
| 909 input_element.setSuggestedValue( | 900 input_element.setSuggestedValue( |
| 910 data->value().substr(0, input_element.maxLength())); | 901 data->value().substr(0, input_element.maxLength())); |
| 911 input_element.setAutofilled(true); | 902 input_element.setAutofilled(true); |
| 912 if (is_initiating_node) | 903 if (is_initiating_node) |
| 913 input_element.setSelectionRange(0, input_element.suggestedValue().length()); | 904 input_element.setSelectionRange(0, input_element.suggestedValue().length()); |
| 914 } | 905 } |
| OLD | NEW |