Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add shadowdom support to autofill form field detection (#1134)

Leendert de Borstb638e3375d31
diff --git a/apps/browser-extension/src/entrypoints/contentScript/Form.ts b/apps/browser-extension/src/entrypoints/contentScript/Form.ts
index 8d4ec58ac..dccf7fff7 100644
--- a/apps/browser-extension/src/entrypoints/contentScript/Form.ts
+++ b/apps/browser-extension/src/entrypoints/contentScript/Form.ts
@@ -32,6 +32,8 @@ export function hidePopupFor(ms: number) : void {
/**
* Validates if an element is a supported input field that can be processed for autofill.
+ * This function supports regular input elements, custom elements with type attributes,
+ * and custom web components that may contain shadow DOM.
* @param element The element to validate
* @returns An object containing validation result and the element cast as HTMLInputElement if valid
*/
@@ -42,14 +44,30 @@ export function validateInputField(element: Element | null): { isValid: boolean;
const textInputTypes = ['text', 'email', 'tel', 'password', 'search', 'url', 'number'];
const elementType = element.getAttribute('type');
- const isInputElement = element.tagName.toLowerCase() === 'input';
+ const tagName = element.tagName.toLowerCase();
+ const isInputElement = tagName === 'input';
+
+ // Check if element has shadow DOM with input elements
+ const elementWithShadow = element as HTMLElement & { shadowRoot?: ShadowRoot };
+ const hasShadowDOMInput = elementWithShadow.shadowRoot &&
+ elementWithShadow.shadowRoot.querySelector('input, textarea');
+
+ // Check if it's a custom element that might be an input
+ const isLikelyCustomInputElement = tagName.includes('-') && (
+ tagName.includes('input') ||
+ tagName.includes('field') ||
+ tagName.includes('text') ||
+ hasShadowDOMInput
+ );
// Check if it's a valid input field we should process
const isValid = (
// Case 1: It's an input element (with either explicit type or defaulting to "text")
(isInputElement && (!elementType || textInputTypes.includes(elementType?.toLowerCase() ?? ''))) ||
// Case 2: Non-input element but has valid type attribute
- (!isInputElement && elementType && textInputTypes.includes(elementType.toLowerCase()))
+ (!isInputElement && elementType && textInputTypes.includes(elementType.toLowerCase())) ||
+ // Case 3: It's a custom element that likely contains an input
+ (isLikelyCustomInputElement)
) as boolean;
return {
@@ -98,7 +116,7 @@ function findActualInput(element: HTMLElement): HTMLInputElement {
return element as HTMLInputElement;
}
- // Try to find a visible child input
+ // Try to find a visible child input in regular DOM
const childInput = element.querySelector('input');
if (childInput) {
const style = window.getComputedStyle(childInput);
@@ -107,6 +125,17 @@ function findActualInput(element: HTMLElement): HTMLInputElement {
}
}
+ // Try to find input in shadow DOM if element has shadowRoot
+ if (element.shadowRoot) {
+ const shadowInput = element.shadowRoot.querySelector('input');
+ if (shadowInput) {
+ const style = window.getComputedStyle(shadowInput);
+ if (style.display !== 'none' && style.visibility !== 'hidden') {
+ return shadowInput as HTMLInputElement;
+ }
+ }
+ }
+
// Fallback to the provided element if no child input found
return element as HTMLInputElement;
}
diff --git a/apps/browser-extension/src/utils/formDetector/FormDetector.ts b/apps/browser-extension/src/utils/formDetector/FormDetector.ts
index d0930a22e..cf3328bd1 100644
--- a/apps/browser-extension/src/utils/formDetector/FormDetector.ts
+++ b/apps/browser-extension/src/utils/formDetector/FormDetector.ts
@@ -169,6 +169,37 @@ export class FormDetector {
return this.clickedElement?.closest('form, [role="dialog"]') as HTMLElement | null;
}
+ /**
+ * Get the actual input element from a potentially custom element.
+ * This handles any element with shadow DOM containing input elements.
+ * @param element The element to check (could be a custom element or regular input)
+ * @returns The actual input element, or the original element if no nested input is found
+ */
+ private getActualInputElement(element: HTMLElement): HTMLElement {
+ // If it's already an input, return it
+ if (element.tagName.toLowerCase() === 'input') {
+ return element;
+ }
+
+ // Check for shadow DOM input (generic approach)
+ const elementWithShadow = element as HTMLElement & { shadowRoot?: ShadowRoot };
+ if (elementWithShadow.shadowRoot) {
+ const shadowInput = elementWithShadow.shadowRoot.querySelector('input, textarea') as HTMLElement;
+ if (shadowInput) {
+ return shadowInput;
+ }
+ }
+
+ // Check for regular child input (non-shadow DOM)
+ const childInput = element.querySelector('input, textarea') as HTMLElement;
+ if (childInput) {
+ return childInput;
+ }
+
+ // Return the original element if no nested input found
+ return element;
+ }
+
/**
* Check if an element and all its parents are visible.
* This checks for display:none, visibility:hidden, and opacity:0
@@ -257,10 +288,32 @@ export class FormDetector {
types: string[],
excludeElements: HTMLInputElement[] = []
): HTMLInputElement[] {
- // Query for both standard input elements and any element with a type attribute
- const candidates = form
- ? form.querySelectorAll<HTMLElement>('input, select, [type]')
- : this.document.querySelectorAll<HTMLElement>('input, select, [type]');
+ // Query for standard input elements, select elements, and elements with type attributes
+ const standardCandidates = form
+ ? Array.from(form.querySelectorAll<HTMLElement>('input, select, [type]'))
+ : Array.from(this.document.querySelectorAll<HTMLElement>('input, select, [type]'));
+
+ /**
+ * Also find any custom elements that might contain shadow DOM inputs
+ * Look for elements with shadow roots that contain input elements
+ */
+ const allElements = form
+ ? Array.from(form.querySelectorAll<HTMLElement>('*'))
+ : Array.from(this.document.querySelectorAll<HTMLElement>('*'));
+
+ const shadowDOMCandidates = allElements.filter(el => {
+ // Check if element has shadow DOM with input elements
+ const elementWithShadow = el as HTMLElement & { shadowRoot?: ShadowRoot };
+ if (elementWithShadow.shadowRoot) {
+ const shadowInput = elementWithShadow.shadowRoot.querySelector('input, textarea');
+ return shadowInput !== null;
+ }
+ return false;
+ });
+
+ // Combine and deduplicate candidates
+ const allCandidates = [...standardCandidates, ...shadowDOMCandidates];
+ const candidates = allCandidates.filter((el, index, arr) => arr.indexOf(el) === index);
const matches: { input: HTMLInputElement; score: number }[] = [];
@@ -274,12 +327,32 @@ export class FormDetector {
}
// Get type from either the element's type property or its type attribute
- const type = input.tagName.toLowerCase() === 'select'
+ const tagName = input.tagName.toLowerCase();
+ let type = tagName === 'select'
? 'select'
: (input as HTMLInputElement).type?.toLowerCase() || input.getAttribute('type')?.toLowerCase() || '';
+ // Check if element has shadow DOM with input elements (generic detection)
+ const elementWithShadow = input as HTMLElement & { shadowRoot?: ShadowRoot };
+ const hasShadowDOMInput = elementWithShadow.shadowRoot &&
+ elementWithShadow.shadowRoot.querySelector('input, textarea');
+
+ // For elements with shadow DOM, get the type from the actual input inside
+ if (hasShadowDOMInput && !type) {
+ const shadowInput = elementWithShadow.shadowRoot!.querySelector('input, textarea') as HTMLInputElement;
+ if (shadowInput) {
+ type = shadowInput.type?.toLowerCase() || 'text';
+ }
+ }
+
+ // Check if this element should be considered based on type matching
if (!types.includes(type)) {
- continue;
+ // For shadow DOM elements, allow if we're looking for text and it contains an input
+ if (hasShadowDOMInput && types.includes('text') && !type) {
+ // This is a shadow DOM element without explicit type, treat as text input
+ } else {
+ continue;
+ }
}
if (types.includes('email') && type === 'email') {
@@ -303,6 +376,34 @@ export class FormDetector {
}
}
+ /**
+ * Check for slot-based labels (e.g., <span slot="label">Email or username</span>)
+ * Look for slot elements within the input's parent hierarchy
+ */
+ let slotParent: HTMLElement | null = input;
+ for (let depth = 0; depth < 3 && slotParent; depth++) {
+ const slotElements = slotParent.querySelectorAll('[slot="label"], [slot="helper-text"]');
+ for (const slotEl of Array.from(slotElements)) {
+ const slotText = slotEl.textContent?.toLowerCase() ?? '';
+ if (slotText) {
+ attributesToCheck.push(slotText);
+ }
+ }
+ /** Also check if the parent itself is a custom element with slots */
+ if (slotParent.shadowRoot) {
+ const shadowSlots = slotParent.shadowRoot.querySelectorAll('slot[name="label"], slot[name="helper-text"]');
+ for (const slot of Array.from(shadowSlots)) {
+ const assignedNodes = (slot as HTMLSlotElement).assignedNodes();
+ for (const node of assignedNodes) {
+ if (node.textContent) {
+ attributesToCheck.push(node.textContent.toLowerCase());
+ }
+ }
+ }
+ }
+ slotParent = slotParent.parentElement;
+ }
+
// Check for sibling elements with class containing "label"
const parent = input.parentElement;
if (parent) {
@@ -631,21 +732,34 @@ export class FormDetector {
// Check if it's a username, email or password field by reusing the existing detection logic
const formWrapper = this.getFormWrapper();
- // Check if the clicked element is a username field.
+ if (!this.clickedElement) {
+ return false;
+ }
+
+ // Get the actual input element (handles shadow DOM)
+ const actualElement = this.getActualInputElement(this.clickedElement);
+
+ // Check both the clicked element and the actual input element
+ const elementsToCheck = [this.clickedElement, actualElement].filter((el, index, arr) =>
+ el && arr.indexOf(el) === index // Remove duplicates
+ );
+
+ // Check if any of the elements is a username field
const usernameFields = this.findAllInputFields(formWrapper as HTMLFormElement | null, CombinedFieldPatterns.username, ['text']);
- if (usernameFields.some(input => input === this.clickedElement)) {
+ if (usernameFields.some(input => elementsToCheck.includes(input))) {
return true;
}
- // Check if the clicked element is a password field.
+ // Check if any of the elements is a password field
const passwordField = this.findPasswordField(formWrapper as HTMLFormElement | null);
- if (passwordField.primary === this.clickedElement || passwordField.confirm === this.clickedElement) {
+ if ((passwordField.primary && elementsToCheck.includes(passwordField.primary)) ||
+ (passwordField.confirm && elementsToCheck.includes(passwordField.confirm))) {
return true;
}
- // Check if the clicked element is an email field.
+ // Check if any of the elements is an email field
const emailFields = this.findAllInputFields(formWrapper as HTMLFormElement | null, CombinedFieldPatterns.email, ['text', 'email']);
- if (emailFields.some(input => input === this.clickedElement)) {
+ if (emailFields.some(input => elementsToCheck.includes(input))) {
return true;
}
diff --git a/apps/browser-extension/src/utils/formDetector/FormFiller.ts b/apps/browser-extension/src/utils/formDetector/FormFiller.ts
index b41c582fe..dc7b4a697 100644
--- a/apps/browser-extension/src/utils/formDetector/FormFiller.ts
+++ b/apps/browser-extension/src/utils/formDetector/FormFiller.ts
@@ -29,13 +29,40 @@ export class FormFiller {
this.fillGenderFields(credential);
}
+ /**
+ * Set value on an input element, handling both regular inputs and custom elements with shadow DOM.
+ * @param element The element to set the value on
+ * @param value The value to set
+ */
+ private setElementValue(element: HTMLInputElement | HTMLSelectElement, value: string): void {
+ // Try to set value directly on the element
+ element.value = value;
+
+ // If it's a custom element with shadow DOM, try to find and fill the actual input
+ if (element.shadowRoot) {
+ const shadowInput = element.shadowRoot.querySelector('input, textarea') as HTMLInputElement;
+ if (shadowInput) {
+ shadowInput.value = value;
+ // Trigger events on the shadow input as well
+ this.triggerInputEvents(shadowInput, false);
+ }
+ }
+
+ // Also check if the element contains a regular child input (non-shadow DOM)
+ const childInput = element.querySelector('input, textarea') as HTMLInputElement;
+ if (childInput && childInput !== element) {
+ childInput.value = value;
+ this.triggerInputEvents(childInput, false);
+ }
+ }
+
/**
* Fill the basic fields of the form.
* @param credential The credential to fill the form with.
*/
private fillBasicFields(credential: Credential): void {
if (this.form.usernameField && credential.Username) {
- this.form.usernameField.value = credential.Username;
+ this.setElementValue(this.form.usernameField, credential.Username);
this.triggerInputEvents(this.form.usernameField);
}
@@ -49,7 +76,7 @@ export class FormFiller {
if (this.form.emailField && (credential.Alias?.Email !== undefined || credential.Username !== undefined)) {
if (credential.Alias?.Email) {
- this.form.emailField.value = credential.Alias.Email;
+ this.setElementValue(this.form.emailField, credential.Alias.Email);
this.triggerInputEvents(this.form.emailField);
} else if (credential.Username && !this.form.usernameField) {
/*
@@ -62,28 +89,28 @@ export class FormFiller {
* from a previous password manager that only had username/password fields
* or where the user manually created a credential with only a username/password.
*/
- this.form.emailField.value = credential.Username;
+ this.setElementValue(this.form.emailField, credential.Username);
this.triggerInputEvents(this.form.emailField);
}
}
if (this.form.emailConfirmField && credential.Alias?.Email) {
- this.form.emailConfirmField.value = credential.Alias.Email;
+ this.setElementValue(this.form.emailConfirmField, credential.Alias.Email);
this.triggerInputEvents(this.form.emailConfirmField);
}
if (this.form.fullNameField && credential.Alias?.FirstName && credential.Alias?.LastName) {
- this.form.fullNameField.value = `${credential.Alias.FirstName} ${credential.Alias.LastName}`;
+ this.setElementValue(this.form.fullNameField, `${credential.Alias.FirstName} ${credential.Alias.LastName}`);
this.triggerInputEvents(this.form.fullNameField);
}
if (this.form.firstNameField && credential.Alias?.FirstName) {
- this.form.firstNameField.value = credential.Alias.FirstName;
+ this.setElementValue(this.form.firstNameField, credential.Alias.FirstName);
this.triggerInputEvents(this.form.firstNameField);
}
if (this.form.lastNameField && credential.Alias?.LastName) {
- this.form.lastNameField.value = credential.Alias.LastName;
+ this.setElementValue(this.form.lastNameField, credential.Alias.LastName);
this.triggerInputEvents(this.form.lastNameField);
}
}
@@ -91,25 +118,56 @@ export class FormFiller {
/**
* Fill the password field with the given password. This uses a small delay between each character to simulate human typing.
* Simulates actual keystroke behavior by appending characters one by one.
+ * Supports both regular inputs and custom elements with shadow DOM.
*
* @param field The password field to fill.
* @param password The password to fill the field with.
*/
private async fillPasswordField(field: HTMLInputElement, password: string): Promise<void> {
+ // Find the actual input element (could be in shadow DOM)
+ let actualInput = field;
+ let isCustomElement = false;
+
+ // Check for shadow DOM input
+ if (field.shadowRoot) {
+ const shadowInput = field.shadowRoot.querySelector('input[type="password"], input') as HTMLInputElement;
+ if (shadowInput) {
+ actualInput = shadowInput;
+ isCustomElement = true;
+ }
+ } else if (field.tagName.toLowerCase() !== 'input') {
+ // Check for child input (non-shadow DOM) only if field is not already an input
+ const childInput = field.querySelector('input[type="password"], input') as HTMLInputElement;
+ if (childInput) {
+ actualInput = childInput;
+ isCustomElement = true;
+ }
+ }
+
// Clear the field first
- field.value = '';
- this.triggerInputEvents(field, true);
+ actualInput.value = '';
+ if (isCustomElement) {
+ field.value = '';
+ }
+ this.triggerInputEvents(actualInput, true);
// Type each character with a small delay
for (const char of password) {
- // Append the character to the current value instead of using substring
- field.value += char;
+ // Append the character to the actual input
+ actualInput.value += char;
+ if (isCustomElement) {
+ // Also update the custom element's value property for compatibility
+ field.value += char;
+ }
// Small random delay between 5-15ms to simulate human typing
- this.triggerInputEvents(field, false);
+ this.triggerInputEvents(actualInput, false);
await new Promise(resolve => setTimeout(resolve, Math.random() * 10 + 5));
}
- this.triggerInputEvents(field, false);
+ this.triggerInputEvents(actualInput, false);
+ if (isCustomElement) {
+ this.triggerInputEvents(field, false);
+ }
}
/**
diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.shadowdom.test.ts b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.shadowdom.test.ts
new file mode 100644
index 000000000..3f10fe460
--- /dev/null
+++ b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.shadowdom.test.ts
@@ -0,0 +1,273 @@
+import { JSDOM } from 'jsdom';
+import { describe, expect, it } from 'vitest';
+
+import { FormDetector } from '../FormDetector';
+
+describe('FormDetector Shadow DOM tests', () => {
+ it('should detect faceplate-text-input with actual shadow DOM as autofill triggerable field', () => {
+ const html = `
+ <form>
+ <fieldset>
+ <faceplate-text-input id="login-username" name="username" autocomplete="username" required="">
+ <span slot="label">Email or username</span>
+ </faceplate-text-input>
+ </fieldset>
+ </form>
+ `;
+
+ const dom = new JSDOM(html, {
+ url: 'http://localhost',
+ runScripts: 'dangerously',
+ resources: 'usable'
+ });
+ const document = dom.window.document;
+
+ // Get the custom element
+ const faceplateElement = document.getElementById('login-username');
+ expect(faceplateElement).toBeTruthy();
+ expect(faceplateElement?.tagName.toLowerCase()).toBe('faceplate-text-input');
+
+ // Create shadow DOM like Reddit's actual implementation
+ const shadowRoot = faceplateElement!.attachShadow({ mode: 'open' });
+
+ // Create the internal input element that would exist in the shadow DOM
+ const shadowHTML = `
+ <div class="faceplate-input-wrapper">
+ <input type="text" name="username" autocomplete="username" class="faceplate-input" />
+ <slot name="label"></slot>
+ </div>
+ `;
+ shadowRoot.innerHTML = shadowHTML;
+
+ // Verify shadow DOM was created correctly
+ const shadowInput = shadowRoot.querySelector('input');
+ expect(shadowInput).toBeTruthy();
+ expect(shadowInput?.type).toBe('text');
+
+ // Create a FormDetector with the custom element
+ const detector = new FormDetector(document, faceplateElement as HTMLElement);
+
+ // Should detect it as an autofill triggerable field
+ expect(detector.isAutofillTriggerableField()).toBe(true);
+ });
+
+ it('should detect faceplate-text-input with type attribute fallback as autofill triggerable field', () => {
+ const html = `
+ <form>
+ <fieldset>
+ <faceplate-text-input id="login-username" type="text" name="username" autocomplete="username" required="">
+ <span slot="label">Email or username</span>
+ </faceplate-text-input>
+ </fieldset>
+ </form>
+ `;
+
+ const dom = new JSDOM(html, {
+ url: 'http://localhost',
+ runScripts: 'dangerously',
+ resources: 'usable'
+ });
+ const document = dom.window.document;
+
+ // Get the custom element
+ const faceplateElement = document.getElementById('login-username');
+ expect(faceplateElement).toBeTruthy();
+ expect(faceplateElement?.tagName.toLowerCase()).toBe('faceplate-text-input');
+
+ // Create a FormDetector with the custom element (without shadow DOM for this test)
+ const detector = new FormDetector(document, faceplateElement as HTMLElement);
+
+ // Should detect it as an autofill triggerable field using type attribute
+ expect(detector.isAutofillTriggerableField()).toBe(true);
+ });
+
+ it('should detect faceplate-text-input with password shadow DOM as triggerable field', () => {
+ const html = `
+ <form>
+ <fieldset>
+ <faceplate-text-input id="login-password" name="password" autocomplete="current-password">
+ <span slot="label">Password</span>
+ </faceplate-text-input>
+ </fieldset>
+ </form>
+ `;
+
+ const dom = new JSDOM(html, {
+ url: 'http://localhost',
+ runScripts: 'dangerously',
+ resources: 'usable'
+ });
+ const document = dom.window.document;
+
+ const faceplateElement = document.getElementById('login-password');
+
+ // Create shadow DOM with password input (like Reddit's actual implementation)
+ const shadowRoot = faceplateElement!.attachShadow({ mode: 'open' });
+ const shadowHTML = `
+ <div class="faceplate-input-wrapper">
+ <input type="password" name="password" autocomplete="current-password" class="faceplate-input" />
+ <slot name="label"></slot>
+ </div>
+ `;
+ shadowRoot.innerHTML = shadowHTML;
+
+ // Verify shadow DOM password input
+ const shadowInput = shadowRoot.querySelector('input');
+ expect(shadowInput?.type).toBe('password');
+
+ const detector = new FormDetector(document, faceplateElement as HTMLElement);
+
+ expect(detector.isAutofillTriggerableField()).toBe(true);
+ });
+
+ it('should detect custom element with email type as triggerable field', () => {
+ const html = `
+ <form>
+ <custom-input id="email-field" type="email" name="email">
+ <span slot="label">Email Address</span>
+ </custom-input>
+ </form>
+ `;
+
+ const dom = new JSDOM(html, {
+ url: 'http://localhost',
+ runScripts: 'dangerously',
+ resources: 'usable'
+ });
+ const document = dom.window.document;
+
+ const customElement = document.getElementById('email-field');
+ const detector = new FormDetector(document, customElement as HTMLElement);
+
+ expect(detector.isAutofillTriggerableField()).toBe(true);
+ });
+
+ it('should not detect non-input custom elements as triggerable fields', () => {
+ const html = `
+ <form>
+ <custom-button id="submit-btn" type="button">
+ <span>Submit</span>
+ </custom-button>
+ </form>
+ `;
+
+ const dom = new JSDOM(html, {
+ url: 'http://localhost',
+ runScripts: 'dangerously',
+ resources: 'usable'
+ });
+ const document = dom.window.document;
+
+ const customButton = document.getElementById('submit-btn');
+ const detector = new FormDetector(document, customButton as HTMLElement);
+
+ expect(detector.isAutofillTriggerableField()).toBe(false);
+ });
+
+ it('should handle real Reddit-like login form with shadow DOM', () => {
+ const html = `
+ <form>
+ <fieldset class="relative mt-0 mb-0 ml-0 mr-0 p-0 border-0 flex flex-col flex-grow bg-transparent">
+ <faceplate-text-input id="login-username" name="username" autocomplete="username" required=""
+ helper-text-placeholder="&nbsp;" aria-disabled="false" helper-text-aria-live="polite"
+ appearance="secondary" faceplate-validity="invalid">
+ <span slot="label">Email or username</span>
+ </faceplate-text-input>
+ </fieldset>
+ <fieldset class="relative mt-0 mb-0 ml-0 mr-0 p-0 border-0 flex flex-col flex-grow bg-transparent">
+ <faceplate-text-input id="login-password" name="password" autocomplete="current-password" required=""
+ helper-text-placeholder="&nbsp;" aria-disabled="false" helper-text-aria-live="polite"
+ appearance="secondary">
+ <span slot="label">Password</span>
+ </faceplate-text-input>
+ </fieldset>
+ </form>
+ `;
+
+ const dom = new JSDOM(html, {
+ url: 'http://localhost',
+ runScripts: 'dangerously',
+ resources: 'usable'
+ });
+ const document = dom.window.document;
+
+ // Set up shadow DOM for username field (no type attribute, just like Reddit)
+ const usernameElement = document.getElementById('login-username');
+ const usernameShadowRoot = usernameElement!.attachShadow({ mode: 'open' });
+ usernameShadowRoot.innerHTML = `
+ <div class="faceplate-input-wrapper">
+ <input type="text" name="username" autocomplete="username" class="faceplate-input" />
+ <slot name="label"></slot>
+ <div class="faceplate-input-decorations"></div>
+ </div>
+ `;
+
+ // Set up shadow DOM for password field (no type attribute, just like Reddit)
+ const passwordElement = document.getElementById('login-password');
+ const passwordShadowRoot = passwordElement!.attachShadow({ mode: 'open' });
+ passwordShadowRoot.innerHTML = `
+ <div class="faceplate-input-wrapper">
+ <input type="password" name="password" autocomplete="current-password" class="faceplate-input" />
+ <slot name="label"></slot>
+ <div class="faceplate-input-decorations"></div>
+ </div>
+ `;
+
+ // Test username field detection
+ const usernameDetector = new FormDetector(document, usernameElement as HTMLElement);
+ expect(usernameDetector.containsLoginForm()).toBe(true);
+ expect(usernameDetector.isAutofillTriggerableField()).toBe(true);
+
+ // Test password field detection
+ const passwordDetector = new FormDetector(document, passwordElement as HTMLElement);
+ expect(passwordDetector.containsLoginForm()).toBe(true);
+ expect(passwordDetector.isAutofillTriggerableField()).toBe(true);
+
+ // Test form extraction
+ const form = usernameDetector.getForm();
+ expect(form).toBeTruthy();
+
+ /**
+ * The form should be able to detect both username and password fields
+ * Note: Since the custom elements are now recognized, they should be included in the form detection
+ */
+ expect(form?.form).toBeTruthy();
+ });
+
+ it('should handle custom elements in form detection with type attributes', () => {
+ const html = `
+ <form>
+ <fieldset>
+ <faceplate-text-input id="username-field" type="text" name="username">
+ <span slot="label">Username</span>
+ </faceplate-text-input>
+ </fieldset>
+ <fieldset>
+ <faceplate-text-input id="password-field" type="password" name="password">
+ <span slot="label">Password</span>
+ </faceplate-text-input>
+ </fieldset>
+ </form>
+ `;
+
+ const dom = new JSDOM(html, {
+ url: 'http://localhost',
+ runScripts: 'dangerously',
+ resources: 'usable'
+ });
+ const document = dom.window.document;
+
+ const usernameElement = document.getElementById('username-field');
+ const detector = new FormDetector(document, usernameElement as HTMLElement);
+
+ // Should contain login form
+ expect(detector.containsLoginForm()).toBe(true);
+
+ // Should detect the field as triggerable
+ expect(detector.isAutofillTriggerableField()).toBe(true);
+
+ // Should be able to get form
+ const form = detector.getForm();
+ expect(form).toBeTruthy();
+ });
+});
diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.slot.test.ts b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.slot.test.ts
new file mode 100644
index 000000000..355dfe7ac
--- /dev/null
+++ b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.slot.test.ts
@@ -0,0 +1,160 @@
+import { JSDOM } from 'jsdom';
+import { describe, expect, it } from 'vitest';
+
+import { FormDetector } from '../FormDetector';
+
+import { FormField, testField } from './TestUtils';
+
+describe('FormDetector Slot-based Form tests', () => {
+ it('contains tests for slot-based form field detection', () => {
+ /**
+ * This test suite verifies that FormDetector can properly detect
+ * form fields that use slot-based labels (Web Components pattern).
+ * This is common in modern web applications using custom elements.
+ */
+ expect(true).toBe(true);
+ });
+
+ describe('Slot-based login form detection', () => {
+ const htmlFile = 'slot-based-form.html';
+
+ testField(FormField.Username, 'login-username', htmlFile);
+ testField(FormField.Password, 'login-password', htmlFile);
+ testField(FormField.Email, 'email-field', htmlFile);
+ testField(FormField.FirstName, 'firstname-field', htmlFile);
+ testField(FormField.LastName, 'lastname-field', htmlFile);
+ });
+
+ describe('Direct slot label detection', () => {
+ it('should detect input field with slot label for username', () => {
+ const html = `
+ <form>
+ <div>
+ <input id="test-username" name="user" type="text" />
+ <span slot="label">username</span>
+ </div>
+ </form>
+ `;
+
+ const dom = new JSDOM(html, {
+ url: 'http://localhost',
+ runScripts: 'dangerously',
+ resources: 'usable'
+ });
+ const document = dom.window.document;
+ const inputElement = document.getElementById('test-username');
+
+ const detector = new FormDetector(document, inputElement as HTMLElement);
+ const form = detector.getForm();
+
+ // Since the slot contains "username", it should be detected as a username field
+ expect(form?.usernameField).toBe(inputElement);
+ });
+
+ it('should detect email field with slot helper-text', () => {
+ const html = `
+ <form>
+ <web-component id="test-email" type="email">
+ <span slot="helper-text">Enter your email address</span>
+ </web-component>
+ </form>
+ `;
+
+ const dom = new JSDOM(html, {
+ url: 'http://localhost',
+ runScripts: 'dangerously',
+ resources: 'usable'
+ });
+ const document = dom.window.document;
+ const inputElement = document.getElementById('test-email');
+
+ const detector = new FormDetector(document, inputElement as HTMLElement);
+ const form = detector.getForm();
+
+ expect(form?.emailField).toBe(inputElement);
+ });
+
+ it('should detect password field with nested slot label', () => {
+ const html = `
+ <form>
+ <fieldset>
+ <custom-password-input id="test-password" type="password">
+ <div slot="label">
+ <span>Your Password</span>
+ </div>
+ </custom-password-input>
+ </fieldset>
+ </form>
+ `;
+
+ const dom = new JSDOM(html, {
+ url: 'http://localhost',
+ runScripts: 'dangerously',
+ resources: 'usable'
+ });
+ const document = dom.window.document;
+ const inputElement = document.getElementById('test-password');
+
+ const detector = new FormDetector(document, inputElement as HTMLElement);
+ const form = detector.getForm();
+
+ expect(form?.passwordField).toBe(inputElement);
+ });
+ });
+
+ describe('Slot-based form with regular inputs', () => {
+ it('should correctly identify input fields with slot labels', () => {
+ const html = `
+ <form>
+ <div>
+ <input id="email-input" type="email" name="email">
+ <span slot="label">Email Address</span>
+ </div>
+ <div>
+ <input id="pass-input" type="password" name="password">
+ <span slot="label">Password</span>
+ </div>
+ <div>
+ <input id="fname-input" type="text" name="fname">
+ <span slot="label">First Name</span>
+ </div>
+ <div>
+ <input id="lname-input" type="text" name="lname">
+ <span slot="label">Last Name</span>
+ </div>
+ </form>
+ `;
+
+ const dom = new JSDOM(html, {
+ url: 'http://localhost',
+ runScripts: 'dangerously',
+ resources: 'usable'
+ });
+ const document = dom.window.document;
+
+ // Test email field detection
+ const emailElement = document.getElementById('email-input');
+ const emailDetector = new FormDetector(document, emailElement as HTMLElement);
+ const emailForm = emailDetector.getForm();
+ expect(emailForm?.emailField).toBe(emailElement);
+
+ // Test password field detection
+ const passElement = document.getElementById('pass-input');
+ const passDetector = new FormDetector(document, passElement as HTMLElement);
+ const passForm = passDetector.getForm();
+ expect(passForm?.passwordField).toBe(passElement);
+
+ // Test first name field detection
+ const fnameElement = document.getElementById('fname-input');
+ const fnameDetector = new FormDetector(document, fnameElement as HTMLElement);
+ const fnameForm = fnameDetector.getForm();
+ expect(fnameForm?.firstNameField).toBe(fnameElement);
+
+ // Test last name field detection
+ const lnameElement = document.getElementById('lname-input');
+ const lnameDetector = new FormDetector(document, lnameElement as HTMLElement);
+ const lnameForm = lnameDetector.getForm();
+ expect(lnameForm?.lastNameField).toBe(lnameElement);
+ });
+ });
+});
diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/slot-based-form.html b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/slot-based-form.html
new file mode 100644
index 000000000..ef0e88733
--- /dev/null
+++ b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/slot-based-form.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Slot-based Login Form Test</title>
+</head>
+<body>
+ <form>
+ <fieldset class="relative mt-0 mb-0 ml-0 mr-0 p-0 border-0 flex flex-col flex-grow bg-transparent">
+ <faceplate-text-input id="login-username" type="text" name="username" autocomplete="username" required="" helper-text-placeholder="&nbsp;" aria-disabled="false" helper-text-aria-live="polite" appearance="secondary" faceplate-validity="invalid">
+ <span slot="label">Email or username</span>
+ </faceplate-text-input>
+ </fieldset>
+
+ <fieldset class="relative mt-0 mb-0 ml-0 mr-0 p-0 border-0 flex flex-col flex-grow bg-transparent">
+ <faceplate-text-input id="login-password" type="password" name="password" autocomplete="current-password" required="" helper-text-placeholder="&nbsp;" aria-disabled="false" helper-text-aria-live="polite" appearance="secondary">
+ <span slot="label">Password</span>
+ </faceplate-text-input>
+ </fieldset>
+
+ <fieldset class="relative mt-0 mb-0 ml-0 mr-0 p-0 border-0 flex flex-col flex-grow bg-transparent">
+ <custom-input-element id="email-field" type="email" name="email" required="">
+ <span slot="label">Email address</span>
+ <span slot="helper-text">We'll never share your email</span>
+ </custom-input-element>
+ </fieldset>
+
+ <fieldset class="relative mt-0 mb-0 ml-0 mr-0 p-0 border-0 flex flex-col flex-grow bg-transparent">
+ <web-component-input id="firstname-field" type="text" name="first_name">
+ <span slot="label">First name</span>
+ </web-component-input>
+ </fieldset>
+
+ <fieldset class="relative mt-0 mb-0 ml-0 mr-0 p-0 border-0 flex flex-col flex-grow bg-transparent">
+ <web-component-input id="lastname-field" type="text" name="last_name">
+ <span slot="label">Last name</span>
+ </web-component-input>
+ </fieldset>
+ </form>
+</body>
+</html>