Skip to content

Commit

Permalink
Consider aria label values for all form inputs and buttons when searc…
Browse files Browse the repository at this point in the history
…hing for the primary form submit button

Previously this would only be considered for elements that were under
consideration only because of an aria role being present on the element.
  • Loading branch information
luckyrat committed Jul 3, 2020
1 parent 320ce55 commit 828b406
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions page/formFilling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,28 @@ export class FormFilling {

minScoreToWin = score;
}

function resolveAriaLabelValues(element: HTMLElement) {
const labels = [];
if (element.hasAttribute("aria-label")) {
labels.push(element.getAttribute("aria-label").toLowerCase());
}
element
.getAttribute("aria-labelledby")
?.trim()
.split(" ")
.forEach(id => {
if (id) {
const labelElement = form.ownerDocument.getElementById(id);
if (labelElement && labelElement.innerText) {
labels.push(labelElement.innerText.toLowerCase());
}
}
});

return labels;
}

/*
In Firefox Array.from...foreach is about 50% faster than the alternative in this comment below... but 20% slower in Chrome.
const buttons = form.ownerDocument.getElementsByTagName("button");
Expand All @@ -1252,6 +1274,7 @@ export class FormFilling {
if (value.name) semanticValues.push(value.name.toLowerCase());
if (value.textContent) semanticValues.push(value.textContent.toLowerCase());
if (value.value) semanticValues.push(value.value.toLowerCase());
semanticValues.push(...resolveAriaLabelValues(value));

let score = this.scoreAdjustmentForMagicWords(
semanticValues,
Expand Down Expand Up @@ -1285,9 +1308,14 @@ export class FormFilling {

// Names are more important but sometimes they don't exist or are random
// so check what is actually displayed to the user
const semanticValues = [];
if (value.value) {
semanticValues.push(value.value.toLowerCase());
}
semanticValues.push(...resolveAriaLabelValues(value));
if (semanticValues.length > 0) {
semanticScore += this.scoreAdjustmentForMagicWords(
[value.value.toLowerCase()],
semanticValues,
40,
this.semanticWhitelistCache,
this.semanticBlacklistCache
Expand Down Expand Up @@ -1334,21 +1362,7 @@ export class FormFilling {
semanticValues.push(value.dataSet.tooltip.toLowerCase());
}
}
if (value.hasAttribute("aria-label")) {
semanticValues.push(value.getAttribute("aria-label").toLowerCase());
}
value
.getAttribute("aria-labelledby")
?.trim()
.split(" ")
.forEach(id => {
if (id) {
const labelElement = form.ownerDocument.getElementById(id);
if (labelElement && labelElement.innerText) {
semanticValues.push(labelElement.innerText);
}
}
});
semanticValues.push(...resolveAriaLabelValues(value));

let score = this.scoreAdjustmentForMagicWords(
semanticValues,
Expand Down

0 comments on commit 828b406

Please sign in to comment.