Skip to content

Commit 444bf0f

Browse files
committed
fix: [#1563] Prevent disabled inputs from being focussed or blurred
1 parent baaeeb9 commit 444bf0f

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

packages/happy-dom/src/nodes/html-element/HTMLElementUtility.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ export default class HTMLElementUtility {
1616
const target = element[PropertySymbol.proxy] || element;
1717
const document = target[PropertySymbol.ownerDocument];
1818

19-
if (document[PropertySymbol.activeElement] !== target || !target[PropertySymbol.isConnected]) {
19+
if (
20+
document[PropertySymbol.activeElement] !== target ||
21+
!target[PropertySymbol.isConnected] ||
22+
target.disabled
23+
) {
2024
return;
2125
}
2226

@@ -53,7 +57,11 @@ export default class HTMLElementUtility {
5357
const target = element[PropertySymbol.proxy] || element;
5458
const document = target[PropertySymbol.ownerDocument];
5559

56-
if (document[PropertySymbol.activeElement] === target || !target[PropertySymbol.isConnected]) {
60+
if (
61+
document[PropertySymbol.activeElement] === target ||
62+
!target[PropertySymbol.isConnected] ||
63+
target.disabled
64+
) {
5765
return;
5866
}
5967

packages/happy-dom/test/nodes/html-input-element/HTMLInputElement.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,30 @@ describe('HTMLInputElement', () => {
11581158
});
11591159
});
11601160

1161+
describe('disabled input', () => {
1162+
it("doesn't focus the input when it's disabled", () => {
1163+
document.body.appendChild(element);
1164+
element.focus();
1165+
expect(element).toBe(document.activeElement);
1166+
element.blur();
1167+
expect(element).not.toBe(document.activeElement);
1168+
element.disabled = true;
1169+
element.focus();
1170+
expect(element).not.toBe(document.activeElement);
1171+
});
1172+
1173+
it("doesn't blur the input when it's disabled", () => {
1174+
document.body.appendChild(element);
1175+
element.focus();
1176+
expect(element).toBe(document.activeElement);
1177+
element.blur();
1178+
expect(element).not.toBe(document.activeElement);
1179+
element.disabled = true;
1180+
element.blur();
1181+
expect(element).not.toBe(document.activeElement);
1182+
});
1183+
});
1184+
11611185
describe('setRangeText()', () => {
11621186
it('Sets a range text with selection mode set to "preserve".', () => {
11631187
element.value = 'TEST_VALUE';

0 commit comments

Comments
 (0)