Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe("InsertAutofillContentService", () => {
delay_between_operations: 20,
},
metadata: {},
autosubmit: null,
autosubmit: [],
Copy link
Contributor Author

@jprusik jprusik Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated typing warning fix

savedUrls: ["https://bitwarden.com"],
untrustedIframe: false,
itemType: "login",
Expand Down Expand Up @@ -218,28 +218,21 @@ describe("InsertAutofillContentService", () => {

await insertAutofillContentService.fillForm(fillScript);

expect(insertAutofillContentService["userCancelledInsecureUrlAutofill"]).toHaveBeenCalled();
expect(
insertAutofillContentService["userCancelledUntrustedIframeAutofill"],
).toHaveBeenCalled();
expect(insertAutofillContentService["runFillScriptAction"]).toHaveBeenCalledTimes(3);
expect(insertAutofillContentService["runFillScriptAction"]).toHaveBeenNthCalledWith(
1,
fillScript.script[0],
0,
fillScript.script,
);
expect(insertAutofillContentService["runFillScriptAction"]).toHaveBeenNthCalledWith(
2,
fillScript.script[1],
1,
fillScript.script,
);
expect(insertAutofillContentService["runFillScriptAction"]).toHaveBeenNthCalledWith(
3,
fillScript.script[2],
2,
fillScript.script,
);
});
});
Expand Down Expand Up @@ -623,7 +616,30 @@ describe("InsertAutofillContentService", () => {
});
});

it("will set the `value` attribute of any passed input or textarea elements", () => {
it("will set the `value` attribute of any passed input or textarea elements if the value differs", () => {
document.body.innerHTML = `<input type="text" id="username" value="old" /><textarea id="bio">old</textarea>`;
const value1 = "test";
const value2 = "test2";
const textInputElement = document.getElementById("username") as HTMLInputElement;
const textareaElement = document.getElementById("bio") as HTMLTextAreaElement;
jest.spyOn(insertAutofillContentService as any, "handleInsertValueAndTriggerSimulatedEvents");

insertAutofillContentService["insertValueIntoField"](textInputElement, value1);

expect(textInputElement.value).toBe(value1);
expect(
insertAutofillContentService["handleInsertValueAndTriggerSimulatedEvents"],
).toHaveBeenCalledWith(textInputElement, expect.any(Function));

insertAutofillContentService["insertValueIntoField"](textareaElement, value2);

expect(textareaElement.value).toBe(value2);
expect(
insertAutofillContentService["handleInsertValueAndTriggerSimulatedEvents"],
).toHaveBeenCalledWith(textareaElement, expect.any(Function));
});

it("will NOT set the `value` attribute of any passed input or textarea elements if they already have values matching the passed value", () => {
document.body.innerHTML = `<input type="text" id="username" /><textarea id="bio"></textarea>`;
const value1 = "test";
const value2 = "test2";
Expand All @@ -638,14 +654,28 @@ describe("InsertAutofillContentService", () => {
expect(textInputElement.value).toBe(value1);
expect(
insertAutofillContentService["handleInsertValueAndTriggerSimulatedEvents"],
).toHaveBeenCalledWith(textInputElement, expect.any(Function));
).not.toHaveBeenCalled();

insertAutofillContentService["insertValueIntoField"](textareaElement, value2);

expect(textareaElement.value).toBe(value2);
expect(
insertAutofillContentService["handleInsertValueAndTriggerSimulatedEvents"],
).toHaveBeenCalledWith(textareaElement, expect.any(Function));
).not.toHaveBeenCalled();
});

it("skips filling when the field already has the target value", () => {
const value = "test";
document.body.innerHTML = `<input type="text" id="username" value="${value}"/>`;
const element = document.getElementById("username") as FillableFormFieldElement;
jest.spyOn(insertAutofillContentService as any, "handleInsertValueAndTriggerSimulatedEvents");

insertAutofillContentService["insertValueIntoField"](element, value);

expect(
insertAutofillContentService["handleInsertValueAndTriggerSimulatedEvents"],
).not.toHaveBeenCalled();
expect(element.value).toBe(value);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ class InsertAutofillContentService implements InsertAutofillContentServiceInterf
return;
}

const fillActionPromises = fillScript.script.map(this.runFillScriptAction);
await Promise.all(fillActionPromises);
for (let index = 0; index < fillScript.script.length; index++) {
await this.runFillScriptAction(fillScript.script[index], index);
}
}

/**
Expand Down Expand Up @@ -189,10 +190,14 @@ class InsertAutofillContentService implements InsertAutofillContentServiceInterf
const elementCanBeReadonly =
elementIsInputElement(element) || elementIsTextAreaElement(element);
const elementCanBeFilled = elementCanBeReadonly || elementIsSelectElement(element);
const elementValue = (element as HTMLInputElement)?.value || element?.innerText || "";

const elementAlreadyHasTheValue = !!(elementValue?.length && elementValue === value);

if (
!element ||
!value ||
elementAlreadyHasTheValue ||
(elementCanBeReadonly && element.readOnly) ||
(elementCanBeFilled && element.disabled)
) {
Expand Down
Loading