commit 611ca78cc08cd6f5ffba2bb08791bc334debb523
parent f4606a96abbf412709b9b8d8696c48844f48ef09
Author: Neil Deakin <neil@mozilla.com>
Date: Fri, 14 Nov 2025 16:04:02 +0000
Bug 1931403, when previewing a field, don't reset the autofillState when a field isn't present in the profile unless the autofillState is preview, r=dimi,credential-management-reviewers
Differential Revision: https://phabricator.services.mozilla.com/D270958
Diffstat:
2 files changed, 51 insertions(+), 1 deletion(-)
diff --git a/browser/extensions/formautofill/test/browser/browser_previewFormFields.js b/browser/extensions/formautofill/test/browser/browser_previewFormFields.js
@@ -128,6 +128,30 @@ const TESTCASES = [
"cc-csc": [NORMAL],
},
},
+ {
+ description: "Preview when field is already filled",
+ document: `<form>
+ <input id="given-name" autocomplete="given-name">
+ <input id="family-name" autocomplete="family-name">
+ <input id="street-address" autocomplete="street-address">
+ <input id="address-level2" autocomplete="address-level2">
+ <input id="organization" autocomplete="organization">
+ </form>`,
+ focusedInputId: "given-name",
+ prefillId: "organization",
+ profileData: {
+ "given-name": "John",
+ "family-name": "Doe",
+ "street-address": "100 Main Street",
+ "address-level2": "Hamilton",
+ },
+ expectedResultState: {
+ "given-name": [PREVIEW],
+ "family-name": [PREVIEW],
+ "street-address": [PREVIEW],
+ "address-level2": [PREVIEW],
+ },
+ },
];
add_task(async function test_preview_form_fields() {
@@ -138,6 +162,20 @@ add_task(async function test_preview_form_fields() {
const TEST_URL =
"https://example.org/document-builder.sjs?html=" + TEST.document;
await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
+ // If prefillId is set, then set the field with that id to a
+ // value and assign the autofill state.
+ if (TEST.prefillId) {
+ await SpecialPowers.spawn(
+ browser,
+ [TEST.prefillId],
+ async prefillId => {
+ const element = content.document.getElementById(prefillId);
+ element.value = "Mozilla";
+ element.autofillState = "autofill";
+ }
+ );
+ }
+
const previewCompeletePromise = TestUtils.topicObserved(
"formautofill-preview-complete"
);
@@ -170,6 +208,14 @@ add_task(async function test_preview_form_fields() {
"Check if preview value is set correctly"
);
}
+
+ if (obj.prefillId) {
+ Assert.equal(
+ content.document.getElementById(obj.prefillId).autofillState,
+ "autofill",
+ "Previously filled field should remain autofilled"
+ );
+ }
});
});
diff --git a/toolkit/components/formautofill/shared/FormAutofillHandler.sys.mjs b/toolkit/components/formautofill/shared/FormAutofillHandler.sys.mjs
@@ -418,7 +418,11 @@ export class FormAutofillHandler {
let value = this.getFilledValueFromProfile(fieldDetail, profile);
if (!value) {
- this.changeFieldState(fieldDetail, FIELD_STATES.NORMAL);
+ // A field could have been filled by a previous fill, so only
+ // clear when in the preview state.
+ if (element.autofillState == FIELD_STATES.PREVIEW) {
+ this.changeFieldState(fieldDetail, FIELD_STATES.NORMAL);
+ }
continue;
}