Skip to content

Commit 3082b7e

Browse files
authored
fix: [#1587] Fixes the HTMLInputElement.defaultChecked property (#1656)
1 parent b95a867 commit 3082b7e

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export default class HTMLInputElement extends HTMLElement {
4141
public [PropertySymbol.value] = null;
4242
public [PropertySymbol.height] = 0;
4343
public [PropertySymbol.width] = 0;
44-
public [PropertySymbol.defaultChecked] = false;
4544
public [PropertySymbol.checked]: boolean | null = null;
4645
public [PropertySymbol.validationMessage] = '';
4746
public [PropertySymbol.validity] = new ValidityState(this);
@@ -62,7 +61,7 @@ export default class HTMLInputElement extends HTMLElement {
6261
* @returns Default checked.
6362
*/
6463
public get defaultChecked(): boolean {
65-
return this[PropertySymbol.defaultChecked];
64+
return this.hasAttribute('checked');
6665
}
6766

6867
/**
@@ -71,7 +70,11 @@ export default class HTMLInputElement extends HTMLElement {
7170
* @param defaultChecked Default checked.
7271
*/
7372
public set defaultChecked(defaultChecked: boolean) {
74-
this[PropertySymbol.defaultChecked] = defaultChecked;
73+
if (defaultChecked) {
74+
this.setAttribute('checked', '');
75+
} else {
76+
this.removeAttribute('checked');
77+
}
7578
}
7679

7780
/**
@@ -1341,7 +1344,6 @@ export default class HTMLInputElement extends HTMLElement {
13411344
clone[PropertySymbol.value] = this[PropertySymbol.value];
13421345
clone[PropertySymbol.height] = this[PropertySymbol.height];
13431346
clone[PropertySymbol.width] = this[PropertySymbol.width];
1344-
clone[PropertySymbol.defaultChecked] = this[PropertySymbol.defaultChecked];
13451347
clone[PropertySymbol.files] = <FileList>this[PropertySymbol.files].slice();
13461348
clone.#selectionStart = this.#selectionStart;
13471349
clone.#selectionEnd = this.#selectionEnd;

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

+17
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,23 @@ describe('HTMLInputElement', () => {
10651065
});
10661066
});
10671067

1068+
describe('get defaultChecked()', () => {
1069+
it('Returns true if the attribute "checked" has been set.', () => {
1070+
expect(element.defaultChecked).toBe(false);
1071+
element.setAttribute('checked', '');
1072+
expect(element.defaultChecked).toBe(true);
1073+
});
1074+
});
1075+
1076+
describe('set defaultChecked()', () => {
1077+
it('Sets the attribute "checked".', () => {
1078+
element.defaultChecked = true;
1079+
expect(element.getAttribute('checked')).toBe('');
1080+
element.defaultChecked = false;
1081+
expect(element.getAttribute('checked')).toBe(null);
1082+
});
1083+
});
1084+
10681085
describe('setCustomValidity()', () => {
10691086
it('Returns validation message.', () => {
10701087
element.setCustomValidity('Error message');

0 commit comments

Comments
 (0)