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