Skip to content

Commit

Permalink
fix(createElement): ignore prop value with null and undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jun 19, 2022
1 parent e0896e6 commit c6ae6f7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/million/createElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ export const createElement = (vnode?: VNode | null, attachField = true): DOMNode
} else if (propName.startsWith('xlink')) {
el.setAttributeNS(XLINK_NS, 'href', String(propValue));
}
} else if (
el[propName] !== undefined &&
el[propName] !== null &&
!(el instanceof SVGElement) &&
propName in el
) {
el[propName] = propValue;
} else {
el.setAttribute(propName, String(propValue));
} else if (propValue !== undefined && propValue !== null) {
if (
el[propName] !== undefined &&
el[propName] !== null &&
!(el instanceof SVGElement) &&
propName in el
) {
el[propName] = propValue;
} else {
el.setAttribute(propName, String(propValue));
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/createElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ describe.concurrent('createElement', () => {
const el = document.createComment('');
expectEqualNode(createElement(null), el);
});

it('should not set prop value with null and undefined', () => {
const el = document.createElement('div');
el.id = '0';
el.setAttribute('bar', 'false');
el.appendChild(document.createTextNode('foo'));
expectEqualNode(createElement(m('div', { id: 0, children: undefined, foo: null, bar: false }, ['foo'])), el);
});
});

0 comments on commit c6ae6f7

Please sign in to comment.