From f961f685659b1acc2b3f33325e81f479c302b4f3 Mon Sep 17 00:00:00 2001 From: Jeff Terrell Date: Thu, 27 May 2021 22:28:58 -0400 Subject: [PATCH] Treat false attribute values like null/undefined Null and undefined attribute values are omitted, which is convenient. Having false attribute values be omitted would also be convenient. Before this commit, a false value results in an actual attribute added to the element, e.g.: const h = require('hyperscript') h('input', {type: 'text', name: null, required: false}).outerHTML ...which yields: '' This commit makes false-valued attributes be omitted, so that the above example would instead yield: '' --- index.js | 1 + test/index.js | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/index.js b/index.js index a3e2541..194ea27 100644 --- a/index.js +++ b/index.js @@ -56,6 +56,7 @@ function context () { e.appendChild(r = l) else if ('object' === typeof l) { for (var k in l) { + if(l[k] === false) continue if('function' === typeof l[k]) { if(/^on\w+/.test(k)) { (function (k, l) { // capture k, l in the closure diff --git a/test/index.js b/test/index.js index c007cc5..fac6fb2 100644 --- a/test/index.js +++ b/test/index.js @@ -154,6 +154,12 @@ test('context cleanup removes event handlers', function(t){ t.end() }) +test('false-valued attributes are ignored', function (t) { + t.equal(h('input', {type: 'text', name: 'foo', required: false}).outerHTML, + '') + t.end() +}) + test('unicode selectors', function (t) { t.equal(h('.⛄').outerHTML, '
') t.equal(h('span#⛄').outerHTML, '')