From f073fde44374aabb7fdb1adad442dac49b8233e6 Mon Sep 17 00:00:00 2001 From: Jeffrey Holland <124393578+jholland-livefront@users.noreply.github.com> Date: Mon, 6 Oct 2025 19:26:55 +0200 Subject: [PATCH] [PM-22454] Autofill correct login form from extension (#16680) * [PM-22454] Autofill correct login form from extension * Remove redundant setting of `f` * Remove correct redundant call --- .../src/autofill/services/autofill.service.ts | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/apps/browser/src/autofill/services/autofill.service.ts b/apps/browser/src/autofill/services/autofill.service.ts index ca735f8f4f..73262962db 100644 --- a/apps/browser/src/autofill/services/autofill.service.ts +++ b/apps/browser/src/autofill/services/autofill.service.ts @@ -2270,6 +2270,8 @@ export default class AutofillService implements AutofillServiceInterface { withoutForm: boolean, ): AutofillField | null { let usernameField: AutofillField = null; + let usernameFieldInSameForm: AutofillField = null; + for (let i = 0; i < pageDetails.fields.length; i++) { const f = pageDetails.fields[i]; if (AutofillService.forCustomFieldsOnly(f)) { @@ -2282,22 +2284,29 @@ export default class AutofillService implements AutofillServiceInterface { const includesUsernameFieldName = this.findMatchingFieldIndex(f, AutoFillConstants.UsernameFieldNames) > -1; + const isInSameForm = f.form === passwordField.form; if ( !f.disabled && (canBeReadOnly || !f.readonly) && - (withoutForm || f.form === passwordField.form || includesUsernameFieldName) && + (withoutForm || isInSameForm || includesUsernameFieldName) && (canBeHidden || f.viewable) && (f.type === "text" || f.type === "email" || f.type === "tel") ) { - usernameField = f; - // We found an exact match. No need to keep looking. - if (includesUsernameFieldName) { - break; + // Prioritize fields in the same form as the password field + if (isInSameForm) { + usernameFieldInSameForm = f; + if (includesUsernameFieldName) { + return f; + } + } else { + usernameField = f; } } } - return usernameField; + + // Prefer username field in same form, fall back to any username field + return usernameFieldInSameForm || usernameField; } /**