Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/autofill/content/renderer/form_autofill_util.h" | 5 #include "components/autofill/content/renderer/form_autofill_util.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 415 previous = previous.previousSibling(); | 415 previous = previous.previousSibling(); |
| 416 } | 416 } |
| 417 | 417 |
| 418 return inferred_label; | 418 return inferred_label; |
| 419 } | 419 } |
| 420 | 420 |
| 421 // Helper for |InferLabelForElement()| that infers a label, if possible, from | 421 // Helper for |InferLabelForElement()| that infers a label, if possible, from |
| 422 // a surrounding div table, | 422 // a surrounding div table, |
| 423 // e.g. <div>Some Text<span><input ...></span></div> | 423 // e.g. <div>Some Text<span><input ...></span></div> |
| 424 // e.g. <div>Some Text</div><div><input ...></div> | 424 // e.g. <div>Some Text</div><div><input ...></div> |
| 425 // | |
| 426 // Because this is already traversing the <div> structure, if it finds a <label> | |
| 427 // sibling along the way, infer from that <label>. A possible future improvement | |
| 428 // is to ignore <label for="valid_id"> since those are associated with other | |
|
Evan Stade
2015/03/16 23:35:36
isn't it pretty easy to do this? just check WebLab
Lei Zhang
2015/03/17 00:23:37
Yes, I just didn't get to it, so I just wrote a no
| |
| 429 // elements. | |
| 425 base::string16 InferLabelFromDivTable(const WebFormControlElement& element) { | 430 base::string16 InferLabelFromDivTable(const WebFormControlElement& element) { |
| 426 WebNode node = element.parentNode(); | 431 WebNode node = element.parentNode(); |
| 427 bool looking_for_parent = true; | 432 bool looking_for_parent = true; |
| 428 std::set<WebNode> divs_to_skip; | 433 std::set<WebNode> divs_to_skip; |
| 429 | 434 |
| 430 // Search the sibling and parent <div>s until we find a candidate label. | 435 // Search the sibling and parent <div>s until we find a candidate label. |
| 431 base::string16 inferred_label; | 436 base::string16 inferred_label; |
| 432 CR_DEFINE_STATIC_LOCAL(WebString, kDiv, ("div")); | 437 CR_DEFINE_STATIC_LOCAL(WebString, kDiv, ("div")); |
| 433 CR_DEFINE_STATIC_LOCAL(WebString, kTable, ("table")); | 438 CR_DEFINE_STATIC_LOCAL(WebString, kTable, ("table")); |
| 434 CR_DEFINE_STATIC_LOCAL(WebString, kFieldSet, ("fieldset")); | 439 CR_DEFINE_STATIC_LOCAL(WebString, kFieldSet, ("fieldset")); |
| 440 CR_DEFINE_STATIC_LOCAL(WebString, kLabel, ("label")); | |
| 435 while (inferred_label.empty() && !node.isNull()) { | 441 while (inferred_label.empty() && !node.isNull()) { |
| 436 if (HasTagName(node, kDiv)) { | 442 if (HasTagName(node, kDiv)) { |
| 437 if (looking_for_parent) | 443 if (looking_for_parent) |
| 438 inferred_label = FindChildTextWithIgnoreList(node, divs_to_skip); | 444 inferred_label = FindChildTextWithIgnoreList(node, divs_to_skip); |
| 439 else | 445 else |
| 440 inferred_label = FindChildText(node); | 446 inferred_label = FindChildText(node); |
| 441 | 447 |
| 442 // Avoid sibling DIVs that contain autofillable fields. | 448 // Avoid sibling DIVs that contain autofillable fields. |
| 443 if (!looking_for_parent && !inferred_label.empty()) { | 449 if (!looking_for_parent && !inferred_label.empty()) { |
| 444 CR_DEFINE_STATIC_LOCAL(WebString, kSelector, | 450 CR_DEFINE_STATIC_LOCAL(WebString, kSelector, |
| 445 ("input, select, textarea")); | 451 ("input, select, textarea")); |
| 446 blink::WebExceptionCode ec = 0; | 452 blink::WebExceptionCode ec = 0; |
| 447 WebElement result_element = node.querySelector(kSelector, ec); | 453 WebElement result_element = node.querySelector(kSelector, ec); |
| 448 if (!result_element.isNull()) { | 454 if (!result_element.isNull()) { |
| 449 inferred_label.clear(); | 455 inferred_label.clear(); |
| 450 divs_to_skip.insert(node); | 456 divs_to_skip.insert(node); |
| 451 } | 457 } |
| 452 } | 458 } |
| 453 | 459 |
| 454 looking_for_parent = false; | 460 looking_for_parent = false; |
| 461 } else if (!looking_for_parent && HasTagName(node, kLabel)) { | |
| 462 inferred_label = FindChildText(node); | |
| 455 } else if (looking_for_parent && | 463 } else if (looking_for_parent && |
| 456 (HasTagName(node, kTable) || HasTagName(node, kFieldSet))) { | 464 (HasTagName(node, kTable) || HasTagName(node, kFieldSet))) { |
| 457 // If the element is in a table or fieldset, its label most likely is too. | 465 // If the element is in a table or fieldset, its label most likely is too. |
| 458 break; | 466 break; |
| 459 } | 467 } |
| 460 | 468 |
| 461 if (node.previousSibling().isNull()) { | 469 if (node.previousSibling().isNull()) { |
| 462 // If there are no more siblings, continue walking up the tree. | 470 // If there are no more siblings, continue walking up the tree. |
| 463 looking_for_parent = true; | 471 looking_for_parent = true; |
| 464 } | 472 } |
| (...skipping 952 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1417 | 1425 |
| 1418 gfx::RectF GetScaledBoundingBox(float scale, WebElement* element) { | 1426 gfx::RectF GetScaledBoundingBox(float scale, WebElement* element) { |
| 1419 gfx::Rect bounding_box(element->boundsInViewportSpace()); | 1427 gfx::Rect bounding_box(element->boundsInViewportSpace()); |
| 1420 return gfx::RectF(bounding_box.x() * scale, | 1428 return gfx::RectF(bounding_box.x() * scale, |
| 1421 bounding_box.y() * scale, | 1429 bounding_box.y() * scale, |
| 1422 bounding_box.width() * scale, | 1430 bounding_box.width() * scale, |
| 1423 bounding_box.height() * scale); | 1431 bounding_box.height() * scale); |
| 1424 } | 1432 } |
| 1425 | 1433 |
| 1426 } // namespace autofill | 1434 } // namespace autofill |
| OLD | NEW |