Skip to content

Commit

Permalink
refactor: call findElementsInShadowTree in foundElements
Browse files Browse the repository at this point in the history
  • Loading branch information
dbajpeyi committed Nov 22, 2024
1 parent 05a38a7 commit b49bc6c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 41 deletions.
17 changes: 9 additions & 8 deletions dist/autofill-debug.js

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

17 changes: 9 additions & 8 deletions dist/autofill.js

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

14 changes: 7 additions & 7 deletions src/Form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,18 @@ class Form {
* If .elements doesn't work, it falls back to querySelectorAll.
* Doesn't look for shadow elements.
* @param {string} selector
* @returns
* @returns {Element[]}
*/
getFormElements(selector) {
// Some sites seem to be overriding `form.elements`, so we need to check if it's still iterable.
/** @type {Element[]|NodeListOf<Element>} element */
let formElements = [];
if (this.form instanceof HTMLFormElement && this.form.elements != null && Symbol.iterator in Object(this.form.elements)) {
return [...this.form.elements].filter((el) => el.matches(selector));
formElements = [...this.form.elements].filter((el) => el.matches(selector));
} else {
return this.form.querySelectorAll(selector);
formElements = this.form.querySelectorAll(selector);
}
return [...formElements, ...findElementsInShadowTree(this.form, selector)];
}

categorizeInputs() {
Expand All @@ -411,12 +414,9 @@ class Form {
this.addInput(this.form);
} else {
/** @type {Element[] | NodeList} */
let foundInputs = [];

const formElements = this.getFormElements(selector);

// Also scan the form for shadow elements
foundInputs = [...formElements, ...findElementsInShadowTree(this.form, selector)];
const foundInputs = this.getFormElements(selector);

if (foundInputs.length < MAX_INPUTS_PER_FORM) {
foundInputs.forEach((input) => this.addInput(input));
Expand Down
4 changes: 2 additions & 2 deletions src/Scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,8 @@ class DefaultScanner {
if (realTarget instanceof HTMLInputElement && !realTarget.hasAttribute(ATTR_INPUT_TYPE)) {
const parentForm = this.getParentForm(realTarget);

// If the parent form is an input element, we don't want to scan it further
if (parentForm instanceof HTMLInputElement) return;
// If the parent form is an input element or the same as the target, we don't want to scan it further.
if (parentForm instanceof HTMLInputElement || parentForm.isEqualNode(realTarget)) return;

const hasShadowTree = event.target?.shadowRoot != null;
const form = new Form(parentForm, realTarget, this.device, this.matching, this.shouldAutoprompt, hasShadowTree);
Expand Down
17 changes: 9 additions & 8 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.

17 changes: 9 additions & 8 deletions swift-package/Resources/assets/autofill.js

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

0 comments on commit b49bc6c

Please sign in to comment.