| 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 bool force_override, | |
| 465 Callback callback) { | |
| 466 std::vector<WebFormControlElement> control_elements; | |
| 467 ExtractAutofillableElements(form_element, autofill::REQUIRE_AUTOCOMPLETE, | |
| 468 &control_elements); | |
| 469 | |
| 470 if (control_elements.size() != data.fields.size()) { | |
| 471 // This case should be reachable only for pathological websites and tests, | |
| 472 // which add or remove form fields while the user is interacting with the | |
| 473 // Autofill popup. | |
| 474 return; | |
| 475 } | |
| 476 | |
| 477 // It's possible that the site has injected fields into the form after the | |
| 478 // page has loaded, so we can't assert that the size of the cached control | |
| 479 // elements is equal to the size of the fields in |form|. Fortunately, the | |
| 480 // one case in the wild where this happens, paypal.com signup form, the fields | |
| 481 // are appended to the end of the form and are not visible. | |
| 482 for (size_t i = 0; i < control_elements.size(); ++i) { | |
| 483 WebFormControlElement* element = &control_elements[i]; | |
| 484 | |
| 485 if (string16(element->nameForAutofill()) != data.fields[i].name) { | |
| 486 // This case should be reachable only for pathological websites, which | |
| 487 // rename form fields while the user is interacting with the Autofill | |
| 488 // popup. I (isherman) am not aware of any such websites, and so am | |
| 489 // optimistically including a NOTREACHED(). If you ever trip this check, | |
| 490 // please file a bug against me. | |
| 491 NOTREACHED(); | |
| 492 continue; | |
| 493 } | |
| 494 | |
| 495 bool is_initiating_element = (*element == initiating_element); | |
| 496 | |
| 497 // Only autofill empty fields and the field that initiated the filling, | |
| 498 // i.e. the field the user is currently editing and interacting with. | |
| 499 const WebInputElement* input_element = toWebInputElement(element); | |
| 500 if (!force_override && IsTextInput(input_element) && | |
| 501 !is_initiating_element && !input_element->value().isEmpty()) | |
| 502 continue; | |
| 503 | |
| 504 if (!element->isEnabled() || element->isReadOnly() || | |
| 505 (only_focusable_elements && !element->isFocusable())) | |
| 506 continue; | |
| 507 | |
| 508 callback(data.fields[i], is_initiating_element, element); | |
| 509 } | |
| 510 } | |
| 511 | |
| 512 // Sets the |field|'s value to the value in |data|. | |
| 513 // Also sets the "autofilled" attribute, causing the background to be yellow. | |
| 514 void FillFormField(const FormFieldData& data, | |
| 515 bool is_initiating_node, | |
| 516 WebKit::WebFormControlElement* field) { | |
| 517 // Nothing to fill. | |
| 518 if (data.value.empty()) | |
| 519 return; | |
| 520 | |
| 521 WebInputElement* input_element = toWebInputElement(field); | |
| 522 if (IsTextInput(input_element)) { | |
| 523 // If the maxlength attribute contains a negative value, maxLength() | |
| 524 // returns the default maxlength value. | |
| 525 input_element->setValue( | |
| 526 data.value.substr(0, input_element->maxLength()), true); | |
| 527 input_element->setAutofilled(true); | |
| 528 if (is_initiating_node) { | |
| 529 int length = input_element->value().length(); | |
| 530 input_element->setSelectionRange(length, length); | |
| 531 // Clear the current IME composition (the underline), if there is one. | |
| 532 input_element->document().frame()->unmarkText(); | |
| 533 } | |
| 534 } else if (IsSelectElement(*field)) { | |
| 535 WebSelectElement select_element = field->to<WebSelectElement>(); | |
| 536 if (select_element.value() != data.value) { | |
| 537 select_element.setValue(data.value); | |
| 538 select_element.dispatchFormControlChangeEvent(); | |
| 539 } | |
| 540 } else { | |
| 541 DCHECK(IsCheckableElement(input_element)); | |
| 542 input_element->setChecked(data.is_checked, true); | |
| 543 } | |
| 544 } | |
| 545 | |
| 546 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|. | |
| 547 // Also sets the "autofilled" attribute, causing the background to be yellow. | |
| 548 void PreviewFormField(const FormFieldData& data, | |
| 549 bool is_initiating_node, | |
| 550 WebKit::WebFormControlElement* field) { | |
| 551 // Nothing to preview. | |
| 552 if (data.value.empty()) | |
| 553 return; | |
| 554 | |
| 555 // Only preview input fields. Excludes checkboxes and radio buttons, as there | |
| 556 // is no provision for setSuggestedCheckedValue in WebInputElement. | |
| 557 WebInputElement* input_element = toWebInputElement(field); | |
| 558 if (!IsTextInput(input_element)) | |
| 559 return; | |
| 560 | |
| 561 // If the maxlength attribute contains a negative value, maxLength() | |
| 562 // returns the default maxlength value. | |
| 563 input_element->setSuggestedValue( | |
| 564 data.value.substr(0, input_element->maxLength())); | |
| 565 input_element->setAutofilled(true); | |
| 566 if (is_initiating_node) { | |
| 567 // Select the part of the text that the user didn't type. | |
| 568 input_element->setSelectionRange(input_element->value().length(), | |
| 569 input_element->suggestedValue().length()); | |
| 570 } | |
| 571 } | |
| 572 | |
| 573 std::string RetrievalMethodToString( | |
| 574 const autofill::WebElementDescriptor::RetrievalMethod& method) { | |
| 575 switch (method) { | |
| 576 case autofill::WebElementDescriptor::CSS_SELECTOR: | |
| 577 return "CSS_SELECTOR"; | |
| 578 case autofill::WebElementDescriptor::ID: | |
| 579 return "ID"; | |
| 580 case autofill::WebElementDescriptor::NONE: | |
| 581 return "NONE"; | |
| 582 } | |
| 583 NOTREACHED(); | |
| 584 return "UNKNOWN"; | |
| 585 } | |
| 586 | |
| 587 } // namespace | |
| 588 | |
| 589 namespace autofill { | |
| 590 | |
| 591 const size_t kMaxParseableFields = 100; | |
| 592 | |
| 593 // In HTML5, all text fields except password are text input fields to | |
| 594 // autocomplete. | |
| 595 bool IsTextInput(const WebInputElement* element) { | |
| 596 if (!element) | |
| 597 return false; | |
| 598 | |
| 599 return element->isTextField() && !element->isPasswordField(); | |
| 600 } | |
| 601 | |
| 602 bool IsSelectElement(const WebFormControlElement& element) { | |
| 603 // Is static for improving performance. | |
| 604 CR_DEFINE_STATIC_LOCAL(WebString, kSelectOne, ("select-one")); | |
| 605 return element.formControlType() == kSelectOne; | |
| 606 } | |
| 607 | |
| 608 bool IsCheckableElement(const WebInputElement* element) { | |
| 609 if (!element) | |
| 610 return false; | |
| 611 | |
| 612 return element->isCheckbox() || element->isRadioButton(); | |
| 613 } | |
| 614 | |
| 615 bool IsAutofillableInputElement(const WebInputElement* element) { | |
| 616 return IsTextInput(element) || IsCheckableElement(element); | |
| 617 } | |
| 618 | |
| 619 const string16 GetFormIdentifier(const WebFormElement& form) { | |
| 620 string16 identifier = form.name(); | |
| 621 CR_DEFINE_STATIC_LOCAL(WebString, kId, ("id")); | |
| 622 if (identifier.empty()) | |
| 623 identifier = form.getAttribute(kId); | |
| 624 | |
| 625 return identifier; | |
| 626 } | |
| 627 | |
| 628 bool ClickElement(const WebDocument& document, | |
| 629 const WebElementDescriptor& element_descriptor) { | |
| 630 WebString web_descriptor = WebString::fromUTF8(element_descriptor.descriptor); | |
| 631 WebKit::WebElement element; | |
| 632 | |
| 633 switch (element_descriptor.retrieval_method) { | |
| 634 case WebElementDescriptor::CSS_SELECTOR: { | |
| 635 WebExceptionCode ec = 0; | |
| 636 element = document.querySelector(web_descriptor, ec); | |
| 637 if (ec) | |
| 638 DVLOG(1) << "Query selector failed. Error code: " << ec << "."; | |
| 639 break; | |
| 640 } | |
| 641 case WebElementDescriptor::ID: | |
| 642 element = document.getElementById(web_descriptor); | |
| 643 break; | |
| 644 case WebElementDescriptor::NONE: | |
| 645 return true; | |
| 646 } | |
| 647 | |
| 648 if (element.isNull()) { | |
| 649 DVLOG(1) << "Could not find " | |
| 650 << element_descriptor.descriptor | |
| 651 << " by " | |
| 652 << RetrievalMethodToString(element_descriptor.retrieval_method) | |
| 653 << "."; | |
| 654 return false; | |
| 655 } | |
| 656 | |
| 657 element.simulateClick(); | |
| 658 return true; | |
| 659 } | |
| 660 | |
| 661 // Fills |autofillable_elements| with all the auto-fillable form control | |
| 662 // elements in |form_element|. | |
| 663 void ExtractAutofillableElements( | |
| 664 const WebFormElement& form_element, | |
| 665 RequirementsMask requirements, | |
| 666 std::vector<WebFormControlElement>* autofillable_elements) { | |
| 667 WebVector<WebFormControlElement> control_elements; | |
| 668 form_element.getFormControlElements(control_elements); | |
| 669 | |
| 670 autofillable_elements->clear(); | |
| 671 for (size_t i = 0; i < control_elements.size(); ++i) { | |
| 672 WebFormControlElement element = control_elements[i]; | |
| 673 if (!IsAutofillableElement(element)) | |
| 674 continue; | |
| 675 | |
| 676 if (requirements & REQUIRE_AUTOCOMPLETE) { | |
| 677 // TODO(jhawkins): WebKit currently doesn't handle the autocomplete | |
| 678 // attribute for select control elements, but it probably should. | |
| 679 WebInputElement* input_element = toWebInputElement(&control_elements[i]); | |
| 680 if (IsAutofillableInputElement(input_element) && | |
| 681 !SatisfiesRequireAutocomplete(*input_element)) | |
| 682 continue; | |
| 683 } | |
| 684 | |
| 685 autofillable_elements->push_back(element); | |
| 686 } | |
| 687 } | |
| 688 | |
| 689 void WebFormControlElementToFormField(const WebFormControlElement& element, | |
| 690 ExtractMask extract_mask, | |
| 691 FormFieldData* field) { | |
| 692 DCHECK(field); | |
| 693 DCHECK(!element.isNull()); | |
| 694 CR_DEFINE_STATIC_LOCAL(WebString, kAutocomplete, ("autocomplete")); | |
| 695 | |
| 696 // The label is not officially part of a WebFormControlElement; however, the | |
| 697 // labels for all form control elements are scraped from the DOM and set in | |
| 698 // WebFormElementToFormData. | |
| 699 field->name = element.nameForAutofill(); | |
| 700 field->form_control_type = UTF16ToUTF8(element.formControlType()); | |
| 701 field->autocomplete_attribute = | |
| 702 UTF16ToUTF8(element.getAttribute(kAutocomplete)); | |
| 703 if (field->autocomplete_attribute.size() > kMaxDataLength) { | |
| 704 // Discard overly long attribute values to avoid DOS-ing the browser | |
| 705 // process. However, send over a default string to indicate that the | |
| 706 // attribute was present. | |
| 707 field->autocomplete_attribute = "x-max-data-length-exceeded"; | |
| 708 } | |
| 709 | |
| 710 if (!IsAutofillableElement(element)) | |
| 711 return; | |
| 712 | |
| 713 const WebInputElement* input_element = toWebInputElement(&element); | |
| 714 if (IsAutofillableInputElement(input_element)) { | |
| 715 if (IsTextInput(input_element)) | |
| 716 field->max_length = input_element->maxLength(); | |
| 717 | |
| 718 field->is_autofilled = input_element->isAutofilled(); | |
| 719 field->is_focusable = input_element->isFocusable(); | |
| 720 field->should_autocomplete = input_element->autoComplete(); | |
| 721 field->is_checkable = IsCheckableElement(input_element); | |
| 722 } else if (extract_mask & EXTRACT_OPTIONS) { | |
| 723 // Set option strings on the field if available. | |
| 724 DCHECK(IsSelectElement(element)); | |
| 725 const WebSelectElement select_element = element.toConst<WebSelectElement>(); | |
| 726 GetOptionStringsFromElement(select_element, | |
| 727 &field->option_values, | |
| 728 &field->option_contents); | |
| 729 } | |
| 730 | |
| 731 if (!(extract_mask & EXTRACT_VALUE)) | |
| 732 return; | |
| 733 | |
| 734 string16 value; | |
| 735 if (IsAutofillableInputElement(input_element)) { | |
| 736 value = input_element->value(); | |
| 737 } else { | |
| 738 DCHECK(IsSelectElement(element)); | |
| 739 const WebSelectElement select_element = element.toConst<WebSelectElement>(); | |
| 740 value = select_element.value(); | |
| 741 | |
| 742 // Convert the |select_element| value to text if requested. | |
| 743 if (extract_mask & EXTRACT_OPTION_TEXT) { | |
| 744 WebVector<WebElement> list_items = select_element.listItems(); | |
| 745 for (size_t i = 0; i < list_items.size(); ++i) { | |
| 746 if (IsOptionElement(list_items[i])) { | |
| 747 const WebOptionElement option_element = | |
| 748 list_items[i].toConst<WebOptionElement>(); | |
| 749 if (option_element.value() == value) { | |
| 750 value = option_element.text(); | |
| 751 break; | |
| 752 } | |
| 753 } | |
| 754 } | |
| 755 } | |
| 756 } | |
| 757 | |
| 758 // Constrain the maximum data length to prevent a malicious site from DOS'ing | |
| 759 // the browser: http://crbug.com/49332 | |
| 760 if (value.size() > kMaxDataLength) | |
| 761 value = value.substr(0, kMaxDataLength); | |
| 762 | |
| 763 field->value = value; | |
| 764 } | |
| 765 | |
| 766 bool WebFormElementToFormData( | |
| 767 const WebKit::WebFormElement& form_element, | |
| 768 const WebKit::WebFormControlElement& form_control_element, | |
| 769 RequirementsMask requirements, | |
| 770 ExtractMask extract_mask, | |
| 771 FormData* form, | |
| 772 FormFieldData* field) { | |
| 773 CR_DEFINE_STATIC_LOCAL(WebString, kLabel, ("label")); | |
| 774 CR_DEFINE_STATIC_LOCAL(WebString, kFor, ("for")); | |
| 775 CR_DEFINE_STATIC_LOCAL(WebString, kHidden, ("hidden")); | |
| 776 | |
| 777 const WebFrame* frame = form_element.document().frame(); | |
| 778 if (!frame) | |
| 779 return false; | |
| 780 | |
| 781 if (requirements & REQUIRE_AUTOCOMPLETE && !form_element.autoComplete()) | |
| 782 return false; | |
| 783 | |
| 784 form->name = GetFormIdentifier(form_element); | |
| 785 form->method = form_element.method(); | |
| 786 form->origin = frame->document().url(); | |
| 787 form->action = frame->document().completeURL(form_element.action()); | |
| 788 form->user_submitted = form_element.wasUserSubmitted(); | |
| 789 | |
| 790 // If the completed URL is not valid, just use the action we get from | |
| 791 // WebKit. | |
| 792 if (!form->action.is_valid()) | |
| 793 form->action = GURL(form_element.action()); | |
| 794 | |
| 795 // A map from a FormFieldData's name to the FormFieldData itself. | |
| 796 std::map<string16, FormFieldData*> name_map; | |
| 797 | |
| 798 // The extracted FormFields. We use pointers so we can store them in | |
| 799 // |name_map|. | |
| 800 ScopedVector<FormFieldData> form_fields; | |
| 801 | |
| 802 WebVector<WebFormControlElement> control_elements; | |
| 803 form_element.getFormControlElements(control_elements); | |
| 804 | |
| 805 // A vector of bools that indicate whether each field in the form meets the | |
| 806 // requirements and thus will be in the resulting |form|. | |
| 807 std::vector<bool> fields_extracted(control_elements.size(), false); | |
| 808 | |
| 809 for (size_t i = 0; i < control_elements.size(); ++i) { | |
| 810 const WebFormControlElement& control_element = control_elements[i]; | |
| 811 | |
| 812 if (!IsAutofillableElement(control_element)) | |
| 813 continue; | |
| 814 | |
| 815 const WebInputElement* input_element = toWebInputElement(&control_element); | |
| 816 if (requirements & REQUIRE_AUTOCOMPLETE && | |
| 817 IsAutofillableInputElement(input_element) && | |
| 818 !SatisfiesRequireAutocomplete(*input_element)) | |
| 819 continue; | |
| 820 | |
| 821 // Create a new FormFieldData, fill it out and map it to the field's name. | |
| 822 FormFieldData* form_field = new FormFieldData; | |
| 823 WebFormControlElementToFormField(control_element, extract_mask, form_field); | |
| 824 form_fields.push_back(form_field); | |
| 825 // TODO(jhawkins): A label element is mapped to a form control element's id. | |
| 826 // field->name() will contain the id only if the name does not exist. Add | |
| 827 // an id() method to WebFormControlElement and use that here. | |
| 828 name_map[form_field->name] = form_field; | |
| 829 fields_extracted[i] = true; | |
| 830 } | |
| 831 | |
| 832 // If we failed to extract any fields, give up. Also, to avoid overly | |
| 833 // expensive computation, we impose a maximum number of allowable fields. | |
| 834 if (form_fields.empty() || form_fields.size() > kMaxParseableFields) | |
| 835 return false; | |
| 836 | |
| 837 // Loop through the label elements inside the form element. For each label | |
| 838 // element, get the corresponding form control element, use the form control | |
| 839 // element's name as a key into the <name, FormFieldData> map to find the | |
| 840 // previously created FormFieldData and set the FormFieldData's label to the | |
| 841 // label.firstChild().nodeValue() of the label element. | |
| 842 WebNodeList labels = form_element.getElementsByTagName(kLabel); | |
| 843 for (unsigned i = 0; i < labels.length(); ++i) { | |
| 844 WebLabelElement label = labels.item(i).to<WebLabelElement>(); | |
| 845 WebFormControlElement field_element = | |
| 846 label.correspondingControl().to<WebFormControlElement>(); | |
| 847 | |
| 848 string16 element_name; | |
| 849 if (field_element.isNull()) { | |
| 850 // Sometimes site authors will incorrectly specify the corresponding | |
| 851 // field element's name rather than its id, so we compensate here. | |
| 852 element_name = label.getAttribute(kFor); | |
| 853 } else if ( | |
| 854 !field_element.isFormControlElement() || | |
| 855 field_element.formControlType() == kHidden) { | |
| 856 continue; | |
| 857 } else { | |
| 858 element_name = field_element.nameForAutofill(); | |
| 859 } | |
| 860 | |
| 861 std::map<string16, FormFieldData*>::iterator iter = | |
| 862 name_map.find(element_name); | |
| 863 if (iter != name_map.end()) { | |
| 864 string16 label_text = FindChildText(label); | |
| 865 | |
| 866 // Concatenate labels because some sites might have multiple label | |
| 867 // candidates. | |
| 868 if (!iter->second->label.empty() && !label_text.empty()) | |
| 869 iter->second->label += ASCIIToUTF16(" "); | |
| 870 iter->second->label += label_text; | |
| 871 } | |
| 872 } | |
| 873 | |
| 874 // Loop through the form control elements, extracting the label text from | |
| 875 // the DOM. We use the |fields_extracted| vector to make sure we assign the | |
| 876 // extracted label to the correct field, as it's possible |form_fields| will | |
| 877 // not contain all of the elements in |control_elements|. | |
| 878 for (size_t i = 0, field_idx = 0; | |
| 879 i < control_elements.size() && field_idx < form_fields.size(); ++i) { | |
| 880 // This field didn't meet the requirements, so don't try to find a label | |
| 881 // for it. | |
| 882 if (!fields_extracted[i]) | |
| 883 continue; | |
| 884 | |
| 885 const WebFormControlElement& control_element = control_elements[i]; | |
| 886 if (form_fields[field_idx]->label.empty()) | |
| 887 form_fields[field_idx]->label = InferLabelForElement(control_element); | |
| 888 | |
| 889 if (field && form_control_element == control_element) | |
| 890 *field = *form_fields[field_idx]; | |
| 891 | |
| 892 ++field_idx; | |
| 893 } | |
| 894 | |
| 895 // Copy the created FormFields into the resulting FormData object. | |
| 896 for (ScopedVector<FormFieldData>::const_iterator iter = form_fields.begin(); | |
| 897 iter != form_fields.end(); ++iter) { | |
| 898 form->fields.push_back(**iter); | |
| 899 } | |
| 900 | |
| 901 return true; | |
| 902 } | |
| 903 | |
| 904 bool FindFormAndFieldForInputElement(const WebInputElement& element, | |
| 905 FormData* form, | |
| 906 FormFieldData* field, | |
| 907 RequirementsMask requirements) { | |
| 908 if (!IsAutofillableElement(element)) | |
| 909 return false; | |
| 910 | |
| 911 const WebFormElement form_element = element.form(); | |
| 912 if (form_element.isNull()) | |
| 913 return false; | |
| 914 | |
| 915 ExtractMask extract_mask = | |
| 916 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS); | |
| 917 return WebFormElementToFormData(form_element, | |
| 918 element, | |
| 919 requirements, | |
| 920 extract_mask, | |
| 921 form, | |
| 922 field); | |
| 923 } | |
| 924 | |
| 925 void FillForm(const FormData& form, const WebInputElement& element) { | |
| 926 WebFormElement form_element = element.form(); | |
| 927 if (form_element.isNull()) | |
| 928 return; | |
| 929 | |
| 930 ForEachMatchingFormField(form_element, | |
| 931 element, | |
| 932 form, | |
| 933 true, /* only_focusable_elements */ | |
| 934 false, /* dont force override */ | |
| 935 &FillFormField); | |
| 936 } | |
| 937 | |
| 938 void FillFormIncludingNonFocusableElements(const FormData& form_data, | |
| 939 const WebFormElement& form_element) { | |
| 940 if (form_element.isNull()) | |
| 941 return; | |
| 942 | |
| 943 ForEachMatchingFormField(form_element, | |
| 944 WebInputElement(), | |
| 945 form_data, | |
| 946 false, /* only_focusable_elements */ | |
| 947 true, /* force override */ | |
| 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 false, /* dont force override */ | |
| 961 &PreviewFormField); | |
| 962 } | |
| 963 | |
| 964 bool ClearPreviewedFormWithElement(const WebInputElement& element, | |
| 965 bool was_autofilled) { | |
| 966 WebFormElement form_element = element.form(); | |
| 967 if (form_element.isNull()) | |
| 968 return false; | |
| 969 | |
| 970 std::vector<WebFormControlElement> control_elements; | |
| 971 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, | |
| 972 &control_elements); | |
| 973 for (size_t i = 0; i < control_elements.size(); ++i) { | |
| 974 // Only text input elements can be previewed. | |
| 975 WebInputElement* input_element = toWebInputElement(&control_elements[i]); | |
| 976 if (!IsTextInput(input_element)) | |
| 977 continue; | |
| 978 | |
| 979 // If the input element is not auto-filled, we did not preview it, so there | |
| 980 // is nothing to reset. | |
| 981 if (!input_element->isAutofilled()) | |
| 982 continue; | |
| 983 | |
| 984 // There might be unrelated elements in this form which have already been | |
| 985 // auto-filled. For example, the user might have already filled the address | |
| 986 // part of a form and now be dealing with the credit card section. We only | |
| 987 // want to reset the auto-filled status for fields that were previewed. | |
| 988 if (input_element->suggestedValue().isEmpty()) | |
| 989 continue; | |
| 990 | |
| 991 // Clear the suggested value. For the initiating node, also restore the | |
| 992 // original value. | |
| 993 input_element->setSuggestedValue(WebString()); | |
| 994 bool is_initiating_node = (element == *input_element); | |
| 995 if (is_initiating_node) | |
| 996 input_element->setAutofilled(was_autofilled); | |
| 997 else | |
| 998 input_element->setAutofilled(false); | |
| 999 | |
| 1000 // Clearing the suggested value in the focused node (above) can cause | |
| 1001 // selection to be lost. We force selection range to restore the text | |
| 1002 // cursor. | |
| 1003 if (is_initiating_node) { | |
| 1004 int length = input_element->value().length(); | |
| 1005 input_element->setSelectionRange(length, length); | |
| 1006 } | |
| 1007 } | |
| 1008 | |
| 1009 return true; | |
| 1010 } | |
| 1011 | |
| 1012 bool FormWithElementIsAutofilled(const WebInputElement& element) { | |
| 1013 WebFormElement form_element = element.form(); | |
| 1014 if (form_element.isNull()) | |
| 1015 return false; | |
| 1016 | |
| 1017 std::vector<WebFormControlElement> control_elements; | |
| 1018 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, | |
| 1019 &control_elements); | |
| 1020 for (size_t i = 0; i < control_elements.size(); ++i) { | |
| 1021 WebInputElement* input_element = toWebInputElement(&control_elements[i]); | |
| 1022 if (!IsAutofillableInputElement(input_element)) | |
| 1023 continue; | |
| 1024 | |
| 1025 if (input_element->isAutofilled()) | |
| 1026 return true; | |
| 1027 } | |
| 1028 | |
| 1029 return false; | |
| 1030 } | |
| 1031 | |
| 1032 } // namespace autofill | |
| OLD | NEW |