From 2b779cc39ab1d4765cc639083c447a9b6c7613e9 Mon Sep 17 00:00:00 2001 From: dbajpeyi Date: Wed, 7 Aug 2024 15:14:07 +0900 Subject: [PATCH] fix: check if form elements are iterable --- dist/autofill-debug.js | 5 +++-- dist/autofill.js | 5 +++-- src/Form/Form.js | 10 +++++++--- swift-package/Resources/assets/autofill-debug.js | 5 +++-- swift-package/Resources/assets/autofill.js | 5 +++-- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/dist/autofill-debug.js b/dist/autofill-debug.js index a3186e91a..8baddfebd 100644 --- a/dist/autofill-debug.js +++ b/dist/autofill-debug.js @@ -10079,12 +10079,13 @@ class Form { } else { /** @type {Element[] | NodeList} */ let foundInputs = []; - if (this.form instanceof HTMLFormElement) { + // Some sites seem to be overriding `form.elements`, so we need to check if it's still iterable. + if (this.form instanceof HTMLFormElement && this.form.elements != null && Symbol.iterator in Object(this.form.elements)) { // For form elements we use .elements to catch fields outside the form itself using the form attribute. // It also catches all elements when the markup is broken. // We use .filter to avoid fieldset, button, textarea etc. const formElements = [...this.form.elements].filter(el => el.matches(selector)); - // If there are not form elements, we try to look for all + // If there are no form elements, we try to look for all // enclosed elements within the form. foundInputs = formElements.length > 0 ? formElements : (0, _autofillUtils.findEnclosedElements)(this.form, selector); } else { diff --git a/dist/autofill.js b/dist/autofill.js index 7feb30ad3..97c5ed0b6 100644 --- a/dist/autofill.js +++ b/dist/autofill.js @@ -5913,12 +5913,13 @@ class Form { } else { /** @type {Element[] | NodeList} */ let foundInputs = []; - if (this.form instanceof HTMLFormElement) { + // Some sites seem to be overriding `form.elements`, so we need to check if it's still iterable. + if (this.form instanceof HTMLFormElement && this.form.elements != null && Symbol.iterator in Object(this.form.elements)) { // For form elements we use .elements to catch fields outside the form itself using the form attribute. // It also catches all elements when the markup is broken. // We use .filter to avoid fieldset, button, textarea etc. const formElements = [...this.form.elements].filter(el => el.matches(selector)); - // If there are not form elements, we try to look for all + // If there are no form elements, we try to look for all // enclosed elements within the form. foundInputs = formElements.length > 0 ? formElements : (0, _autofillUtils.findEnclosedElements)(this.form, selector); } else { diff --git a/src/Form/Form.js b/src/Form/Form.js index 0da080109..e8bd13a5d 100644 --- a/src/Form/Form.js +++ b/src/Form/Form.js @@ -400,13 +400,17 @@ class Form { } else { /** @type {Element[] | NodeList} */ let foundInputs = [] - - if (this.form instanceof HTMLFormElement) { + // Some sites seem to be overriding `form.elements`, so we need to check if it's still iterable. + if ( + this.form instanceof HTMLFormElement && + this.form.elements != null && + Symbol.iterator in Object(this.form.elements) + ) { // For form elements we use .elements to catch fields outside the form itself using the form attribute. // It also catches all elements when the markup is broken. // We use .filter to avoid fieldset, button, textarea etc. const formElements = [...this.form.elements].filter((el) => el.matches(selector)) - // If there are not form elements, we try to look for all + // If there are no form elements, we try to look for all // enclosed elements within the form. foundInputs = formElements.length > 0 ? formElements diff --git a/swift-package/Resources/assets/autofill-debug.js b/swift-package/Resources/assets/autofill-debug.js index a3186e91a..8baddfebd 100644 --- a/swift-package/Resources/assets/autofill-debug.js +++ b/swift-package/Resources/assets/autofill-debug.js @@ -10079,12 +10079,13 @@ class Form { } else { /** @type {Element[] | NodeList} */ let foundInputs = []; - if (this.form instanceof HTMLFormElement) { + // Some sites seem to be overriding `form.elements`, so we need to check if it's still iterable. + if (this.form instanceof HTMLFormElement && this.form.elements != null && Symbol.iterator in Object(this.form.elements)) { // For form elements we use .elements to catch fields outside the form itself using the form attribute. // It also catches all elements when the markup is broken. // We use .filter to avoid fieldset, button, textarea etc. const formElements = [...this.form.elements].filter(el => el.matches(selector)); - // If there are not form elements, we try to look for all + // If there are no form elements, we try to look for all // enclosed elements within the form. foundInputs = formElements.length > 0 ? formElements : (0, _autofillUtils.findEnclosedElements)(this.form, selector); } else { diff --git a/swift-package/Resources/assets/autofill.js b/swift-package/Resources/assets/autofill.js index 7feb30ad3..97c5ed0b6 100644 --- a/swift-package/Resources/assets/autofill.js +++ b/swift-package/Resources/assets/autofill.js @@ -5913,12 +5913,13 @@ class Form { } else { /** @type {Element[] | NodeList} */ let foundInputs = []; - if (this.form instanceof HTMLFormElement) { + // Some sites seem to be overriding `form.elements`, so we need to check if it's still iterable. + if (this.form instanceof HTMLFormElement && this.form.elements != null && Symbol.iterator in Object(this.form.elements)) { // For form elements we use .elements to catch fields outside the form itself using the form attribute. // It also catches all elements when the markup is broken. // We use .filter to avoid fieldset, button, textarea etc. const formElements = [...this.form.elements].filter(el => el.matches(selector)); - // If there are not form elements, we try to look for all + // If there are no form elements, we try to look for all // enclosed elements within the form. foundInputs = formElements.length > 0 ? formElements : (0, _autofillUtils.findEnclosedElements)(this.form, selector); } else {