1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-06-26 10:35:48 +02:00
bitwarden-browser/apps/browser/src/autofill/content
Rafael Kraut 208be8dfbf
[PS-1918] Make autofill doc-scanner traverse into ShadowRoot (#4119)
* This commit implements the following main changes:

- Query elements by using a TreeWalker instead of `document.querySelector[All]`. The reason for this is that `querySelector[All]` doesn't traverse into elements with ShadowRoot.
- Recursively traverse into elements with `openOrClosedShadowRoot` or `Element.shadowRoot` (depending on browser support) inside TreeWalker loop.
- Use new query logic everywhere inside `autofill.js`. This also means we need to use filter functions to find elements with specific nodeNames and/or attributes instead of CSS selector strings.
- Add two new `instanceof Element` checks to prevent `Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'." errors`.

This change is fully backward compatible. If `openOrClosedShadowRoot` is not available it will always return undefined and we will never traverse into ShadowRoots just as the behavior was before this change.

* refactor: outsource recursive logic to accumulatingQueryDocAll

We don't want the `els` argument on the `queryDocAll` function because it's never used from outside the function itself. Thus the recursive logic is moved to `accumulatingQueryDocAll`.
Now `queryDocAll` creates an empty array and passes it to `accumulatingQueryDocAll` which recursively walks the document and all ShadowRoots and pushes all found nodes directly to the referenced array.

The decision to use a directly mutated array instead of `Array.concat(els)` or `Array.push(...els)` is for performance reasons. Pushing to the referenced array was 74% faster than using `Array.push` with spread operator and even 90% faster than using `Array.concat`.

Co-authored-by: Chad Miller <64046472+chadm-sq@users.noreply.github.com>

* refactor: extract input field relevance check into own function

Addresses CodeScene analysis violation "Bumpy Road Ahead" where conditional logic is checked for a nesting of 2 or deeper.

* refactor: use proper element attribute handling

- use el.type attribute instead of el.attribute.type on input elements. This makes sure we also get 'text' when type attribute is not explicitly specified
- use el.htmlFor attribute instead of el.attribute.for on label elements
- use `hasAttribute` and `getAttribute` methods instead of `attributes[]` which is discouraged by https://quirksmode.org/dom/core/#attributes
- improve readability of `isRelevantInputField`

---------

Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com>
Co-authored-by: Chad Miller <64046472+chadm-sq@users.noreply.github.com>
Co-authored-by: Thomas Rittson <trittson@bitwarden.com>
2023-02-20 08:43:18 +10:00
..
autofill.css [SG-998] and [SG-999] Vault and Autofill team refactor (#4542) 2023-01-31 16:08:37 -05:00
autofill.js [PS-1918] Make autofill doc-scanner traverse into ShadowRoot (#4119) 2023-02-20 08:43:18 +10:00
autofiller.ts [SG-998] and [SG-999] Vault and Autofill team refactor (#4542) 2023-01-31 16:08:37 -05:00
context-menu-handler.ts [SG-998] and [SG-999] Vault and Autofill team refactor (#4542) 2023-01-31 16:08:37 -05:00
message_handler.ts [SG-998] and [SG-999] Vault and Autofill team refactor (#4542) 2023-01-31 16:08:37 -05:00
misc-utils.ts [SG-998] and [SG-999] Vault and Autofill team refactor (#4542) 2023-01-31 16:08:37 -05:00
notification-bar.ts [SG-998] and [SG-999] Vault and Autofill team refactor (#4542) 2023-01-31 16:08:37 -05:00