Skip to content

Commit

Permalink
Merge pull request #51 from preactjs/ff-attribute-throw
Browse files Browse the repository at this point in the history
Fix crash with empty attribute values
  • Loading branch information
marvinhagemeister authored Sep 10, 2020
2 parents 9aff394 + 8b97be0 commit 5dcfac0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ function toCamelCase(str) {

function attributeChangedCallback(name, oldValue, newValue) {
if (!this._vdom) return;
// Attributes use `null` as an empty value whereas `undefined` is more
// common in pure JS components, especially with default parameters.
// When calling `node.removeAttribute()` we'll receive `null` as the new
// value. See issue #50.
newValue = newValue == null ? undefined : newValue;
const props = {};
props[name] = newValue;
props[toCamelCase(name)] = newValue;
Expand Down
15 changes: 15 additions & 0 deletions src/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ describe('web components', () => {
);
});

function NullProps({ size = 'md' }) {
return <div>{size.toUpperCase()}</div>;
}

registerElement(NullProps, 'x-null-props', ['size'], { shadow: true });

// #50
it('remove attributes without crashing', () => {
const el = document.createElement('x-null-props');
assert.doesNotThrow(() => (el.size = 'foo'));
root.appendChild(el);

assert.doesNotThrow(() => el.removeAttribute('size'));
});

describe('DOM properties', () => {
it('passes property changes to props', () => {
const el = document.createElement('x-clock');
Expand Down

0 comments on commit 5dcfac0

Please sign in to comment.