Skip to content

Commit

Permalink
fix: check for outbound links
Browse files Browse the repository at this point in the history
  • Loading branch information
dbajpeyi committed Jan 14, 2025
1 parent 85b0b7b commit 6e1ef70
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 50 deletions.
30 changes: 21 additions & 9 deletions dist/autofill-debug.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 21 additions & 9 deletions dist/autofill.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 27 additions & 13 deletions src/Form/FormAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,25 +231,28 @@ class FormAnalyzer {
}

/**
* Function that checks if the element is an external link or a custom web element that
* encapsulates a link.
* Function that checks if the element is link like and navigating away from the current page
* @param {any} el
* @returns {boolean}
*/
isElementExternalLink(el) {
isOutboundLink(el) {
// Checks if the element is present in the cusotm elements registry and ends with a '-link' suffix.
// If it does, it checks if it contains an anchor element inside.
const tagName = el.nodeName.toLowerCase();
const isCustomWebElementLink =
customElements?.get(tagName) != null && /-link$/.test(tagName) && findElementsInShadowTree(el, 'a').length > 0;

// if an external link matches one of the regexes, we assume the match is not pertinent to the current form
const isElementLink =
(el instanceof HTMLAnchorElement && el.href && el.getAttribute('href') !== '#') ||
(el.getAttribute('role') || '').toUpperCase() === 'LINK' ||
el.matches('button[class*=secondary]');

return isCustomWebElementLink || isElementLink;
const isElementLikelyALink = (el) => {
if (el == null) return false;
return (
(el instanceof HTMLAnchorElement && el.href && el.getAttribute('href') !== '#') ||
(el.getAttribute('role') || '').toUpperCase() === 'LINK' ||
el.matches('button[class*=secondary]')
);
};

return isCustomWebElementLink || isElementLikelyALink(el) || isElementLikelyALink(el.closest('a'));
}

evaluateElement(el) {
Expand Down Expand Up @@ -278,15 +281,26 @@ class FormAnalyzer {
}
});
} else {
// Here we don't think this is a submit, so if there is another submit in the form, flip the score
const thereIsASubmitButton = Boolean(this.form.querySelector('input[type=submit], button[type=submit]'));
shouldFlip = thereIsASubmitButton && this.shouldFlipScoreForButtonText(string);
// Here we don't think this is a submit, so determine if we should flip the score
const hasAnotherSubmitButton = Boolean(this.form.querySelector('input[type=submit], button[type=submit]'));
const buttonText = string;

if (hasAnotherSubmitButton) {
// If there's another submit button, flip based on text content alone
shouldFlip = this.shouldFlipScoreForButtonText(buttonText);
} else {
// With no submit button, only flip if it's an outbound link that navigates away, and also match the text
// Here we want to be more conservative, because we don't want to flip for every link given that there was no
// submit button detected on the form, hence the extra check for the link.
const isOutboundLink = this.isOutboundLink(el);
shouldFlip = isOutboundLink && this.shouldFlipScoreForButtonText(buttonText);
}
}
const strength = likelyASubmit ? 20 : 4;
this.updateSignal({ string, strength, signalType: `button: ${string}`, shouldFlip });
return;
}
if (this.isElementExternalLink(el)) {
if (this.isOutboundLink(el)) {
let shouldFlip = true;
let strength = 1;
// Don't flip forgotten password links
Expand Down
30 changes: 21 additions & 9 deletions swift-package/Resources/assets/autofill-debug.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 21 additions & 9 deletions swift-package/Resources/assets/autofill.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test-forms/fedex_login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 2 additions & 1 deletion test-forms/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -551,5 +551,6 @@
{ "html": "deltamath_reset_password.html"},
{ "html": "freshdirect_com_login.html"},
{ "html": "auth_wetransfer_com_login.html"},
{ "html": "revolut_business_login.html"}
{ "html": "revolut_business_login.html"},
{ "html": "fedex_login.html"}
]

0 comments on commit 6e1ef70

Please sign in to comment.