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 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 // * CombineAndCollapseWhitespace("foo", " bar", false) -> "foo bar" | 108 // * CombineAndCollapseWhitespace("foo", " bar", false) -> "foo bar" |
109 // * CombineAndCollapseWhitespace("foo", " bar", true) -> "foo bar" | 109 // * CombineAndCollapseWhitespace("foo", " bar", true) -> "foo bar" |
110 // * CombineAndCollapseWhitespace("foo ", " bar", false) -> "foo bar" | 110 // * CombineAndCollapseWhitespace("foo ", " bar", false) -> "foo bar" |
111 // * CombineAndCollapseWhitespace(" foo", "bar ", false) -> " foobar " | 111 // * CombineAndCollapseWhitespace(" foo", "bar ", false) -> " foobar " |
112 // * CombineAndCollapseWhitespace(" foo", "bar ", true) -> " foo bar " | 112 // * CombineAndCollapseWhitespace(" foo", "bar ", true) -> " foo bar " |
113 const base::string16 CombineAndCollapseWhitespace( | 113 const base::string16 CombineAndCollapseWhitespace( |
114 const base::string16& prefix, | 114 const base::string16& prefix, |
115 const base::string16& suffix, | 115 const base::string16& suffix, |
116 bool force_whitespace) { | 116 bool force_whitespace) { |
117 base::string16 prefix_trimmed; | 117 base::string16 prefix_trimmed; |
118 TrimPositions prefix_trailing_whitespace = | 118 base::TrimPositions prefix_trailing_whitespace = |
119 TrimWhitespace(prefix, TRIM_TRAILING, &prefix_trimmed); | 119 base::TrimWhitespace(prefix, base::TRIM_TRAILING, &prefix_trimmed); |
120 | 120 |
121 // Recursively compute the children's text. | 121 // Recursively compute the children's text. |
122 base::string16 suffix_trimmed; | 122 base::string16 suffix_trimmed; |
123 TrimPositions suffix_leading_whitespace = | 123 base::TrimPositions suffix_leading_whitespace = |
124 TrimWhitespace(suffix, TRIM_LEADING, &suffix_trimmed); | 124 base::TrimWhitespace(suffix, base::TRIM_LEADING, &suffix_trimmed); |
125 | 125 |
126 if (prefix_trailing_whitespace || suffix_leading_whitespace || | 126 if (prefix_trailing_whitespace || suffix_leading_whitespace || |
127 force_whitespace) { | 127 force_whitespace) { |
128 return prefix_trimmed + base::ASCIIToUTF16(" ") + suffix_trimmed; | 128 return prefix_trimmed + base::ASCIIToUTF16(" ") + suffix_trimmed; |
129 } else { | 129 } else { |
130 return prefix_trimmed + suffix_trimmed; | 130 return prefix_trimmed + suffix_trimmed; |
131 } | 131 } |
132 } | 132 } |
133 | 133 |
134 // This is a helper function for the FindChildText() function (see below). | 134 // This is a helper function for the FindChildText() function (see below). |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 // |innerText()|, the search depth and breadth are limited to a fixed threshold. | 183 // |innerText()|, the search depth and breadth are limited to a fixed threshold. |
184 // Whitespace is trimmed from text accumulated at descendant nodes. | 184 // Whitespace is trimmed from text accumulated at descendant nodes. |
185 base::string16 FindChildText(const WebNode& node) { | 185 base::string16 FindChildText(const WebNode& node) { |
186 if (node.isTextNode()) | 186 if (node.isTextNode()) |
187 return node.nodeValue(); | 187 return node.nodeValue(); |
188 | 188 |
189 WebNode child = node.firstChild(); | 189 WebNode child = node.firstChild(); |
190 | 190 |
191 const int kChildSearchDepth = 10; | 191 const int kChildSearchDepth = 10; |
192 base::string16 node_text = FindChildTextInner(child, kChildSearchDepth); | 192 base::string16 node_text = FindChildTextInner(child, kChildSearchDepth); |
193 TrimWhitespace(node_text, TRIM_ALL, &node_text); | 193 base::TrimWhitespace(node_text, base::TRIM_ALL, &node_text); |
194 return node_text; | 194 return node_text; |
195 } | 195 } |
196 | 196 |
197 // Helper for |InferLabelForElement()| that infers a label, if possible, from | 197 // Helper for |InferLabelForElement()| that infers a label, if possible, from |
198 // a previous sibling of |element|, | 198 // a previous sibling of |element|, |
199 // e.g. Some Text <input ...> | 199 // e.g. Some Text <input ...> |
200 // or Some <span>Text</span> <input ...> | 200 // or Some <span>Text</span> <input ...> |
201 // or <p>Some Text</p><input ...> | 201 // or <p>Some Text</p><input ...> |
202 // or <label>Some Text</label> <input ...> | 202 // or <label>Some Text</label> <input ...> |
203 // or Some Text <img><input ...> | 203 // or Some Text <img><input ...> |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 // A text node's value will be empty if it is for a line break. | 235 // A text node's value will be empty if it is for a line break. |
236 bool add_space = previous.isTextNode() && value.empty(); | 236 bool add_space = previous.isTextNode() && value.empty(); |
237 inferred_label = | 237 inferred_label = |
238 CombineAndCollapseWhitespace(value, inferred_label, add_space); | 238 CombineAndCollapseWhitespace(value, inferred_label, add_space); |
239 continue; | 239 continue; |
240 } | 240 } |
241 | 241 |
242 // If we have identified a partial label and have reached a non-lightweight | 242 // If we have identified a partial label and have reached a non-lightweight |
243 // element, consider the label to be complete. | 243 // element, consider the label to be complete. |
244 base::string16 trimmed_label; | 244 base::string16 trimmed_label; |
245 TrimWhitespace(inferred_label, TRIM_ALL, &trimmed_label); | 245 base::TrimWhitespace(inferred_label, base::TRIM_ALL, &trimmed_label); |
246 if (!trimmed_label.empty()) | 246 if (!trimmed_label.empty()) |
247 break; | 247 break; |
248 | 248 |
249 // <img> and <br> tags often appear between the input element and its | 249 // <img> and <br> tags often appear between the input element and its |
250 // label text, so skip over them. | 250 // label text, so skip over them. |
251 CR_DEFINE_STATIC_LOCAL(WebString, kImage, ("img")); | 251 CR_DEFINE_STATIC_LOCAL(WebString, kImage, ("img")); |
252 CR_DEFINE_STATIC_LOCAL(WebString, kBreak, ("br")); | 252 CR_DEFINE_STATIC_LOCAL(WebString, kBreak, ("br")); |
253 if (HasTagName(previous, kImage) || HasTagName(previous, kBreak)) | 253 if (HasTagName(previous, kImage) || HasTagName(previous, kBreak)) |
254 continue; | 254 continue; |
255 | 255 |
256 // We only expect <p> and <label> tags to contain the full label text. | 256 // We only expect <p> and <label> tags to contain the full label text. |
257 CR_DEFINE_STATIC_LOCAL(WebString, kPage, ("p")); | 257 CR_DEFINE_STATIC_LOCAL(WebString, kPage, ("p")); |
258 CR_DEFINE_STATIC_LOCAL(WebString, kLabel, ("label")); | 258 CR_DEFINE_STATIC_LOCAL(WebString, kLabel, ("label")); |
259 if (HasTagName(previous, kPage) || HasTagName(previous, kLabel)) | 259 if (HasTagName(previous, kPage) || HasTagName(previous, kLabel)) |
260 inferred_label = FindChildText(previous); | 260 inferred_label = FindChildText(previous); |
261 | 261 |
262 break; | 262 break; |
263 } | 263 } |
264 | 264 |
265 TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label); | 265 base::TrimWhitespace(inferred_label, base::TRIM_ALL, &inferred_label); |
266 return inferred_label; | 266 return inferred_label; |
267 } | 267 } |
268 | 268 |
269 // Helper for |InferLabelForElement()| that infers a label, if possible, from | 269 // Helper for |InferLabelForElement()| that infers a label, if possible, from |
270 // enclosing list item, | 270 // enclosing list item, |
271 // e.g. <li>Some Text<input ...><input ...><input ...></tr> | 271 // e.g. <li>Some Text<input ...><input ...><input ...></tr> |
272 base::string16 InferLabelFromListItem(const WebFormControlElement& element) { | 272 base::string16 InferLabelFromListItem(const WebFormControlElement& element) { |
273 WebNode parent = element.parentNode(); | 273 WebNode parent = element.parentNode(); |
274 CR_DEFINE_STATIC_LOCAL(WebString, kListItem, ("li")); | 274 CR_DEFINE_STATIC_LOCAL(WebString, kListItem, ("li")); |
275 while (!parent.isNull() && parent.isElementNode() && | 275 while (!parent.isNull() && parent.isElementNode() && |
(...skipping 905 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1181 | 1181 |
1182 gfx::RectF GetScaledBoundingBox(float scale, WebInputElement* element) { | 1182 gfx::RectF GetScaledBoundingBox(float scale, WebInputElement* element) { |
1183 gfx::Rect bounding_box(element->boundsInViewportSpace()); | 1183 gfx::Rect bounding_box(element->boundsInViewportSpace()); |
1184 return gfx::RectF(bounding_box.x() * scale, | 1184 return gfx::RectF(bounding_box.x() * scale, |
1185 bounding_box.y() * scale, | 1185 bounding_box.y() * scale, |
1186 bounding_box.width() * scale, | 1186 bounding_box.width() * scale, |
1187 bounding_box.height() * scale); | 1187 bounding_box.height() * scale); |
1188 } | 1188 } |
1189 | 1189 |
1190 } // namespace autofill | 1190 } // namespace autofill |
OLD | NEW |