Commit f5dc74a3 authored by Luka Dojcilovic's avatar Luka Dojcilovic Committed by Commit Bot

[Autofill] Added aria-label to InferLabelForElement

Added aria-labels as one of the possible things to be used as a label
for form control elements because they may contain useful information
for username detection.

Bug: 783185
Change-Id: I59a7f025c616f38d533c9c9a7327ec8ec9021c4c
Reviewed-on: https://chromium-review.googlesource.com/951765Reviewed-by: default avatarMaxim Kolosovskiy <kolos@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Commit-Queue: Maxim Kolosovskiy <kolos@chromium.org>
Cr-Commit-Position: refs/heads/master@{#541929}
parent 09aa62c7
......@@ -96,6 +96,7 @@ enum LabelSource {
DD_TAG,
LI_TAG,
PLACE_HOLDER,
ARIA_LABEL,
COMBINED,
VALUE,
};
......
......@@ -488,6 +488,8 @@ EnumTraits<autofill::mojom::LabelSource, autofill::FormFieldData::LabelSource>::
return autofill::mojom::LabelSource::LI_TAG;
case autofill::FormFieldData::LabelSource::PLACE_HOLDER:
return autofill::mojom::LabelSource::PLACE_HOLDER;
case autofill::FormFieldData::LabelSource::ARIA_LABEL:
return autofill::mojom::LabelSource::ARIA_LABEL;
case autofill::FormFieldData::LabelSource::COMBINED:
return autofill::mojom::LabelSource::COMBINED;
case autofill::FormFieldData::LabelSource::VALUE:
......@@ -528,6 +530,9 @@ bool EnumTraits<autofill::mojom::LabelSource,
case autofill::mojom::LabelSource::PLACE_HOLDER:
*output = autofill::FormFieldData::LabelSource::PLACE_HOLDER;
return true;
case autofill::mojom::LabelSource::ARIA_LABEL:
*output = autofill::FormFieldData::LabelSource::ARIA_LABEL;
return true;
case autofill::mojom::LabelSource::COMBINED:
*output = autofill::FormFieldData::LabelSource::COMBINED;
return true;
......
......@@ -15,6 +15,7 @@
#include "base/i18n/case_conversion.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
......@@ -392,6 +393,16 @@ base::string16 InferLabelFromPlaceholder(const WebFormControlElement& element) {
return base::string16();
}
// Helper for |InferLabelForElement()| that infers a label, if possible, from
// the aria-label. e.g. <input aria-label="foo">
base::string16 InferLabelFromAriaLabel(const WebFormControlElement& element) {
static const base::NoDestructor<WebString> kAriaLabel("aria-label");
if (element.HasAttribute(*kAriaLabel))
return element.GetAttribute(*kAriaLabel).Utf16();
return base::string16();
}
// Helper for |InferLabelForElement()| that infers a label, from
// the value attribute when it is present and user has not typed in (if
// element's value attribute is same as the element's value).
......@@ -711,6 +722,14 @@ bool InferLabelForElement(const WebFormControlElement& element,
return true;
}
// If we didn't find a placeholder, check for aria-label text.
inferred_label = InferLabelFromAriaLabel(element);
if (IsLabelValid(inferred_label, stop_words)) {
*label_source = FormFieldData::LabelSource::ARIA_LABEL;
*label = std::move(inferred_label);
return true;
}
// For all other searches that involve traversing up the tree, the search
// order is based on which tag is the closest ancestor to |element|.
std::vector<std::string> tag_names = AncestorTagNames(element);
......
......@@ -229,6 +229,8 @@ TEST_F(FormAutofillUtilsTest, InferLabelSourceTest) {
FormFieldData::LabelSource::P_TAG},
{"PLACE_HOLDER", "<input id='target' placeholder='label'/>",
FormFieldData::LabelSource::PLACE_HOLDER},
{"ARIA_LABEL", "<input id='target' aria-label='label'/>",
FormFieldData::LabelSource::ARIA_LABEL},
{"VALUE", "<input id='target' value='label'/>",
FormFieldData::LabelSource::VALUE},
{"LI_TAG", "<li>label<div><input id='target'/></div></li>",
......
......@@ -59,6 +59,7 @@ struct FormFieldData {
DD_TAG,
LI_TAG,
PLACE_HOLDER,
ARIA_LABEL,
COMBINED, // Combined with various elements.
VALUE, // label is the value of element.
};
......
......@@ -1088,6 +1088,25 @@ __gCrWeb.fill.inferLabelFromPlaceholder = function(element) {
return element.placeholder || element.getAttribute('placeholder') || '';
};
/**
* Helper for |InferLabelForElement()| that infers a label, if possible, from
* the aria-label attribute.
*
* It is based on the logic in
* string16 InferLabelFromAriaLabel(const WebFormControlElement& element)
* in chromium/src/components/autofill/content/renderer/form_autofill_util.cc.
*
* @param {FormControlElement} element An element to examine.
* @return {string} The label of element.
*/
__gCrWeb.fill.inferLabelFromAriaLabel = function(element) {
if (!element) {
return '';
}
return element.getAttribute('aria-label') || '';
};
/**
* Helper for |InferLabelForElement()| that infers a label, if possible, from
* the value attribute when it is present and user has not typed in (if
......@@ -1554,6 +1573,12 @@ __gCrWeb.fill.inferLabelForElement = function(element) {
return inferredLabel;
}
// If we didn't find a placeholder, check for the aria-label case.
inferredLabel = __gCrWeb.fill.inferLabelFromAriaLabel(element);
if (__gCrWeb.fill.IsLabelValid(inferredLabel)) {
return inferredLabel;
}
// For all other searches that involve traversing up the tree, the search
// order is based on which tag is the closest ancestor to |element|.
var tagNames = __gCrWeb.fill.ancestorTagNames(element);
......
......@@ -241,7 +241,7 @@
</div>
<div class="row">
<div class="small-4 small-offset-1 columns">
<select name="creditCard.expMonth" id="rc-payment-card-month" aria-required="true" aria-label="Expiration" date="" month”="" title="overall type: CREDIT_CARD_EXP_MONTH
<select name="creditCard.expMonth" id="rc-payment-card-month" aria-required="true" aria-label="Expiration" date="" month”="" title="overall type: CREDIT_CARD_EXP_MONTH
server type: CREDIT_CARD_EXP_MONTH
heuristic type: CREDIT_CARD_EXP_MONTH
field signature: 989675451
......@@ -274,7 +274,7 @@
</select>
</div>
<div class="small-5 columns end">
<select name="creditCard.expYear" id="rc-payment-card-year" aria-required="true" aria-label="Expiration" date="" year”="" title="overall type: CREDIT_CARD_EXP_4_DIGIT_YEAR
<select name="creditCard.expYear" id="rc-payment-card-year" aria-required="true" aria-label="Expiration" date="" year”="" title="overall type: CREDIT_CARD_EXP_4_DIGIT_YEAR
server type: CREDIT_CARD_EXP_4_DIGIT_YEAR
heuristic type: CREDIT_CARD_EXP_4_DIGIT_YEAR
field signature: 891328465
......
UNKNOWN_TYPE | payment.type | Credit Card | CREDITCARD | payment.type_1-default
UNKNOWN_TYPE | payment.type | Choose Payment Method | PAYPAL | payment.type_1-default
UNKNOWN_TYPE | payment.type | Paypal | PAYPAL | payment.type_1-default
CREDIT_CARD_TYPE | creditCard.cardType.code | Card type | -1 | credit-card-cc
CREDIT_CARD_NUMBER | creditCard.cardNumber | Card number | | credit-card-cc
CREDIT_CARD_EXP_MONTH | creditCard.expMonth | Expiration date | 01 | credit-card-cc
CREDIT_CARD_EXP_4_DIGIT_YEAR | creditCard.expYear | Expiration date | 2016 | credit-card-cc
CREDIT_CARD_EXP_MONTH | creditCard.expMonth | Expiration | 01 | credit-card-cc
CREDIT_CARD_EXP_4_DIGIT_YEAR | creditCard.expYear | Expiration | 2016 | credit-card-cc
CREDIT_CARD_VERIFICATION_CODE | fake-password | Security code | | credit-card-cc
CREDIT_CARD_VERIFICATION_CODE | creditCard.securityCode | Security code | | credit-card-cc
UNKNOWN_TYPE | useMyShippingAddress | Use my shipping address | false | payment.type_1-default
......
UNKNOWN_TYPE | | Copy link | | _1-default
UNKNOWN_TYPE | | Copy link | | _1-default
UNKNOWN_TYPE | | Link to share | | _1-default
UNKNOWN_TYPE | | Link to share | | _1-default
UNKNOWN_TYPE | | Can edit Access level for invited people. | | _1-default
UNKNOWN_TYPE | | Enter names or email addresses... | | _1-default
UNKNOWN_TYPE | :z | Add a note | | _1-default
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment