Skip to content

Commit

Permalink
Fix createElementFromAttrs bug (#31751)
Browse files Browse the repository at this point in the history
The "false" value was not handled correctly, it would cause bugs in the
future (fortunately, this behavior is not used in code yet).
  • Loading branch information
wxiaoguang authored Aug 1, 2024
1 parent d128352 commit e367835
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion web_src/js/utils/dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ test('createElementFromAttrs', () => {
class: 'cls-1 cls-2',
'data-foo': 'the-data',
disabled: true,
checked: false,
required: null,
tabindex: 0,
});
expect(el.outerHTML).toEqual('<button id="the-id" class="cls-1 cls-2" data-foo="the-data" disabled=""></button>');
expect(el.outerHTML).toEqual('<button id="the-id" class="cls-1 cls-2" data-foo="the-data" disabled="" tabindex="0"></button>');
});
2 changes: 1 addition & 1 deletion web_src/js/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export function createElementFromAttrs(tagName, attrs) {
const el = document.createElement(tagName);
for (const [key, value] of Object.entries(attrs)) {
if (value === undefined || value === null) continue;
if (value === true) {
if (typeof value === 'boolean') {
el.toggleAttribute(key, value);
} else {
el.setAttribute(key, String(value));
Expand Down

0 comments on commit e367835

Please sign in to comment.