From cba85813d25fabb850d4019c510e95ffeaeebde2 Mon Sep 17 00:00:00 2001 From: Arthur Stolyar Date: Tue, 6 Dec 2016 08:14:51 +0300 Subject: [PATCH 1/6] Support empty tags --- package.json | 3 +++ src/vhtml.js | 28 ++++++++++++++++++---------- test/vhtml.js | 25 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index b9b826d..dc3cb77 100644 --- a/package.json +++ b/package.json @@ -69,5 +69,8 @@ "rollup-plugin-babel": "^2.4.0", "rollup-plugin-es3": "^1.0.3", "uglify-js": "^2.6.2" + }, + "dependencies": { + "empty-tags": "^1.0.0" } } diff --git a/src/vhtml.js b/src/vhtml.js index c62689e..060f008 100644 --- a/src/vhtml.js +++ b/src/vhtml.js @@ -1,3 +1,5 @@ +import emptyTags from 'empty-tags'; + // escape an attribute let esc = str => String(str).replace(/[&<>"']/g, s=>`&${map[s]};`); let map = {'&':'amp','<':'lt','>':'gt','"':'quot',"'":'apos'}; @@ -24,20 +26,26 @@ export default function h(name, attrs) { s += ` ${esc(i)}="${esc(attrs[i])}"`; } } - s += '>'; - while (stack.length) { - let child = stack.pop(); - if (child) { - if (child.pop) { - for (let i=child.length; i--; ) stack.push(child[i]); - } - else { - s += sanitized[child]===true ? child : esc(child); + if (emptyTags.indexOf(name) === -1) { + s += '>'; + + while (stack.length) { + let child = stack.pop(); + if (child) { + if (child.pop) { + for (let i=child.length; i--; ) stack.push(child[i]); + } + else { + s += sanitized[child]===true ? child : esc(child); + } } } + + sanitized[s += ``] = true; + } else { + sanitized[s += '>'] = true; } - sanitized[s += ``] = true; return s; } diff --git a/test/vhtml.js b/test/vhtml.js index 0ab297c..a27b4ca 100644 --- a/test/vhtml.js +++ b/test/vhtml.js @@ -77,4 +77,29 @@ describe('vhtml', () => { `

Hi!

` ); }); + + it('should understand empty elements', () => { + expect( +
+ + +
+ + + +
+ + + + + + + + + +
+ ).to.equal( + `


` + ); + }); }); From fe9557d1aa619cfe5fc3283d10e4b8aa0df9a0c6 Mon Sep 17 00:00:00 2001 From: Arthur Stolyar Date: Tue, 6 Dec 2016 08:30:07 +0300 Subject: [PATCH 2/6] Improve test for empty (void) tags --- test/vhtml.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/vhtml.js b/test/vhtml.js index a27b4ca..43f05c3 100644 --- a/test/vhtml.js +++ b/test/vhtml.js @@ -97,9 +97,13 @@ describe('vhtml', () => { + {/* Not void elements */} +
+ +

).to.equal( - `


` + `


` ); }); }); From 0524b7aade7623b2398b647f91b0b8b11fb13652 Mon Sep 17 00:00:00 2001 From: Arthur Stolyar Date: Tue, 6 Dec 2016 08:42:55 +0300 Subject: [PATCH 3/6] Rename test for empty (void) tags --- test/vhtml.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/vhtml.js b/test/vhtml.js index 43f05c3..4b6adc2 100644 --- a/test/vhtml.js +++ b/test/vhtml.js @@ -78,7 +78,7 @@ describe('vhtml', () => { ); }); - it('should understand empty elements', () => { + it('should support empty (void) tags', () => { expect(
From 08d964e6b545285f8c707259c2bb54c807344b3c Mon Sep 17 00:00:00 2001 From: Arthur Stolyar Date: Thu, 8 Dec 2016 03:13:48 +0300 Subject: [PATCH 4/6] Inline empty-tags module in the project --- package.json | 3 --- src/vhtml.js | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/package.json b/package.json index dc3cb77..b9b826d 100644 --- a/package.json +++ b/package.json @@ -69,8 +69,5 @@ "rollup-plugin-babel": "^2.4.0", "rollup-plugin-es3": "^1.0.3", "uglify-js": "^2.6.2" - }, - "dependencies": { - "empty-tags": "^1.0.0" } } diff --git a/src/vhtml.js b/src/vhtml.js index 060f008..eaa308f 100644 --- a/src/vhtml.js +++ b/src/vhtml.js @@ -1,4 +1,4 @@ -import emptyTags from 'empty-tags'; +import emptyTags from './empty-tags'; // escape an attribute let esc = str => String(str).replace(/[&<>"']/g, s=>`&${map[s]};`); From 85d29db69c232dd35905eeefe9cfd98c328ea79f Mon Sep 17 00:00:00 2001 From: Arthur Stolyar Date: Thu, 8 Dec 2016 03:16:00 +0300 Subject: [PATCH 5/6] Add forgotten empty-tags file --- src/empty-tags.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/empty-tags.js diff --git a/src/empty-tags.js b/src/empty-tags.js new file mode 100644 index 0000000..6f91e68 --- /dev/null +++ b/src/empty-tags.js @@ -0,0 +1,18 @@ +export default [ + 'area', + 'base', + 'br', + 'col', + 'command', + 'embed', + 'hr', + 'img', + 'input', + 'keygen', + 'link', + 'meta', + 'param', + 'source', + 'track', + 'wbr' +]; \ No newline at end of file From 2a02d4d002c9f42a36a9e82b2521b2378050df37 Mon Sep 17 00:00:00 2001 From: Arthur Stolyar Date: Tue, 13 Dec 2016 00:13:48 +0300 Subject: [PATCH 6/6] Simplify sanitization mapping --- src/vhtml.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/vhtml.js b/src/vhtml.js index eaa308f..d719228 100644 --- a/src/vhtml.js +++ b/src/vhtml.js @@ -42,10 +42,11 @@ export default function h(name, attrs) { } } - sanitized[s += ``] = true; + s += ``; } else { - sanitized[s += '>'] = true; + s += '>'; } + sanitized[s] = true; return s; }