1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-04-11 19:37:19 +02:00

[PM-6546] Fix issue with blurring of elements after autofill occurs

This commit is contained in:
Cesar Gonzalez 2024-02-29 10:14:34 -06:00
parent 7cfe862aa6
commit c37eb259af
No known key found for this signature in database
GPG Key ID: 3381A5457F8CCECF
2 changed files with 23 additions and 9 deletions

View File

@ -552,17 +552,30 @@ describe("InsertAutofillContentService", () => {
insertAutofillContentService as any, insertAutofillContentService as any,
"simulateUserMouseClickAndFocusEventInteractions", "simulateUserMouseClickAndFocusEventInteractions",
); );
jest.spyOn(targetInput, "blur");
insertAutofillContentService["handleFocusOnFieldByOpidAction"]("__0"); insertAutofillContentService["handleFocusOnFieldByOpidAction"]("__0");
expect( expect(
insertAutofillContentService["collectAutofillContentService"].getAutofillFieldElementByOpid, insertAutofillContentService["collectAutofillContentService"].getAutofillFieldElementByOpid,
).toBeCalledWith("__0"); ).toBeCalledWith("__0");
expect(targetInput.blur).not.toHaveBeenCalled();
expect( expect(
insertAutofillContentService["simulateUserMouseClickAndFocusEventInteractions"], insertAutofillContentService["simulateUserMouseClickAndFocusEventInteractions"],
).toHaveBeenCalledWith(targetInput, true); ).toHaveBeenCalledWith(targetInput, true);
expect(elementEventCount).toEqual(expectedElementEventCount); expect(elementEventCount).toEqual(expectedElementEventCount);
}); });
it("blurs the element if it is currently the active element before simulating click and focus events", () => {
const targetInput = document.querySelector('input[type="text"]') as FormElementWithAttribute;
targetInput.opid = "__0";
targetInput.focus();
jest.spyOn(targetInput, "blur");
insertAutofillContentService["handleFocusOnFieldByOpidAction"]("__0");
expect(targetInput.blur).toHaveBeenCalled();
});
}); });
describe("insertValueIntoField", () => { describe("insertValueIntoField", () => {
@ -709,7 +722,7 @@ describe("InsertAutofillContentService", () => {
}); });
describe("triggerPostInsertEventsOnElement", () => { describe("triggerPostInsertEventsOnElement", () => {
it("triggers simulated event interactions and blurs the element after", () => { it("triggers simulated event interactions", () => {
const elementValue = "test"; const elementValue = "test";
document.body.innerHTML = `<input type="text" id="username" value="${elementValue}"/>`; document.body.innerHTML = `<input type="text" id="username" value="${elementValue}"/>`;
const element = document.getElementById("username") as FillableFormFieldElement; const element = document.getElementById("username") as FillableFormFieldElement;
@ -725,7 +738,6 @@ describe("InsertAutofillContentService", () => {
expect(insertAutofillContentService["simulateInputElementChangedEvent"]).toHaveBeenCalledWith( expect(insertAutofillContentService["simulateInputElementChangedEvent"]).toHaveBeenCalledWith(
element, element,
); );
expect(element.blur).toHaveBeenCalled();
expect(element.value).toBe(elementValue); expect(element.value).toBe(elementValue);
}); });
}); });

View File

@ -184,11 +184,18 @@ class InsertAutofillContentService implements InsertAutofillContentServiceInterf
/** /**
* Handles finding an element by opid and triggering click and focus events on the element. * Handles finding an element by opid and triggering click and focus events on the element.
* @param {string} opid * To ensure that we trigger a blur event correctly on a filled field, we first check if the
* @private * element is already focused. If it is, we blur the element before focusing on it again.
*
* @param {string} opid - The opid of the element to focus on.
*/ */
private handleFocusOnFieldByOpidAction(opid: string) { private handleFocusOnFieldByOpidAction(opid: string) {
const element = this.collectAutofillContentService.getAutofillFieldElementByOpid(opid); const element = this.collectAutofillContentService.getAutofillFieldElementByOpid(opid);
if (document.activeElement === element) {
element.blur();
}
this.simulateUserMouseClickAndFocusEventInteractions(element, true); this.simulateUserMouseClickAndFocusEventInteractions(element, true);
} }
@ -281,7 +288,6 @@ class InsertAutofillContentService implements InsertAutofillContentServiceInterf
} }
this.simulateInputElementChangedEvent(element); this.simulateInputElementChangedEvent(element);
element.blur();
} }
/** /**
@ -378,10 +384,6 @@ class InsertAutofillContentService implements InsertAutofillContentServiceInterf
element.dispatchEvent(new Event(simulatedInputEvents[index], { bubbles: true })); element.dispatchEvent(new Event(simulatedInputEvents[index], { bubbles: true }));
} }
} }
private nodeIsElement(node: Node): node is HTMLElement {
return node.nodeType === Node.ELEMENT_NODE;
}
} }
export default InsertAutofillContentService; export default InsertAutofillContentService;