Skip to content

Commit

Permalink
Autofill: Improve inferring labels from div/td tags.
Browse files Browse the repository at this point in the history
Do not always infer from tables before divs. If the closest ancestor of
an element is a div, then try to infer from divs first.

Review URL: https://codereview.chromium.org/847433002

Cr-Commit-Position: refs/heads/master@{#310945}
  • Loading branch information
leizleiz authored and Commit bot committed Jan 10, 2015
1 parent e952053 commit cf465f4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ ADDRESS_HOME_CITY | ShipCity | Town/City* | | ShipName_1-default
ADDRESS_HOME_STATE | ShipState | State/Province* | | ShipName_1-default
UNKNOWN_TYPE | ShipStateOther | State/Province* | | ShipName_1-default
ADDRESS_HOME_ZIP | ShipZip | Zip/Postal Code* | | ShipName_1-default
UNKNOWN_TYPE | ShowGiftMsg | 2 Shipping Address | on | ShipName_1-default
ADDRESS_HOME_STREET_ADDRESS | GiftMessage | 2 Shipping Address | | ShipName_1-default
UNKNOWN_TYPE | ShowGiftMsg | This is a gift order. Check this box to see gift options. | on | ShipName_1-default
UNKNOWN_TYPE | GiftMessage | One message per order You have | | ShipName_1-default
UNKNOWN_TYPE | countdown | You have | 300 | ShipName_1-default
UNKNOWN_TYPE | ApplyGiftWrap | 2 Shipping Address | on | ShipName_1-default
UNKNOWN_TYPE | ApplyGiftWrap | (+$0.00) | on | ShipName_1-default
UNKNOWN_TYPE | txtCoupon | Apply Promo Code | | ShipName_1-default
UNKNOWN_TYPE | PaymentMethod | | PayByCafeCash | ShipName_1-default
UNKNOWN_TYPE | PaymentMethod | | PayByGC | ShipName_1-default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ ADDRESS_HOME_COUNTRY | CC_COUNTRY | Country | United States | f1_1-default
PHONE_HOME_WHOLE_NUMBER | dphone | Telephone | | f1_1-default
EMAIL_ADDRESS | demail | Email * | | f1_1-default
UNKNOWN_TYPE | amt | $ | | f1_1-default
UNKNOWN_TYPE | tributeSelect | Name * | on | f1_1-default
UNKNOWN_TYPE | DonationType | Name * | OneTime | f1_1-default
UNKNOWN_TYPE | tributeSelect | Gift amount in USD ($10 minimum) $ | on | f1_1-default
UNKNOWN_TYPE | DonationType | Gift amount in USD ($10 minimum) $ | OneTime | f1_1-default
UNKNOWN_TYPE | DonationType | This is a one time donation | Recurs | f1_1-default
UNKNOWN_TYPE | ccrecurring | I would like to make this a recurring donation deducted | Monthly | f1_1-default
UNKNOWN_TYPE | remLen | ( You may enter up to 500 characters. ) | 500 | f1_1-default
Expand Down
46 changes: 39 additions & 7 deletions components/autofill/content/renderer/form_autofill_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,25 @@ base::string16 InferLabelFromDefinitionList(
return FindChildText(previous);
}

// Returns true if the closest ancestor is a <div> and not a <td>.
// Returns false if the closest ancestor is a <td> tag,
// or if there is no <div> or <td> ancestor.
bool ClosestAncestorIsDivAndNotTD(const WebFormControlElement& element) {
for (WebNode parent_node = element.parentNode();
!parent_node.isNull();
parent_node = parent_node.parentNode()) {
if (!parent_node.isElementNode())
continue;

WebElement cur_element = parent_node.to<WebElement>();
if (cur_element.hasHTMLTagName("div"))
return true;
if (cur_element.hasHTMLTagName("td"))
return false;
}
return false;
}

// Infers corresponding label for |element| from surrounding context in the DOM,
// e.g. the contents of the preceding <p> tag or text element.
base::string16 InferLabelForElement(const WebFormControlElement& element) {
Expand All @@ -470,6 +489,20 @@ base::string16 InferLabelForElement(const WebFormControlElement& element) {
if (!inferred_label.empty())
return inferred_label;

// If we didn't find a label, check for definition list case.
inferred_label = InferLabelFromDefinitionList(element);
if (!inferred_label.empty())
return inferred_label;

bool check_div_first = ClosestAncestorIsDivAndNotTD(element);
if (check_div_first) {
// If we didn't find a label, check for div table case first since it's the
// closest ancestor.
inferred_label = InferLabelFromDivTable(element);
if (!inferred_label.empty())
return inferred_label;
}

// If we didn't find a label, check for table cell case.
inferred_label = InferLabelFromTableColumn(element);
if (!inferred_label.empty())
Expand All @@ -480,13 +513,12 @@ base::string16 InferLabelForElement(const WebFormControlElement& element) {
if (!inferred_label.empty())
return inferred_label;

// If we didn't find a label, check for definition list case.
inferred_label = InferLabelFromDefinitionList(element);
if (!inferred_label.empty())
return inferred_label;

// If we didn't find a label, check for div table case.
return InferLabelFromDivTable(element);
if (!check_div_first) {
// If we didn't find a label from the table, check for div table case if we
// haven't already.
inferred_label = InferLabelFromDivTable(element);
}
return inferred_label;
}

// Fills |option_strings| with the values of the <option> elements present in
Expand Down

0 comments on commit cf465f4

Please sign in to comment.