Skip to content

Commit

Permalink
refactor: run shadow scan only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
dbajpeyi committed Jul 16, 2024
1 parent b321a1f commit 6f19720
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 55 deletions.
23 changes: 13 additions & 10 deletions dist/autofill-debug.js

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

23 changes: 13 additions & 10 deletions dist/autofill.js

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

4 changes: 3 additions & 1 deletion src/Form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ class Form {
* @param {import("../DeviceInterface/InterfacePrototype").default} deviceInterface
* @param {import("../Form/matching").Matching} [matching]
* @param {Boolean} [shouldAutoprompt]
* @param {Boolean} [hasShadowInput]
*/
constructor (form, input, deviceInterface, matching, shouldAutoprompt = false) {
constructor (form, input, deviceInterface, matching, shouldAutoprompt = false, hasShadowInput = false) {
this.destroyed = false
this.form = form
this.matching = matching || createMatching()
this.formAnalyzer = new FormAnalyzer(form, input, matching)
this.device = deviceInterface
this.hasShadowInput = hasShadowInput

/** @type Record<'all' | SupportedMainTypes, Set> */
this.inputs = {
Expand Down
24 changes: 10 additions & 14 deletions src/Scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ class DefaultScanner {
stopped = false
/** @type {import("./Form/matching").Matching} matching */
matching
/** @type Map<HTMLInputElement, HTMLFormElement> */
shadowInputForm = new Map()

/**
* @param {import("./DeviceInterface/InterfacePrototype").default} device
Expand Down Expand Up @@ -164,12 +162,12 @@ class DefaultScanner {
return this
}
inputs.forEach((input) => this.addInput(input))
if (context instanceof HTMLFormElement) {
if (context instanceof HTMLFormElement && this.forms.get(context)?.hasShadowInput) {
const selector = this.matching.cssSelector('formInputsSelectorWithoutSelect')
const shadowElements = findEnclosedElements(context, selector)
shadowElements.forEach((input) => {
if (input instanceof HTMLInputElement) {
this.addInput(input)
this.addInput(input, context)
}
})
}
Expand Down Expand Up @@ -269,15 +267,12 @@ class DefaultScanner {

/**
* @param {HTMLInputElement|HTMLSelectElement} input
* @param {HTMLFormElement|null} form
*/
addInput (input) {
addInput (input, form = null) {
if (this.stopped) return

const parentForm = (input instanceof HTMLInputElement && this.shadowInputForm.has(input))
? this.shadowInputForm.get(input)
: this.getParentForm(input)

if (parentForm === undefined) return
const parentForm = form || this.getParentForm(input)

if (parentForm instanceof HTMLFormElement && this.forms.has(parentForm)) {
const foundForm = this.forms.get(parentForm)
Expand Down Expand Up @@ -440,10 +435,11 @@ class DefaultScanner {
realTarget instanceof HTMLInputElement &&
!realTarget.hasAttribute(ATTR_INPUT_TYPE)
) {
const form = this.getParentForm(realTarget)
if (form && form instanceof HTMLFormElement) {
this.shadowInputForm.set(realTarget, form)
this.findEligibleInputs(form)
const parentForm = this.getParentForm(realTarget)
if (parentForm && parentForm instanceof HTMLFormElement) {
const form = new Form(parentForm, realTarget, this.device, this.matching, this.shouldAutoprompt, true)
this.forms.set(parentForm, form)
this.findEligibleInputs(parentForm)
}
}

Expand Down
23 changes: 13 additions & 10 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.

23 changes: 13 additions & 10 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 6f19720

Please sign in to comment.