| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/autofill/renderer/form_autofill_util.h" | |
| 6 | |
| 7 #include <map> | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/scoped_vector.h" | |
| 12 #include "base/metrics/field_trial.h" | |
| 13 #include "base/string_util.h" | |
| 14 #include "base/utf_string_conversions.h" | |
| 15 #include "components/autofill/common/autofill_switches.h" | |
| 16 #include "components/autofill/common/form_data.h" | |
| 17 #include "components/autofill/common/form_field_data.h" | |
| 18 #include "components/autofill/common/web_element_descriptor.h" | |
| 19 #include "third_party/WebKit/public/platform/WebString.h" | |
| 20 #include "third_party/WebKit/public/platform/WebVector.h" | |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebExceptionCode.h" | |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement
.h" | |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" | |
| 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
| 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | |
| 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebLabelElement.h" | |
| 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | |
| 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeList.h" | |
| 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebOptionElement.h" | |
| 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSelectElement.h" | |
| 33 | |
| 34 using WebKit::WebDocument; | |
| 35 using WebKit::WebElement; | |
| 36 using WebKit::WebExceptionCode; | |
| 37 using WebKit::WebFormControlElement; | |
| 38 using WebKit::WebFormElement; | |
| 39 using WebKit::WebFrame; | |
| 40 using WebKit::WebInputElement; | |
| 41 using WebKit::WebLabelElement; | |
| 42 using WebKit::WebNode; | |
| 43 using WebKit::WebNodeList; | |
| 44 using WebKit::WebOptionElement; | |
| 45 using WebKit::WebSelectElement; | |
| 46 using WebKit::WebString; | |
| 47 using WebKit::WebVector; | |
| 48 | |
| 49 namespace autofill { | |
| 50 namespace { | |
| 51 | |
| 52 // The maximum length allowed for form data. | |
| 53 const size_t kMaxDataLength = 1024; | |
| 54 | |
| 55 bool IsOptionElement(const WebElement& element) { | |
| 56 CR_DEFINE_STATIC_LOCAL(WebString, kOption, ("option")); | |
| 57 return element.hasTagName(kOption); | |
| 58 } | |
| 59 | |
| 60 bool IsScriptElement(const WebElement& element) { | |
| 61 CR_DEFINE_STATIC_LOCAL(WebString, kScript, ("script")); | |
| 62 return element.hasTagName(kScript); | |
| 63 } | |
| 64 | |
| 65 bool IsNoScriptElement(const WebElement& element) { | |
| 66 CR_DEFINE_STATIC_LOCAL(WebString, kNoScript, ("noscript")); | |
| 67 return element.hasTagName(kNoScript); | |
| 68 } | |
| 69 | |
| 70 bool HasTagName(const WebNode& node, const WebKit::WebString& tag) { | |
| 71 return node.isElementNode() && node.toConst<WebElement>().hasTagName(tag); | |
| 72 } | |
| 73 | |
| 74 bool IsAutofillableElement(const WebFormControlElement& element) { | |
| 75 const WebInputElement* input_element = toWebInputElement(&element); | |
| 76 return IsAutofillableInputElement(input_element) || IsSelectElement(element); | |
| 77 } | |
| 78 | |
| 79 bool IsAutocheckoutEnabled() { | |
| 80 return base::FieldTrialList::FindFullName("Autocheckout") == "Yes" || | |
| 81 CommandLine::ForCurrentProcess()->HasSwitch( | |
| 82 switches::kEnableExperimentalFormFilling); | |
| 83 } | |
| 84 | |
| 85 // Check whether the given field satisfies the REQUIRE_AUTOCOMPLETE requirement. | |
| 86 // When Autocheckout is enabled, this requirement is enforced in the browser | |
| 87 // process rather than in the renderer process, and hence all fields are | |
| 88 // considered to satisfy this requirement. | |
| 89 bool SatisfiesRequireAutocomplete(const WebInputElement& input_element) { | |
| 90 return input_element.autoComplete() || IsAutocheckoutEnabled(); | |
| 91 } | |
| 92 | |
| 93 // Appends |suffix| to |prefix| so that any intermediary whitespace is collapsed | |
| 94 // to a single space. If |force_whitespace| is true, then the resulting string | |
| 95 // is guaranteed to have a space between |prefix| and |suffix|. Otherwise, the | |
| 96 // result includes a space only if |prefix| has trailing whitespace or |suffix| | |
| 97 // has leading whitespace. | |
| 98 // A few examples: | |
| 99 // * CombineAndCollapseWhitespace("foo", "bar", false) -> "foobar" | |
| 100 // * CombineAndCollapseWhitespace("foo", "bar", true) -> "foo bar" | |
| 101 // * CombineAndCollapseWhitespace("foo ", "bar", false) -> "foo bar" | |
| 102 // * CombineAndCollapseWhitespace("foo", " bar", false) -> "foo bar" | |
| 103 // * CombineAndCollapseWhitespace("foo", " bar", true) -> "foo bar" | |
| 104 // * CombineAndCollapseWhitespace("foo ", " bar", false) -> "foo bar" | |
| 105 // * CombineAndCollapseWhitespace(" foo", "bar ", false) -> " foobar " | |
| 106 // * CombineAndCollapseWhitespace(" foo", "bar ", true) -> " foo bar " | |
| 107 const base::string16 CombineAndCollapseWhitespace( | |
| 108 const base::string16& prefix, | |
| 109 const base::string16& suffix, | |
| 110 bool force_whitespace) { | |
| 111 base::string16 prefix_trimmed; | |
| 112 TrimPositions prefix_trailing_whitespace = | |
| 113 TrimWhitespace(prefix, TRIM_TRAILING, &prefix_trimmed); | |
| 114 | |
| 115 // Recursively compute the children's text. | |
| 116 base::string16 suffix_trimmed; | |
| 117 TrimPositions suffix_leading_whitespace = | |
| 118 TrimWhitespace(suffix, TRIM_LEADING, &suffix_trimmed); | |
| 119 | |
| 120 if (prefix_trailing_whitespace || suffix_leading_whitespace || | |
| 121 force_whitespace) { | |
| 122 return prefix_trimmed + ASCIIToUTF16(" ") + suffix_trimmed; | |
| 123 } else { | |
| 124 return prefix_trimmed + suffix_trimmed; | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 // This is a helper function for the FindChildText() function (see below). | |
| 129 // Search depth is limited with the |depth| parameter. | |
| 130 base::string16 FindChildTextInner(const WebNode& node, int depth) { | |
| 131 if (depth <= 0 || node.isNull()) | |
| 132 return base::string16(); | |
| 133 | |
| 134 // Skip over comments. | |
| 135 if (node.nodeType() == WebNode::CommentNode) | |
| 136 return FindChildTextInner(node.nextSibling(), depth - 1); | |
| 137 | |
| 138 if (node.nodeType() != WebNode::ElementNode && | |
| 139 node.nodeType() != WebNode::TextNode) | |
| 140 return base::string16(); | |
| 141 | |
| 142 // Ignore elements known not to contain inferable labels. | |
| 143 if (node.isElementNode()) { | |
| 144 const WebElement element = node.toConst<WebElement>(); | |
| 145 if (IsOptionElement(element) || | |
| 146 IsScriptElement(element) || | |
| 147 IsNoScriptElement(element) || | |
| 148 (element.isFormControlElement() && | |
| 149 IsAutofillableElement(element.toConst<WebFormControlElement>()))) { | |
| 150 return base::string16(); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 // Extract the text exactly at this node. | |
| 155 base::string16 node_text = node.nodeValue(); | |
| 156 | |
| 157 // Recursively compute the children's text. | |
| 158 // Preserve inter-element whitespace separation. | |
| 159 base::string16 child_text = FindChildTextInner(node.firstChild(), depth - 1); | |
| 160 bool add_space = node.nodeType() == WebNode::TextNode && node_text.empty(); | |
| 161 node_text = CombineAndCollapseWhitespace(node_text, child_text, add_space); | |
| 162 | |
| 163 // Recursively compute the siblings' text. | |
| 164 // Again, preserve inter-element whitespace separation. | |
| 165 base::string16 sibling_text = | |
| 166 FindChildTextInner(node.nextSibling(), depth - 1); | |
| 167 add_space = node.nodeType() == WebNode::TextNode && node_text.empty(); | |
| 168 node_text = CombineAndCollapseWhitespace(node_text, sibling_text, add_space); | |
| 169 | |
| 170 return node_text; | |
| 171 } | |
| 172 | |
| 173 // Returns the aggregated values of the descendants of |element| that are | |
| 174 // non-empty text nodes. This is a faster alternative to |innerText()| for | |
| 175 // performance critical operations. It does a full depth-first search so can be | |
| 176 // used when the structure is not directly known. However, unlike with | |
| 177 // |innerText()|, the search depth and breadth are limited to a fixed threshold. | |
| 178 // Whitespace is trimmed from text accumulated at descendant nodes. | |
| 179 base::string16 FindChildText(const WebNode& node) { | |
| 180 if (node.isTextNode()) | |
| 181 return node.nodeValue(); | |
| 182 | |
| 183 WebNode child = node.firstChild(); | |
| 184 | |
| 185 const int kChildSearchDepth = 10; | |
| 186 base::string16 node_text = FindChildTextInner(child, kChildSearchDepth); | |
| 187 TrimWhitespace(node_text, TRIM_ALL, &node_text); | |
| 188 return node_text; | |
| 189 } | |
| 190 | |
| 191 // Helper for |InferLabelForElement()| that infers a label, if possible, from | |
| 192 // a previous sibling of |element|, | |
| 193 // e.g. Some Text <input ...> | |
| 194 // or Some <span>Text</span> <input ...> | |
| 195 // or <p>Some Text</p><input ...> | |
| 196 // or <label>Some Text</label> <input ...> | |
| 197 // or Some Text <img><input ...> | |
| 198 // or <b>Some Text</b><br/> <input ...>. | |
| 199 base::string16 InferLabelFromPrevious(const WebFormControlElement& element) { | |
| 200 base::string16 inferred_label; | |
| 201 WebNode previous = element; | |
| 202 while (true) { | |
| 203 previous = previous.previousSibling(); | |
| 204 if (previous.isNull()) | |
| 205 break; | |
| 206 | |
| 207 // Skip over comments. | |
| 208 WebNode::NodeType node_type = previous.nodeType(); | |
| 209 if (node_type == WebNode::CommentNode) | |
| 210 continue; | |
| 211 | |
| 212 // Otherwise, only consider normal HTML elements and their contents. | |
| 213 if (node_type != WebNode::TextNode && | |
| 214 node_type != WebNode::ElementNode) | |
| 215 break; | |
| 216 | |
| 217 // A label might be split across multiple "lightweight" nodes. | |
| 218 // Coalesce any text contained in multiple consecutive | |
| 219 // (a) plain text nodes or | |
| 220 // (b) inline HTML elements that are essentially equivalent to text nodes. | |
| 221 CR_DEFINE_STATIC_LOCAL(WebString, kBold, ("b")); | |
| 222 CR_DEFINE_STATIC_LOCAL(WebString, kStrong, ("strong")); | |
| 223 CR_DEFINE_STATIC_LOCAL(WebString, kSpan, ("span")); | |
| 224 CR_DEFINE_STATIC_LOCAL(WebString, kFont, ("font")); | |
| 225 if (previous.isTextNode() || | |
| 226 HasTagName(previous, kBold) || HasTagName(previous, kStrong) || | |
| 227 HasTagName(previous, kSpan) || HasTagName(previous, kFont)) { | |
| 228 base::string16 value = FindChildText(previous); | |
| 229 // A text node's value will be empty if it is for a line break. | |
| 230 bool add_space = previous.isTextNode() && value.empty(); | |
| 231 inferred_label = | |
| 232 CombineAndCollapseWhitespace(value, inferred_label, add_space); | |
| 233 continue; | |
| 234 } | |
| 235 | |
| 236 // If we have identified a partial label and have reached a non-lightweight | |
| 237 // element, consider the label to be complete. | |
| 238 base::string16 trimmed_label; | |
| 239 TrimWhitespace(inferred_label, TRIM_ALL, &trimmed_label); | |
| 240 if (!trimmed_label.empty()) | |
| 241 break; | |
| 242 | |
| 243 // <img> and <br> tags often appear between the input element and its | |
| 244 // label text, so skip over them. | |
| 245 CR_DEFINE_STATIC_LOCAL(WebString, kImage, ("img")); | |
| 246 CR_DEFINE_STATIC_LOCAL(WebString, kBreak, ("br")); | |
| 247 if (HasTagName(previous, kImage) || HasTagName(previous, kBreak)) | |
| 248 continue; | |
| 249 | |
| 250 // We only expect <p> and <label> tags to contain the full label text. | |
| 251 CR_DEFINE_STATIC_LOCAL(WebString, kPage, ("p")); | |
| 252 CR_DEFINE_STATIC_LOCAL(WebString, kLabel, ("label")); | |
| 253 if (HasTagName(previous, kPage) || HasTagName(previous, kLabel)) | |
| 254 inferred_label = FindChildText(previous); | |
| 255 | |
| 256 break; | |
| 257 } | |
| 258 | |
| 259 TrimWhitespace(inferred_label, TRIM_ALL, &inferred_label); | |
| 260 return inferred_label; | |
| 261 } | |
| 262 | |
| 263 // Helper for |InferLabelForElement()| that infers a label, if possible, from | |
| 264 // enclosing list item, | |
| 265 // e.g. <li>Some Text<input ...><input ...><input ...></tr> | |
| 266 base::string16 InferLabelFromListItem(const WebFormControlElement& element) { | |
| 267 WebNode parent = element.parentNode(); | |
| 268 CR_DEFINE_STATIC_LOCAL(WebString, kListItem, ("li")); | |
| 269 while (!parent.isNull() && parent.isElementNode() && | |
| 270 !parent.to<WebElement>().hasTagName(kListItem)) { | |
| 271 parent = parent.parentNode(); | |
| 272 } | |
| 273 | |
| 274 if (!parent.isNull() && HasTagName(parent, kListItem)) | |
| 275 return FindChildText(parent); | |
| 276 | |
| 277 return base::string16(); | |
| 278 } | |
| 279 | |
| 280 // Helper for |InferLabelForElement()| that infers a label, if possible, from | |
| 281 // surrounding table structure, | |
| 282 // e.g. <tr><td>Some Text</td><td><input ...></td></tr> | |
| 283 // or <tr><th>Some Text</th><td><input ...></td></tr> | |
| 284 // or <tr><td><b>Some Text</b></td><td><b><input ...></b></td></tr> | |
| 285 // or <tr><th><b>Some Text</b></th><td><b><input ...></b></td></tr> | |
| 286 base::string16 InferLabelFromTableColumn(const WebFormControlElement& element) { | |
| 287 CR_DEFINE_STATIC_LOCAL(WebString, kTableCell, ("td")); | |
| 288 WebNode parent = element.parentNode(); | |
| 289 while (!parent.isNull() && parent.isElementNode() && | |
| 290 !parent.to<WebElement>().hasTagName(kTableCell)) { | |
| 291 parent = parent.parentNode(); | |
| 292 } | |
| 293 | |
| 294 if (parent.isNull()) | |
| 295 return base::string16(); | |
| 296 | |
| 297 // Check all previous siblings, skipping non-element nodes, until we find a | |
| 298 // non-empty text block. | |
| 299 base::string16 inferred_label; | |
| 300 WebNode previous = parent.previousSibling(); | |
| 301 CR_DEFINE_STATIC_LOCAL(WebString, kTableHeader, ("th")); | |
| 302 while (inferred_label.empty() && !previous.isNull()) { | |
| 303 if (HasTagName(previous, kTableCell) || HasTagName(previous, kTableHeader)) | |
| 304 inferred_label = FindChildText(previous); | |
| 305 | |
| 306 previous = previous.previousSibling(); | |
| 307 } | |
| 308 | |
| 309 return inferred_label; | |
| 310 } | |
| 311 | |
| 312 // Helper for |InferLabelForElement()| that infers a label, if possible, from | |
| 313 // surrounding table structure, | |
| 314 // e.g. <tr><td>Some Text</td></tr><tr><td><input ...></td></tr> | |
| 315 base::string16 InferLabelFromTableRow(const WebFormControlElement& element) { | |
| 316 CR_DEFINE_STATIC_LOCAL(WebString, kTableRow, ("tr")); | |
| 317 WebNode parent = element.parentNode(); | |
| 318 while (!parent.isNull() && parent.isElementNode() && | |
| 319 !parent.to<WebElement>().hasTagName(kTableRow)) { | |
| 320 parent = parent.parentNode(); | |
| 321 } | |
| 322 | |
| 323 if (parent.isNull()) | |
| 324 return base::string16(); | |
| 325 | |
| 326 // Check all previous siblings, skipping non-element nodes, until we find a | |
| 327 // non-empty text block. | |
| 328 base::string16 inferred_label; | |
| 329 WebNode previous = parent.previousSibling(); | |
| 330 while (inferred_label.empty() && !previous.isNull()) { | |
| 331 if (HasTagName(previous, kTableRow)) | |
| 332 inferred_label = FindChildText(previous); | |
| 333 | |
| 334 previous = previous.previousSibling(); | |
| 335 } | |
| 336 | |
| 337 return inferred_label; | |
| 338 } | |
| 339 | |
| 340 // Helper for |InferLabelForElement()| that infers a label, if possible, from | |
| 341 // a surrounding div table, | |
| 342 // e.g. <div>Some Text<span><input ...></span></div> | |
| 343 // e.g. <div>Some Text</div><div><input ...></div> | |
| 344 base::string16 InferLabelFromDivTable(const WebFormControlElement& element) { | |
| 345 WebNode node = element.parentNode(); | |
| 346 bool looking_for_parent = true; | |
| 347 | |
| 348 // Search the sibling and parent <div>s until we find a candidate label. | |
| 349 base::string16 inferred_label; | |
| 350 CR_DEFINE_STATIC_LOCAL(WebString, kDiv, ("div")); | |
| 351 CR_DEFINE_STATIC_LOCAL(WebString, kTable, ("table")); | |
| 352 CR_DEFINE_STATIC_LOCAL(WebString, kFieldSet, ("fieldset")); | |
| 353 while (inferred_label.empty() && !node.isNull()) { | |
| 354 if (HasTagName(node, kDiv)) { | |
| 355 looking_for_parent = false; | |
| 356 inferred_label = FindChildText(node); | |
| 357 } else if (looking_for_parent && | |
| 358 (HasTagName(node, kTable) || HasTagName(node, kFieldSet))) { | |
| 359 // If the element is in a table or fieldset, its label most likely is too. | |
| 360 break; | |
| 361 } | |
| 362 | |
| 363 if (node.previousSibling().isNull()) { | |
| 364 // If there are no more siblings, continue walking up the tree. | |
| 365 looking_for_parent = true; | |
| 366 } | |
| 367 | |
| 368 if (looking_for_parent) | |
| 369 node = node.parentNode(); | |
| 370 else | |
| 371 node = node.previousSibling(); | |
| 372 } | |
| 373 | |
| 374 return inferred_label; | |
| 375 } | |
| 376 | |
| 377 // Helper for |InferLabelForElement()| that infers a label, if possible, from | |
| 378 // a surrounding definition list, | |
| 379 // e.g. <dl><dt>Some Text</dt><dd><input ...></dd></dl> | |
| 380 // e.g. <dl><dt><b>Some Text</b></dt><dd><b><input ...></b></dd></dl> | |
| 381 base::string16 InferLabelFromDefinitionList( | |
| 382 const WebFormControlElement& element) { | |
| 383 CR_DEFINE_STATIC_LOCAL(WebString, kDefinitionData, ("dd")); | |
| 384 WebNode parent = element.parentNode(); | |
| 385 while (!parent.isNull() && parent.isElementNode() && | |
| 386 !parent.to<WebElement>().hasTagName(kDefinitionData)) | |
| 387 parent = parent.parentNode(); | |
| 388 | |
| 389 if (parent.isNull() || !HasTagName(parent, kDefinitionData)) | |
| 390 return base::string16(); | |
| 391 | |
| 392 // Skip by any intervening text nodes. | |
| 393 WebNode previous = parent.previousSibling(); | |
| 394 while (!previous.isNull() && previous.isTextNode()) | |
| 395 previous = previous.previousSibling(); | |
| 396 | |
| 397 CR_DEFINE_STATIC_LOCAL(WebString, kDefinitionTag, ("dt")); | |
| 398 if (previous.isNull() || !HasTagName(previous, kDefinitionTag)) | |
| 399 return base::string16(); | |
| 400 | |
| 401 return FindChildText(previous); | |
| 402 } | |
| 403 | |
| 404 // Infers corresponding label for |element| from surrounding context in the DOM, | |
| 405 // e.g. the contents of the preceding <p> tag or text element. | |
| 406 base::string16 InferLabelForElement(const WebFormControlElement& element) { | |
| 407 base::string16 inferred_label = InferLabelFromPrevious(element); | |
| 408 if (!inferred_label.empty()) | |
| 409 return inferred_label; | |
| 410 | |
| 411 // If we didn't find a label, check for list item case. | |
| 412 inferred_label = InferLabelFromListItem(element); | |
| 413 if (!inferred_label.empty()) | |
| 414 return inferred_label; | |
| 415 | |
| 416 // If we didn't find a label, check for table cell case. | |
| 417 inferred_label = InferLabelFromTableColumn(element); | |
| 418 if (!inferred_label.empty()) | |
| 419 return inferred_label; | |
| 420 | |
| 421 // If we didn't find a label, check for table row case. | |
| 422 inferred_label = InferLabelFromTableRow(element); | |
| 423 if (!inferred_label.empty()) | |
| 424 return inferred_label; | |
| 425 | |
| 426 // If we didn't find a label, check for definition list case. | |
| 427 inferred_label = InferLabelFromDefinitionList(element); | |
| 428 if (!inferred_label.empty()) | |
| 429 return inferred_label; | |
| 430 | |
| 431 // If we didn't find a label, check for div table case. | |
| 432 return InferLabelFromDivTable(element); | |
| 433 } | |
| 434 | |
| 435 // Fills |option_strings| with the values of the <option> elements present in | |
| 436 // |select_element|. | |
| 437 void GetOptionStringsFromElement(const WebSelectElement& select_element, | |
| 438 std::vector<base::string16>* option_values, | |
| 439 std::vector<base::string16>* option_contents) { | |
| 440 DCHECK(!select_element.isNull()); | |
| 441 | |
| 442 option_values->clear(); | |
| 443 option_contents->clear(); | |
| 444 WebVector<WebElement> list_items = select_element.listItems(); | |
| 445 option_values->reserve(list_items.size()); | |
| 446 option_contents->reserve(list_items.size()); | |
| 447 for (size_t i = 0; i < list_items.size(); ++i) { | |
| 448 if (IsOptionElement(list_items[i])) { | |
| 449 const WebOptionElement option = list_items[i].toConst<WebOptionElement>(); | |
| 450 option_values->push_back(option.value()); | |
| 451 option_contents->push_back(option.text()); | |
| 452 } | |
| 453 } | |
| 454 } | |
| 455 | |
| 456 // The callback type used by |ForEachMatchingFormField()|. | |
| 457 typedef void (*Callback)(const FormFieldData&, | |
| 458 bool, /* is_initiating_element */ | |
| 459 WebKit::WebFormControlElement*); | |
| 460 | |
| 461 // For each autofillable field in |data| that matches a field in the |form|, | |
| 462 // the |callback| is invoked with the corresponding |form| field data. | |
| 463 void ForEachMatchingFormField(const WebFormElement& form_element, | |
| 464 const WebElement& initiating_element, | |
| 465 const FormData& data, | |
| 466 bool only_focusable_elements, | |
| 467 bool force_override, | |
| 468 Callback callback) { | |
| 469 std::vector<WebFormControlElement> control_elements; | |
| 470 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, | |
| 471 &control_elements); | |
| 472 | |
| 473 if (control_elements.size() != data.fields.size()) { | |
| 474 // This case should be reachable only for pathological websites and tests, | |
| 475 // which add or remove form fields while the user is interacting with the | |
| 476 // Autofill popup. | |
| 477 return; | |
| 478 } | |
| 479 | |
| 480 // It's possible that the site has injected fields into the form after the | |
| 481 // page has loaded, so we can't assert that the size of the cached control | |
| 482 // elements is equal to the size of the fields in |form|. Fortunately, the | |
| 483 // one case in the wild where this happens, paypal.com signup form, the fields | |
| 484 // are appended to the end of the form and are not visible. | |
| 485 for (size_t i = 0; i < control_elements.size(); ++i) { | |
| 486 WebFormControlElement* element = &control_elements[i]; | |
| 487 | |
| 488 if (base::string16(element->nameForAutofill()) != data.fields[i].name) { | |
| 489 // This case should be reachable only for pathological websites, which | |
| 490 // rename form fields while the user is interacting with the Autofill | |
| 491 // popup. I (isherman) am not aware of any such websites, and so am | |
| 492 // optimistically including a NOTREACHED(). If you ever trip this check, | |
| 493 // please file a bug against me. | |
| 494 NOTREACHED(); | |
| 495 continue; | |
| 496 } | |
| 497 | |
| 498 bool is_initiating_element = (*element == initiating_element); | |
| 499 | |
| 500 // Only autofill empty fields and the field that initiated the filling, | |
| 501 // i.e. the field the user is currently editing and interacting with. | |
| 502 const WebInputElement* input_element = toWebInputElement(element); | |
| 503 if (!force_override && IsTextInput(input_element) && | |
| 504 !is_initiating_element && !input_element->value().isEmpty()) | |
| 505 continue; | |
| 506 | |
| 507 if (!element->isEnabled() || element->isReadOnly() || | |
| 508 (only_focusable_elements && !element->isFocusable())) | |
| 509 continue; | |
| 510 | |
| 511 callback(data.fields[i], is_initiating_element, element); | |
| 512 } | |
| 513 } | |
| 514 | |
| 515 // Sets the |field|'s value to the value in |data|. | |
| 516 // Also sets the "autofilled" attribute, causing the background to be yellow. | |
| 517 void FillFormField(const FormFieldData& data, | |
| 518 bool is_initiating_node, | |
| 519 WebKit::WebFormControlElement* field) { | |
| 520 // Nothing to fill. | |
| 521 if (data.value.empty()) | |
| 522 return; | |
| 523 | |
| 524 WebInputElement* input_element = toWebInputElement(field); | |
| 525 if (IsTextInput(input_element)) { | |
| 526 // If the maxlength attribute contains a negative value, maxLength() | |
| 527 // returns the default maxlength value. | |
| 528 input_element->setValue( | |
| 529 data.value.substr(0, input_element->maxLength()), true); | |
| 530 input_element->setAutofilled(true); | |
| 531 if (is_initiating_node) { | |
| 532 int length = input_element->value().length(); | |
| 533 input_element->setSelectionRange(length, length); | |
| 534 // Clear the current IME composition (the underline), if there is one. | |
| 535 input_element->document().frame()->unmarkText(); | |
| 536 } | |
| 537 } else if (IsSelectElement(*field)) { | |
| 538 WebSelectElement select_element = field->to<WebSelectElement>(); | |
| 539 if (select_element.value() != data.value) { | |
| 540 select_element.setValue(data.value); | |
| 541 select_element.dispatchFormControlChangeEvent(); | |
| 542 } | |
| 543 } else { | |
| 544 DCHECK(IsCheckableElement(input_element)); | |
| 545 input_element->setChecked(data.is_checked, true); | |
| 546 } | |
| 547 } | |
| 548 | |
| 549 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|. | |
| 550 // Also sets the "autofilled" attribute, causing the background to be yellow. | |
| 551 void PreviewFormField(const FormFieldData& data, | |
| 552 bool is_initiating_node, | |
| 553 WebKit::WebFormControlElement* field) { | |
| 554 // Nothing to preview. | |
| 555 if (data.value.empty()) | |
| 556 return; | |
| 557 | |
| 558 // Only preview input fields. Excludes checkboxes and radio buttons, as there | |
| 559 // is no provision for setSuggestedCheckedValue in WebInputElement. | |
| 560 WebInputElement* input_element = toWebInputElement(field); | |
| 561 if (!IsTextInput(input_element)) | |
| 562 return; | |
| 563 | |
| 564 // If the maxlength attribute contains a negative value, maxLength() | |
| 565 // returns the default maxlength value. | |
| 566 input_element->setSuggestedValue( | |
| 567 data.value.substr(0, input_element->maxLength())); | |
| 568 input_element->setAutofilled(true); | |
| 569 if (is_initiating_node) { | |
| 570 // Select the part of the text that the user didn't type. | |
| 571 input_element->setSelectionRange(input_element->value().length(), | |
| 572 input_element->suggestedValue().length()); | |
| 573 } | |
| 574 } | |
| 575 | |
| 576 std::string RetrievalMethodToString( | |
| 577 const WebElementDescriptor::RetrievalMethod& method) { | |
| 578 switch (method) { | |
| 579 case WebElementDescriptor::CSS_SELECTOR: | |
| 580 return "CSS_SELECTOR"; | |
| 581 case WebElementDescriptor::ID: | |
| 582 return "ID"; | |
| 583 case WebElementDescriptor::NONE: | |
| 584 return "NONE"; | |
| 585 } | |
| 586 NOTREACHED(); | |
| 587 return "UNKNOWN"; | |
| 588 } | |
| 589 | |
| 590 } // namespace | |
| 591 | |
| 592 const size_t kMaxParseableFields = 100; | |
| 593 | |
| 594 // All text fields, including password fields, should be extracted. | |
| 595 bool IsTextInput(const WebInputElement* element) { | |
| 596 return element && element->isTextField(); | |
| 597 } | |
| 598 | |
| 599 bool IsSelectElement(const WebFormControlElement& element) { | |
| 600 // Is static for improving performance. | |
| 601 CR_DEFINE_STATIC_LOCAL(WebString, kSelectOne, ("select-one")); | |
| 602 return element.formControlType() == kSelectOne; | |
| 603 } | |
| 604 | |
| 605 bool IsCheckableElement(const WebInputElement* element) { | |
| 606 if (!element) | |
| 607 return false; | |
| 608 | |
| 609 return element->isCheckbox() || element->isRadioButton(); | |
| 610 } | |
| 611 | |
| 612 bool IsAutofillableInputElement(const WebInputElement* element) { | |
| 613 return IsTextInput(element) || IsCheckableElement(element); | |
| 614 } | |
| 615 | |
| 616 const base::string16 GetFormIdentifier(const WebFormElement& form) { | |
| 617 base::string16 identifier = form.name(); | |
| 618 CR_DEFINE_STATIC_LOCAL(WebString, kId, ("id")); | |
| 619 if (identifier.empty()) | |
| 620 identifier = form.getAttribute(kId); | |
| 621 | |
| 622 return identifier; | |
| 623 } | |
| 624 | |
| 625 bool ClickElement(const WebDocument& document, | |
| 626 const WebElementDescriptor& element_descriptor) { | |
| 627 WebString web_descriptor = WebString::fromUTF8(element_descriptor.descriptor); | |
| 628 WebKit::WebElement element; | |
| 629 | |
| 630 switch (element_descriptor.retrieval_method) { | |
| 631 case WebElementDescriptor::CSS_SELECTOR: { | |
| 632 WebExceptionCode ec = 0; | |
| 633 element = document.querySelector(web_descriptor, ec); | |
| 634 if (ec) | |
| 635 DVLOG(1) << "Query selector failed. Error code: " << ec << "."; | |
| 636 break; | |
| 637 } | |
| 638 case WebElementDescriptor::ID: | |
| 639 element = document.getElementById(web_descriptor); | |
| 640 break; | |
| 641 case WebElementDescriptor::NONE: | |
| 642 return true; | |
| 643 } | |
| 644 | |
| 645 if (element.isNull()) { | |
| 646 DVLOG(1) << "Could not find " | |
| 647 << element_descriptor.descriptor | |
| 648 << " by " | |
| 649 << RetrievalMethodToString(element_descriptor.retrieval_method) | |
| 650 << "."; | |
| 651 return false; | |
| 652 } | |
| 653 | |
| 654 element.simulateClick(); | |
| 655 return true; | |
| 656 } | |
| 657 | |
| 658 // Fills |autofillable_elements| with all the auto-fillable form control | |
| 659 // elements in |form_element|. | |
| 660 void ExtractAutofillableElements( | |
| 661 const WebFormElement& form_element, | |
| 662 RequirementsMask requirements, | |
| 663 std::vector<WebFormControlElement>* autofillable_elements) { | |
| 664 WebVector<WebFormControlElement> control_elements; | |
| 665 form_element.getFormControlElements(control_elements); | |
| 666 | |
| 667 autofillable_elements->clear(); | |
| 668 for (size_t i = 0; i < control_elements.size(); ++i) { | |
| 669 WebFormControlElement element = control_elements[i]; | |
| 670 if (!IsAutofillableElement(element)) | |
| 671 continue; | |
| 672 | |
| 673 if (requirements & REQUIRE_AUTOCOMPLETE) { | |
| 674 // TODO(jhawkins): WebKit currently doesn't handle the autocomplete | |
| 675 // attribute for select control elements, but it probably should. | |
| 676 WebInputElement* input_element = toWebInputElement(&control_elements[i]); | |
| 677 if (IsAutofillableInputElement(input_element) && | |
| 678 !SatisfiesRequireAutocomplete(*input_element)) | |
| 679 continue; | |
| 680 } | |
| 681 | |
| 682 autofillable_elements->push_back(element); | |
| 683 } | |
| 684 } | |
| 685 | |
| 686 void WebFormControlElementToFormField(const WebFormControlElement& element, | |
| 687 ExtractMask extract_mask, | |
| 688 FormFieldData* field) { | |
| 689 DCHECK(field); | |
| 690 DCHECK(!element.isNull()); | |
| 691 CR_DEFINE_STATIC_LOCAL(WebString, kAutocomplete, ("autocomplete")); | |
| 692 | |
| 693 // The label is not officially part of a WebFormControlElement; however, the | |
| 694 // labels for all form control elements are scraped from the DOM and set in | |
| 695 // WebFormElementToFormData. | |
| 696 field->name = element.nameForAutofill(); | |
| 697 field->form_control_type = UTF16ToUTF8(element.formControlType()); | |
| 698 field->autocomplete_attribute = | |
| 699 UTF16ToUTF8(element.getAttribute(kAutocomplete)); | |
| 700 if (field->autocomplete_attribute.size() > kMaxDataLength) { | |
| 701 // Discard overly long attribute values to avoid DOS-ing the browser | |
| 702 // process. However, send over a default string to indicate that the | |
| 703 // attribute was present. | |
| 704 field->autocomplete_attribute = "x-max-data-length-exceeded"; | |
| 705 } | |
| 706 | |
| 707 if (!IsAutofillableElement(element)) | |
| 708 return; | |
| 709 | |
| 710 const WebInputElement* input_element = toWebInputElement(&element); | |
| 711 if (IsAutofillableInputElement(input_element)) { | |
| 712 if (IsTextInput(input_element)) | |
| 713 field->max_length = input_element->maxLength(); | |
| 714 | |
| 715 field->is_autofilled = input_element->isAutofilled(); | |
| 716 field->is_focusable = input_element->isFocusable(); | |
| 717 field->should_autocomplete = input_element->autoComplete(); | |
| 718 field->is_checkable = IsCheckableElement(input_element); | |
| 719 field->is_checked = input_element->isChecked(); | |
| 720 } else if (extract_mask & EXTRACT_OPTIONS) { | |
| 721 // Set option strings on the field if available. | |
| 722 DCHECK(IsSelectElement(element)); | |
| 723 const WebSelectElement select_element = element.toConst<WebSelectElement>(); | |
| 724 GetOptionStringsFromElement(select_element, | |
| 725 &field->option_values, | |
| 726 &field->option_contents); | |
| 727 } | |
| 728 | |
| 729 if (!(extract_mask & EXTRACT_VALUE)) | |
| 730 return; | |
| 731 | |
| 732 base::string16 value; | |
| 733 if (IsAutofillableInputElement(input_element)) { | |
| 734 value = input_element->value(); | |
| 735 } else { | |
| 736 DCHECK(IsSelectElement(element)); | |
| 737 const WebSelectElement select_element = element.toConst<WebSelectElement>(); | |
| 738 value = select_element.value(); | |
| 739 | |
| 740 // Convert the |select_element| value to text if requested. | |
| 741 if (extract_mask & EXTRACT_OPTION_TEXT) { | |
| 742 WebVector<WebElement> list_items = select_element.listItems(); | |
| 743 for (size_t i = 0; i < list_items.size(); ++i) { | |
| 744 if (IsOptionElement(list_items[i])) { | |
| 745 const WebOptionElement option_element = | |
| 746 list_items[i].toConst<WebOptionElement>(); | |
| 747 if (option_element.value() == value) { | |
| 748 value = option_element.text(); | |
| 749 break; | |
| 750 } | |
| 751 } | |
| 752 } | |
| 753 } | |
| 754 } | |
| 755 | |
| 756 // Constrain the maximum data length to prevent a malicious site from DOS'ing | |
| 757 // the browser: http://crbug.com/49332 | |
| 758 if (value.size() > kMaxDataLength) | |
| 759 value = value.substr(0, kMaxDataLength); | |
| 760 | |
| 761 field->value = value; | |
| 762 } | |
| 763 | |
| 764 bool WebFormElementToFormData( | |
| 765 const WebKit::WebFormElement& form_element, | |
| 766 const WebKit::WebFormControlElement& form_control_element, | |
| 767 RequirementsMask requirements, | |
| 768 ExtractMask extract_mask, | |
| 769 FormData* form, | |
| 770 FormFieldData* field) { | |
| 771 CR_DEFINE_STATIC_LOCAL(WebString, kLabel, ("label")); | |
| 772 CR_DEFINE_STATIC_LOCAL(WebString, kFor, ("for")); | |
| 773 CR_DEFINE_STATIC_LOCAL(WebString, kHidden, ("hidden")); | |
| 774 | |
| 775 const WebFrame* frame = form_element.document().frame(); | |
| 776 if (!frame) | |
| 777 return false; | |
| 778 | |
| 779 if (requirements & REQUIRE_AUTOCOMPLETE && !form_element.autoComplete()) | |
| 780 return false; | |
| 781 | |
| 782 form->name = GetFormIdentifier(form_element); | |
| 783 form->method = form_element.method(); | |
| 784 form->origin = frame->document().url(); | |
| 785 form->action = frame->document().completeURL(form_element.action()); | |
| 786 form->user_submitted = form_element.wasUserSubmitted(); | |
| 787 | |
| 788 // If the completed URL is not valid, just use the action we get from | |
| 789 // WebKit. | |
| 790 if (!form->action.is_valid()) | |
| 791 form->action = GURL(form_element.action()); | |
| 792 | |
| 793 // A map from a FormFieldData's name to the FormFieldData itself. | |
| 794 std::map<base::string16, FormFieldData*> name_map; | |
| 795 | |
| 796 // The extracted FormFields. We use pointers so we can store them in | |
| 797 // |name_map|. | |
| 798 ScopedVector<FormFieldData> form_fields; | |
| 799 | |
| 800 WebVector<WebFormControlElement> control_elements; | |
| 801 form_element.getFormControlElements(control_elements); | |
| 802 | |
| 803 // A vector of bools that indicate whether each field in the form meets the | |
| 804 // requirements and thus will be in the resulting |form|. | |
| 805 std::vector<bool> fields_extracted(control_elements.size(), false); | |
| 806 | |
| 807 for (size_t i = 0; i < control_elements.size(); ++i) { | |
| 808 const WebFormControlElement& control_element = control_elements[i]; | |
| 809 | |
| 810 if (!IsAutofillableElement(control_element)) | |
| 811 continue; | |
| 812 | |
| 813 const WebInputElement* input_element = toWebInputElement(&control_element); | |
| 814 if (requirements & REQUIRE_AUTOCOMPLETE && | |
| 815 IsAutofillableInputElement(input_element) && | |
| 816 !SatisfiesRequireAutocomplete(*input_element)) | |
| 817 continue; | |
| 818 | |
| 819 // Create a new FormFieldData, fill it out and map it to the field's name. | |
| 820 FormFieldData* form_field = new FormFieldData; | |
| 821 WebFormControlElementToFormField(control_element, extract_mask, form_field); | |
| 822 form_fields.push_back(form_field); | |
| 823 // TODO(jhawkins): A label element is mapped to a form control element's id. | |
| 824 // field->name() will contain the id only if the name does not exist. Add | |
| 825 // an id() method to WebFormControlElement and use that here. | |
| 826 name_map[form_field->name] = form_field; | |
| 827 fields_extracted[i] = true; | |
| 828 } | |
| 829 | |
| 830 // If we failed to extract any fields, give up. Also, to avoid overly | |
| 831 // expensive computation, we impose a maximum number of allowable fields. | |
| 832 if (form_fields.empty() || form_fields.size() > kMaxParseableFields) | |
| 833 return false; | |
| 834 | |
| 835 // Loop through the label elements inside the form element. For each label | |
| 836 // element, get the corresponding form control element, use the form control | |
| 837 // element's name as a key into the <name, FormFieldData> map to find the | |
| 838 // previously created FormFieldData and set the FormFieldData's label to the | |
| 839 // label.firstChild().nodeValue() of the label element. | |
| 840 WebNodeList labels = form_element.getElementsByTagName(kLabel); | |
| 841 for (unsigned i = 0; i < labels.length(); ++i) { | |
| 842 WebLabelElement label = labels.item(i).to<WebLabelElement>(); | |
| 843 WebFormControlElement field_element = | |
| 844 label.correspondingControl().to<WebFormControlElement>(); | |
| 845 | |
| 846 base::string16 element_name; | |
| 847 if (field_element.isNull()) { | |
| 848 // Sometimes site authors will incorrectly specify the corresponding | |
| 849 // field element's name rather than its id, so we compensate here. | |
| 850 element_name = label.getAttribute(kFor); | |
| 851 } else if ( | |
| 852 !field_element.isFormControlElement() || | |
| 853 field_element.formControlType() == kHidden) { | |
| 854 continue; | |
| 855 } else { | |
| 856 element_name = field_element.nameForAutofill(); | |
| 857 } | |
| 858 | |
| 859 std::map<base::string16, FormFieldData*>::iterator iter = | |
| 860 name_map.find(element_name); | |
| 861 if (iter != name_map.end()) { | |
| 862 base::string16 label_text = FindChildText(label); | |
| 863 | |
| 864 // Concatenate labels because some sites might have multiple label | |
| 865 // candidates. | |
| 866 if (!iter->second->label.empty() && !label_text.empty()) | |
| 867 iter->second->label += ASCIIToUTF16(" "); | |
| 868 iter->second->label += label_text; | |
| 869 } | |
| 870 } | |
| 871 | |
| 872 // Loop through the form control elements, extracting the label text from | |
| 873 // the DOM. We use the |fields_extracted| vector to make sure we assign the | |
| 874 // extracted label to the correct field, as it's possible |form_fields| will | |
| 875 // not contain all of the elements in |control_elements|. | |
| 876 for (size_t i = 0, field_idx = 0; | |
| 877 i < control_elements.size() && field_idx < form_fields.size(); ++i) { | |
| 878 // This field didn't meet the requirements, so don't try to find a label | |
| 879 // for it. | |
| 880 if (!fields_extracted[i]) | |
| 881 continue; | |
| 882 | |
| 883 const WebFormControlElement& control_element = control_elements[i]; | |
| 884 if (form_fields[field_idx]->label.empty()) | |
| 885 form_fields[field_idx]->label = InferLabelForElement(control_element); | |
| 886 | |
| 887 if (field && form_control_element == control_element) | |
| 888 *field = *form_fields[field_idx]; | |
| 889 | |
| 890 ++field_idx; | |
| 891 } | |
| 892 | |
| 893 // Copy the created FormFields into the resulting FormData object. | |
| 894 for (ScopedVector<FormFieldData>::const_iterator iter = form_fields.begin(); | |
| 895 iter != form_fields.end(); ++iter) { | |
| 896 form->fields.push_back(**iter); | |
| 897 } | |
| 898 | |
| 899 return true; | |
| 900 } | |
| 901 | |
| 902 bool FindFormAndFieldForInputElement(const WebInputElement& element, | |
| 903 FormData* form, | |
| 904 FormFieldData* field, | |
| 905 RequirementsMask requirements) { | |
| 906 if (!IsAutofillableElement(element)) | |
| 907 return false; | |
| 908 | |
| 909 const WebFormElement form_element = element.form(); | |
| 910 if (form_element.isNull()) | |
| 911 return false; | |
| 912 | |
| 913 ExtractMask extract_mask = | |
| 914 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS); | |
| 915 return WebFormElementToFormData(form_element, | |
| 916 element, | |
| 917 requirements, | |
| 918 extract_mask, | |
| 919 form, | |
| 920 field); | |
| 921 } | |
| 922 | |
| 923 void FillForm(const FormData& form, const WebInputElement& element) { | |
| 924 WebFormElement form_element = element.form(); | |
| 925 if (form_element.isNull()) | |
| 926 return; | |
| 927 | |
| 928 ForEachMatchingFormField(form_element, | |
| 929 element, | |
| 930 form, | |
| 931 true, /* only_focusable_elements */ | |
| 932 false, /* dont force override */ | |
| 933 &FillFormField); | |
| 934 } | |
| 935 | |
| 936 void FillFormIncludingNonFocusableElements(const FormData& form_data, | |
| 937 const WebFormElement& form_element) { | |
| 938 if (form_element.isNull()) | |
| 939 return; | |
| 940 | |
| 941 ForEachMatchingFormField(form_element, | |
| 942 WebInputElement(), | |
| 943 form_data, | |
| 944 false, /* only_focusable_elements */ | |
| 945 true, /* force override */ | |
| 946 &FillFormField); | |
| 947 } | |
| 948 | |
| 949 void PreviewForm(const FormData& form, const WebInputElement& element) { | |
| 950 WebFormElement form_element = element.form(); | |
| 951 if (form_element.isNull()) | |
| 952 return; | |
| 953 | |
| 954 ForEachMatchingFormField(form_element, | |
| 955 element, | |
| 956 form, | |
| 957 true, /* only_focusable_elements */ | |
| 958 false, /* dont force override */ | |
| 959 &PreviewFormField); | |
| 960 } | |
| 961 | |
| 962 bool ClearPreviewedFormWithElement(const WebInputElement& element, | |
| 963 bool was_autofilled) { | |
| 964 WebFormElement form_element = element.form(); | |
| 965 if (form_element.isNull()) | |
| 966 return false; | |
| 967 | |
| 968 std::vector<WebFormControlElement> control_elements; | |
| 969 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, | |
| 970 &control_elements); | |
| 971 for (size_t i = 0; i < control_elements.size(); ++i) { | |
| 972 // Only text input elements can be previewed. | |
| 973 WebInputElement* input_element = toWebInputElement(&control_elements[i]); | |
| 974 if (!IsTextInput(input_element)) | |
| 975 continue; | |
| 976 | |
| 977 // If the input element is not auto-filled, we did not preview it, so there | |
| 978 // is nothing to reset. | |
| 979 if (!input_element->isAutofilled()) | |
| 980 continue; | |
| 981 | |
| 982 // There might be unrelated elements in this form which have already been | |
| 983 // auto-filled. For example, the user might have already filled the address | |
| 984 // part of a form and now be dealing with the credit card section. We only | |
| 985 // want to reset the auto-filled status for fields that were previewed. | |
| 986 if (input_element->suggestedValue().isEmpty()) | |
| 987 continue; | |
| 988 | |
| 989 // Clear the suggested value. For the initiating node, also restore the | |
| 990 // original value. | |
| 991 input_element->setSuggestedValue(WebString()); | |
| 992 bool is_initiating_node = (element == *input_element); | |
| 993 if (is_initiating_node) | |
| 994 input_element->setAutofilled(was_autofilled); | |
| 995 else | |
| 996 input_element->setAutofilled(false); | |
| 997 | |
| 998 // Clearing the suggested value in the focused node (above) can cause | |
| 999 // selection to be lost. We force selection range to restore the text | |
| 1000 // cursor. | |
| 1001 if (is_initiating_node) { | |
| 1002 int length = input_element->value().length(); | |
| 1003 input_element->setSelectionRange(length, length); | |
| 1004 } | |
| 1005 } | |
| 1006 | |
| 1007 return true; | |
| 1008 } | |
| 1009 | |
| 1010 bool FormWithElementIsAutofilled(const WebInputElement& element) { | |
| 1011 WebFormElement form_element = element.form(); | |
| 1012 if (form_element.isNull()) | |
| 1013 return false; | |
| 1014 | |
| 1015 std::vector<WebFormControlElement> control_elements; | |
| 1016 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, | |
| 1017 &control_elements); | |
| 1018 for (size_t i = 0; i < control_elements.size(); ++i) { | |
| 1019 WebInputElement* input_element = toWebInputElement(&control_elements[i]); | |
| 1020 if (!IsAutofillableInputElement(input_element)) | |
| 1021 continue; | |
| 1022 | |
| 1023 if (input_element->isAutofilled()) | |
| 1024 return true; | |
| 1025 } | |
| 1026 | |
| 1027 return false; | |
| 1028 } | |
| 1029 | |
| 1030 } // namespace autofill | |
| OLD | NEW |