Skip to content

Commit

Permalink
fix: [#1563] Prevents disabled inputs from being focussed or blurred
Browse files Browse the repository at this point in the history
Co-authored-by: David Ortner <[email protected]>
  • Loading branch information
karpiuMG and capricorn86 authored Mar 4, 2025
1 parent ee6fff7 commit e4fea23
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/happy-dom/src/nodes/html-element/HTMLElementUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export default class HTMLElementUtility {
const target = element[PropertySymbol.proxy] || element;
const document = target[PropertySymbol.ownerDocument];

if (document[PropertySymbol.activeElement] !== target || !target[PropertySymbol.isConnected]) {
if (
document[PropertySymbol.activeElement] !== target ||
!target[PropertySymbol.isConnected] ||
target.disabled
) {
return;
}

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

if (document[PropertySymbol.activeElement] === target || !target[PropertySymbol.isConnected]) {
if (
document[PropertySymbol.activeElement] === target ||
!target[PropertySymbol.isConnected] ||
target.disabled
) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,30 @@ describe('HTMLInputElement', () => {
});
});

describe('disabled input', () => {
it("doesn't focus the input when it's disabled", () => {
document.body.appendChild(element);
element.focus();
expect(element).toBe(document.activeElement);
element.blur();
expect(element).not.toBe(document.activeElement);
element.disabled = true;
element.focus();
expect(element).not.toBe(document.activeElement);
});

it("doesn't blur the input when it's disabled", () => {
document.body.appendChild(element);
element.focus();
expect(element).toBe(document.activeElement);
element.blur();
expect(element).not.toBe(document.activeElement);
element.disabled = true;
element.blur();
expect(element).not.toBe(document.activeElement);
});
});

describe('setRangeText()', () => {
it('Sets a range text with selection mode set to "preserve".', () => {
element.value = 'TEST_VALUE';
Expand Down

0 comments on commit e4fea23

Please sign in to comment.