Skip to content

Commit

Permalink
fix: treat boolean true as empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
dyf19118 committed May 28, 2024
1 parent df0fd70 commit f5685cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions packages/core/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ export const noop = () => {};

/** set dom attribute for consistent behavior */
export const updateDomAttr = (dom: HTMLElement, name: string, value: any) => {
if (value != null && (value !== false || name.indexOf('aria-') === 0)) {
// any truty value, or falsy aria-* values
dom.setAttribute(name, value);
let isAria = false;

if (value != null && (value !== false || (isAria = name.indexOf('aria-') === 0))) {
dom.setAttribute(
name,
!isAria && typeof value === 'boolean'
// true but not aria-*
? ''
// falsy aria-* values
: value
);
} else {
dom.removeAttribute(name);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/comp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('@property', () => {
// * boolean property with its value default to true should be ignored
expect(node!.textContent).to.equal('false');
// * all truthy value, or falsy value except than '' will be treated as true
comp!.setAttribute('testattr7', '');
comp!.setAttribute('testattr7', '123');
await nextTick();
expect(node!.textContent).to.equal('true');
comp!.removeAttribute('testattr7');
Expand Down

0 comments on commit f5685cd

Please sign in to comment.