Skip to content

Commit

Permalink
fix: check for custom web elements
Browse files Browse the repository at this point in the history
  • Loading branch information
dbajpeyi committed Nov 15, 2024
1 parent 9764283 commit 42564d9
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 61 deletions.
33 changes: 21 additions & 12 deletions dist/autofill-debug.js

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

33 changes: 21 additions & 12 deletions dist/autofill.js

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

14 changes: 13 additions & 1 deletion src/Form/FormAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ class FormAnalyzer {
});
}

/**
* Checks if the element is present in the cusotm elements registry and ends with a '-link' suffix
* @param {any} el
* @returns
*/
isCustomWebElementLink(el) {
const tagName = el.nodeName.toLowerCase();
const isCustomElement = customElements != null && customElements.get(tagName) != null;
return isCustomElement && /-link$/.test(tagName) && findEnclosedShadowElements(el, 'a').length > 0;
}

evaluateElement(el) {
const string = getTextShallow(el);

Expand Down Expand Up @@ -262,7 +273,8 @@ class FormAnalyzer {
if (
(el instanceof HTMLAnchorElement && el.href && el.getAttribute('href') !== '#') ||
(el.getAttribute('role') || '').toUpperCase() === 'LINK' ||
el.matches('button[class*=secondary]')
el.matches('button[class*=secondary]') ||
this.isCustomWebElementLink(el)
) {
let shouldFlip = true;
let strength = 1;
Expand Down
14 changes: 6 additions & 8 deletions src/Scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class DefaultScanner {
return this;
}
inputs.forEach((input) => this.addInput(input));
if (context instanceof HTMLFormElement && this.forms.get(context)?.hasShadowTree && inputs.length === 0) {
if (context instanceof HTMLFormElement && this.forms.get(context)?.hasShadowTree) {
findEnclosedShadowElements(context, formInputsSelectorWithoutSelect).forEach((input) => {
if (input instanceof HTMLInputElement) {
this.addInput(input, context);
Expand Down Expand Up @@ -239,7 +239,7 @@ class DefaultScanner {
let traversalLayerCount = 0;
let element = input;
// traverse the DOM to search for related inputs
while (traversalLayerCount <= 5 && element.parentElement !== document.documentElement) {
while (traversalLayerCount <= 10 && element.parentElement !== document.documentElement) {
// Avoid overlapping containers or forms
const siblingForm = element.parentElement?.querySelector('form');
if (siblingForm && siblingForm !== element) {
Expand Down Expand Up @@ -436,12 +436,10 @@ class DefaultScanner {
// find the enclosing parent form, and scan it.
if (realTarget instanceof HTMLInputElement && !realTarget.hasAttribute(ATTR_INPUT_TYPE)) {
const parentForm = this.getParentForm(realTarget);
if (parentForm && parentForm instanceof HTMLFormElement) {
const hasShadowTree = event.target?.shadowRoot != null;
const form = new Form(parentForm, realTarget, this.device, this.matching, this.shouldAutoprompt, hasShadowTree);
this.forms.set(parentForm, form);
this.findEligibleInputs(parentForm);
}
const hasShadowTree = event.target?.shadowRoot != null;
const form = new Form(parentForm, realTarget, this.device, this.matching, this.shouldAutoprompt, hasShadowTree);
this.forms.set(parentForm, form);
this.findEligibleInputs(parentForm);
}

window.performance?.mark?.('scan_shadow:init:end');
Expand Down
7 changes: 3 additions & 4 deletions src/autofill-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,17 +567,16 @@ function getActiveElement(root = document) {
}

/**
* Takes a root element and tries to find visible elements first, and if it fails, it tries to find shadow elements
* Takes a root element and tries to find elements in shadow DOMs that match the selector
* @param {HTMLElement|HTMLFormElement} root
* @param {string} selector
* @returns {Element[]}
*/
function findEnclosedShadowElements(root, selector) {
// Check if there are any shadow elements that match the selector
const shadowElements = [];
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);

let node = walker.nextNode();
/** @type {Node|null} */
let node = walker.currentNode;
while (node) {
if (node instanceof HTMLElement && node.shadowRoot) {
shadowElements.push(...node.shadowRoot.querySelectorAll(selector));
Expand Down
33 changes: 21 additions & 12 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.

Loading

0 comments on commit 42564d9

Please sign in to comment.