Skip to content

Commit

Permalink
PB-34314 Fix shadow-dom autofill fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Benj1er committed Aug 12, 2024
1 parent 15dce42 commit c71d329
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
18 changes: 6 additions & 12 deletions src/react-web-integration/Autofill/Autofill.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
import UserEventsService from "../lib/User/UserEventsService";
import InFormFieldSelector from "../lib/InForm/InFormFieldSelector";
import InFormCallToActionField from "../lib/InForm/InFormCallToActionField";

/**
* Fill the login form.
Expand All @@ -33,7 +34,7 @@ const fillForm = function(formData) {
}

// Get password element
const passwordElement = getPasswordElement(formData);
const passwordElement = getPasswordElement();
let usernameElement = null;
/*
* If password element exists
Expand Down Expand Up @@ -200,8 +201,8 @@ const findInputElementInIframe = function(type, iframeDocument) {
* Find the password element on the page.
* @return {HTMLInputElement/null}
*/
const getPasswordElement = function(formData) {
const passwordElements = document.querySelectorAll(InFormFieldSelector.PASSWORD_FIELD_SELECTOR);
const getPasswordElement = function() {
const passwordElements = InFormCallToActionField.findAll(InFormFieldSelector.PASSWORD_FIELD_SELECTOR);

// A password element has been found.
if (passwordElements.length) {
Expand All @@ -210,16 +211,9 @@ const getPasswordElement = function(formData) {
return passwordElement;
}
}
// If all passwords are hidden return null to autofill only the username input (PB-20173)
return null;
} else {
/*
* If no password element found on the page, the login form could be served by an iframe.
* Search the password element in the page iframes. By instance reddit.com login page serves its login
* form in an iframe.
*/
return getInputElementFromIframe('password', formData);
}
// If all passwords are hidden return null to autofill only the username input (PB-20173)
return null;
};

/**
Expand Down
9 changes: 3 additions & 6 deletions src/react-web-integration/lib/Dom/DomUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,16 @@ class DomUtils {
const filterByShadowRoot = element => element.shadowRoot ? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_SKIP;
const treeWalker = document.createTreeWalker(
document.activeElement,
document.body,
NodeFilter.SHOW_ELEMENT,
filterByShadowRoot
);
const shadowDomDocuments = [];
// Check directly the next node to not have the main document
let currentNode = treeWalker?.nextNode();
while (currentNode) {
const shadowDom = browser.dom?.openOrClosedShadowRoot(currentNode) || currentNode.shadowRoot;
while (treeWalker.nextNode()) {
const shadowDom = browser.dom?.openOrClosedShadowRoot(treeWalker.currentNode) || treeWalker.currentNode.shadowRoot;
if (shadowDom) {
shadowDomDocuments.push(shadowDom);
}
currentNode = treeWalker?.nextNode();
}
return shadowDomDocuments;
}
Expand Down

0 comments on commit c71d329

Please sign in to comment.