diff --git a/test/test-basic-properties.js b/test/test-basic-properties.js index 481e3de..a825ee9 100644 --- a/test/test-basic-properties.js +++ b/test/test-basic-properties.js @@ -250,6 +250,19 @@ it('inheritance is considered in type checking', () => { assert(el.objectProperty === array, 'property was set'); }); +it('numeric properties deserialize "" (empty) to "NaN"', () => { + const el = document.createElement('test-element'); + el.setAttribute('numeric-property', '0'); + document.body.append(el); + assert(el.numericProperty === 0, '"0" was coerced to 0'); + el.setAttribute('numeric-property', ''); + assert(Number.isNaN(el.numericProperty), '"" was coerced to NaN'); + el.setAttribute('numeric-property', ' '); + assert(Number.isNaN(el.numericProperty), '" " was coerced to NaN'); + el.setAttribute('numeric-property', ' '); + assert(Number.isNaN(el.numericProperty), '" " was coerced to NaN'); +}); + it('cannot set to known properties', () => { class BadTestElement extends XElement { static get properties() { diff --git a/x-element.js b/x-element.js index 441ee4b..8a9d215 100644 --- a/x-element.js +++ b/x-element.js @@ -852,7 +852,13 @@ export default class XElement extends HTMLElement { return value; } else { // Coerce type as needed. - return property.type(value); + switch (property.type) { + case Number: + // Don't try and coerce something like "Number('') >> 0". + return value.trim() ? property.type(value) : Number.NaN; + default: + return property.type(value); + } } }