Skip to content

Commit

Permalink
fix: [#1587] Fixes the HTMLInputElement.defaultChecked property (#1656)
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 authored Dec 31, 2024
1 parent b95a867 commit 3082b7e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export default class HTMLInputElement extends HTMLElement {
public [PropertySymbol.value] = null;
public [PropertySymbol.height] = 0;
public [PropertySymbol.width] = 0;
public [PropertySymbol.defaultChecked] = false;
public [PropertySymbol.checked]: boolean | null = null;
public [PropertySymbol.validationMessage] = '';
public [PropertySymbol.validity] = new ValidityState(this);
Expand All @@ -62,7 +61,7 @@ export default class HTMLInputElement extends HTMLElement {
* @returns Default checked.
*/
public get defaultChecked(): boolean {
return this[PropertySymbol.defaultChecked];
return this.hasAttribute('checked');
}

/**
Expand All @@ -71,7 +70,11 @@ export default class HTMLInputElement extends HTMLElement {
* @param defaultChecked Default checked.
*/
public set defaultChecked(defaultChecked: boolean) {
this[PropertySymbol.defaultChecked] = defaultChecked;
if (defaultChecked) {
this.setAttribute('checked', '');
} else {
this.removeAttribute('checked');
}
}

/**
Expand Down Expand Up @@ -1341,7 +1344,6 @@ export default class HTMLInputElement extends HTMLElement {
clone[PropertySymbol.value] = this[PropertySymbol.value];
clone[PropertySymbol.height] = this[PropertySymbol.height];
clone[PropertySymbol.width] = this[PropertySymbol.width];
clone[PropertySymbol.defaultChecked] = this[PropertySymbol.defaultChecked];
clone[PropertySymbol.files] = <FileList>this[PropertySymbol.files].slice();
clone.#selectionStart = this.#selectionStart;
clone.#selectionEnd = this.#selectionEnd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,23 @@ describe('HTMLInputElement', () => {
});
});

describe('get defaultChecked()', () => {
it('Returns true if the attribute "checked" has been set.', () => {
expect(element.defaultChecked).toBe(false);
element.setAttribute('checked', '');
expect(element.defaultChecked).toBe(true);
});
});

describe('set defaultChecked()', () => {
it('Sets the attribute "checked".', () => {
element.defaultChecked = true;
expect(element.getAttribute('checked')).toBe('');
element.defaultChecked = false;
expect(element.getAttribute('checked')).toBe(null);
});
});

describe('setCustomValidity()', () => {
it('Returns validation message.', () => {
element.setCustomValidity('Error message');
Expand Down

0 comments on commit 3082b7e

Please sign in to comment.