Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Credentials] Autofill when elements are in shadow #592

Merged
merged 22 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: only discover shadow elements when needed
  • Loading branch information
dbajpeyi committed Jul 16, 2024
commit 1c876a6b62268d45e7089ebb9b0761801495d9f9
32 changes: 22 additions & 10 deletions dist/autofill-debug.js

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

32 changes: 22 additions & 10 deletions dist/autofill.js

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

13 changes: 6 additions & 7 deletions src/Form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
shouldLog,
safeRegexTest,
getActiveElement,
findEnclosedShadowElements
findEnclosedElements
} from '../autofill-utils.js'

import {getInputSubtype, getInputMainType, createMatching, getInputVariant} from './matching.js'
Expand Down Expand Up @@ -423,10 +423,10 @@ class Form {
// It also catches all elements when the markup is broken.
// We use .filter to avoid fieldset, button, textarea etc.
// Additionally, we try to find any shadow elements that might be there in the form.
foundInputs = [
...this.form.elements,
...findEnclosedShadowElements(this.form, selector)
].filter(el => el.matches(selector))
const formElements = [...this.form.elements]
foundInputs = formElements.length > 0
? formElements.filter((el) => el.matches(selector))
: findEnclosedElements(this.form, selector)
} else {
foundInputs = this.form.querySelectorAll(selector)
}
Expand All @@ -450,8 +450,7 @@ class Form {
get submitButtons () {
const selector = this.matching.cssSelector('submitButtonSelector')
const allButtons = /** @type {HTMLElement[]} */([
...this.form.querySelectorAll(selector),
...findEnclosedShadowElements(this.form, selector)
...findEnclosedElements(this.form, selector)
])

return allButtons
Expand Down
7 changes: 2 additions & 5 deletions src/Form/FormAnalyzer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { removeExcessWhitespace, Matching } from './matching.js'
import { constants } from '../constants.js'
import { matchingConfiguration } from './matching-config/__generated__/compiled-matching-config.js'
import {findEnclosedShadowElements, getTextShallow, isLikelyASubmitButton, safeRegexTest} from '../autofill-utils.js'
import {findEnclosedElements, getTextShallow, isLikelyASubmitButton, safeRegexTest} from '../autofill-utils.js'

class FormAnalyzer {
/** @type HTMLElement */
Expand Down Expand Up @@ -301,10 +301,7 @@ class FormAnalyzer {

// Check form contents (noisy elements are skipped with the safeUniversalSelector)
const selector = this.matching.cssSelector('safeUniversalSelector')
const formElements = [
...this.form.querySelectorAll(selector),
...findEnclosedShadowElements(this.form, selector)
]
const formElements = findEnclosedElements(this.form, selector)
for (let i = 0; i < formElements.length; i++) {
// Safety cutoff to avoid huge DOMs freezing the browser
if (i >= 200) break
Expand Down
7 changes: 4 additions & 3 deletions src/Scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
logPerformance,
isFormLikelyToBeUsedAsPageWrapper,
shouldLog, pierceShadowTree,
findEnclosedShadowElements
findEnclosedElements
} from './autofill-utils.js'
import { AddDebugFlagCall } from './deviceApiCalls/__generated__/deviceApiCalls.js'

Expand Down Expand Up @@ -157,15 +157,16 @@ class DefaultScanner {
if ('matches' in context && context.matches?.(this.matching.cssSelector('formInputsSelectorWithoutSelect'))) {
this.addInput(context)
} else {
const inputs = context.querySelectorAll(this.matching.cssSelector('formInputsSelectorWithoutSelect'))
const selector = this.matching.cssSelector('formInputsSelectorWithoutSelect')
const inputs = context.querySelectorAll(selector)
if (inputs.length > this.options.maxInputsPerPage) {
this.stopScanner(`Too many input fields in the given context (${inputs.length}), stop scanning`, context)
return this
}
inputs.forEach((input) => this.addInput(input))
if (context instanceof HTMLFormElement) {
shakyShane marked this conversation as resolved.
Show resolved Hide resolved
const selector = this.matching.cssSelector('formInputsSelectorWithoutSelect')
const shadowElements = findEnclosedShadowElements(context, selector)
const shadowElements = findEnclosedElements(context, selector)
shadowElements.forEach((input) => {
if (input instanceof HTMLInputElement) {
this.addInput(input)
Expand Down
19 changes: 14 additions & 5 deletions src/autofill-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,20 @@ function getActiveElement (root = document) {
}

/**
* Takes a root, creates a treewalker and finds all shadow elements that match the selector
* @param {HTMLElement} root
* Takes a form element, creates a treewalker and finds all shadow elements that match the selector
* @param {HTMLElement|HTMLFormElement} root
* @param {string} selector
* @returns {Element[]}
* @returns {HTMLElement[]}
*/
function findEnclosedShadowElements (root, selector) {
function findEnclosedElements (root, selector) {
// Check if there are any normal elements that match the selector
const elements = root.querySelectorAll(selector)
if (elements.length > 0) {
/** @ts-ignore */
return elements
}

// Check if there are any shadow elements that match the selector
const shadowElements = []
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT)

Expand All @@ -584,6 +592,7 @@ function findEnclosedShadowElements (root, selector) {
node = walker.nextNode()
}

/** @ts-ignore */
return shadowElements
}

Expand Down Expand Up @@ -619,5 +628,5 @@ export {
safeRegexTest,
pierceShadowTree,
getActiveElement,
findEnclosedShadowElements
findEnclosedElements
}
32 changes: 22 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.

Loading
Loading