From 8767d1778ba86827c483d64a740b4818a8372d1d Mon Sep 17 00:00:00 2001 From: Nathan Cahill Date: Sat, 29 Apr 2017 16:49:25 -0700 Subject: [PATCH 1/6] support classname (#5) --- src/vhtml.js | 2 +- test/vhtml.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/vhtml.js b/src/vhtml.js index 7f36540..837da4f 100644 --- a/src/vhtml.js +++ b/src/vhtml.js @@ -23,7 +23,7 @@ export default function h(name, attrs) { let s = `<${name}`; if (attrs) for (let i in attrs) { if (attrs[i]!==false && attrs[i]!=null) { - s += ` ${esc(i)}="${esc(attrs[i])}"`; + s += ` ${i === 'className' ? 'class' : esc(i)}="${esc(attrs[i])}"`; } } diff --git a/test/vhtml.js b/test/vhtml.js index 3ca4a31..382fa53 100644 --- a/test/vhtml.js +++ b/test/vhtml.js @@ -157,4 +157,12 @@ describe('vhtml', () => { `


` ); }); + + it('should handle className as class', () => { + expect( +
+ ).to.equal( + '
' + ); + }); }); From a111d561839f0a48cd1fdd9744b24190cef7a582 Mon Sep 17 00:00:00 2001 From: Nathan Cahill Date: Sun, 30 Apr 2017 14:13:08 -0700 Subject: [PATCH 2/6] support the other special prop (#7) --- src/vhtml.js | 6 +++++- test/vhtml.js | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/vhtml.js b/src/vhtml.js index 837da4f..d42d9c9 100644 --- a/src/vhtml.js +++ b/src/vhtml.js @@ -3,6 +3,10 @@ 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'}; +let DOMAttributeNames = { + className: 'class', + htmlFor: 'for' +}; let sanitized = {}; @@ -23,7 +27,7 @@ export default function h(name, attrs) { let s = `<${name}`; if (attrs) for (let i in attrs) { if (attrs[i]!==false && attrs[i]!=null) { - s += ` ${i === 'className' ? 'class' : esc(i)}="${esc(attrs[i])}"`; + s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`; } } diff --git a/test/vhtml.js b/test/vhtml.js index 382fa53..606d762 100644 --- a/test/vhtml.js +++ b/test/vhtml.js @@ -158,11 +158,11 @@ describe('vhtml', () => { ); }); - it('should handle className as class', () => { + it('should handle special prop names', () => { expect( -
+
).to.equal( - '
' + '
' ); }); }); From 714c9e0a6fd8ff7968d239e875a1f8b3c71a10eb Mon Sep 17 00:00:00 2001 From: Peter Lenahan Date: Thu, 23 May 2019 08:37:55 -0400 Subject: [PATCH 3/6] Address vhtml#11: Add ability to directly set innerHTML of a component using the `dangerouslySetInnerHTML` attribute. (#12) * Address vhtml#11: Add ability to directly set innerHTML of a component using the `dangerouslySetInnerHTML` attribute. * Remove check for valid `__html` property of `dangerouslySetInnerHTML * Hoist line that is always executed out of conditional block Saves 4 bytes. * Hoist declaration of `s` to avoid extra `let` statement. Saves 3 bytes. * Hoist default assignment of `attrs` to the top of `h`. This allows us to assume that `attrs` is a plain object, and remove multiple guards. Saves 6B. --- src/vhtml.js | 21 +++++++++++---------- test/vhtml.js | 8 ++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/vhtml.js b/src/vhtml.js index d42d9c9..8576bc3 100644 --- a/src/vhtml.js +++ b/src/vhtml.js @@ -3,6 +3,7 @@ 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'}; +let setInnerHTMLAttr = 'dangerouslySetInnerHTML'; let DOMAttributeNames = { className: 'class', htmlFor: 'for' @@ -12,29 +13,31 @@ let sanitized = {}; /** Hyperscript reviver that constructs a sanitized HTML string. */ export default function h(name, attrs) { - let stack=[]; + let stack=[], s = `<${name}`; + attrs = attrs || {}; for (let i=arguments.length; i-- > 2; ) { stack.push(arguments[i]); } // Sortof component support! if (typeof name==='function') { - (attrs || (attrs = {})).children = stack.reverse(); + attrs.children = stack.reverse(); return name(attrs); // return name(attrs, stack.reverse()); } - let s = `<${name}`; - if (attrs) for (let i in attrs) { - if (attrs[i]!==false && attrs[i]!=null) { + for (let i in attrs) { + if (attrs[i]!==false && attrs[i]!=null && i !== setInnerHTMLAttr) { s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`; } } + s += '>'; if (emptyTags.indexOf(name) === -1) { - s += '>'; - - while (stack.length) { + if (attrs[setInnerHTMLAttr]) { + s += attrs[setInnerHTMLAttr].__html; + } + else while (stack.length) { let child = stack.pop(); if (child) { if (child.pop) { @@ -47,8 +50,6 @@ export default function h(name, attrs) { } s += ``; - } else { - s += '>'; } sanitized[s] = true; diff --git a/test/vhtml.js b/test/vhtml.js index 606d762..8fef98e 100644 --- a/test/vhtml.js +++ b/test/vhtml.js @@ -40,6 +40,14 @@ describe('vhtml', () => { ); }); + it('should not sanitize the "dangerouslySetInnerHTML" attribute, and directly set its `__html` property as innerHTML', () => { + expect( +
Injected HTML" }} /> + ).to.equal( + `
Injected HTML
` + ); + }); + it('should flatten children', () => { expect(
From 8a0ae24ae616758db5034a40a33fff1c0a924fe6 Mon Sep 17 00:00:00 2001 From: Michael Stillwell Date: Thu, 23 May 2019 14:05:17 +0100 Subject: [PATCH 4/6] Support fragments via h(null, null, ...args) (#16) * Test on latest LTS node * Adds support for fragments * Bump Travis node versions * Back out of unnecessary change * Update .travis.yml * Update vhtml.js * Size optimization (-3b) --- .travis.yml | 4 ++-- src/vhtml.js | 15 +++++++++------ test/vhtml.js | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8524235..965fe2d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: node_js -node_js: - - 4 +node_js: node +cache: npm diff --git a/src/vhtml.js b/src/vhtml.js index 8576bc3..0e03f69 100644 --- a/src/vhtml.js +++ b/src/vhtml.js @@ -13,7 +13,7 @@ let sanitized = {}; /** Hyperscript reviver that constructs a sanitized HTML string. */ export default function h(name, attrs) { - let stack=[], s = `<${name}`; + let stack=[], s = ''; attrs = attrs || {}; for (let i=arguments.length; i-- > 2; ) { stack.push(arguments[i]); @@ -26,12 +26,15 @@ export default function h(name, attrs) { // return name(attrs, stack.reverse()); } - for (let i in attrs) { - if (attrs[i]!==false && attrs[i]!=null && i !== setInnerHTMLAttr) { - s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`; + if (name) { + s += '<' + name; + if (attrs) for (let i in attrs) { + if (attrs[i]!==false && attrs[i]!=null && i !== setInnerHTMLAttr) { + s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`; + } } + s += '>'; } - s += '>'; if (emptyTags.indexOf(name) === -1) { if (attrs[setInnerHTMLAttr]) { @@ -49,7 +52,7 @@ export default function h(name, attrs) { } } - s += ``; + s += name ? `` : ''; } sanitized[s] = true; diff --git a/test/vhtml.js b/test/vhtml.js index 8fef98e..f88ddf2 100644 --- a/test/vhtml.js +++ b/test/vhtml.js @@ -173,4 +173,21 @@ describe('vhtml', () => { '
' ); }); + + it('should support string fragments', () => { + expect( + h(null, null, "foo", "bar", "baz") + ).to.equal( + 'foobarbaz' + ); + }); + + it('should support element fragments', () => { + expect( + h(null, null,

foo

, bar,
baz
) + ).to.equal( + '

foo

bar
baz
' + ); + }); + }); From e0c23d4481ac247814789dcb3f3b1d9568d091df Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 3 Dec 2019 19:49:24 -0500 Subject: [PATCH 5/6] ignore package lock --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 72e9b45..9df4e9f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /dist /node_modules /npm-debug.log +package-lock.json .DS_Store From 96fe21e63a983d7a8f52d8c51a0c994490313abc Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 3 Dec 2019 19:50:12 -0500 Subject: [PATCH 6/6] 2.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 33c90aa..6e3d2ff 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vhtml", "amdName": "vhtml", - "version": "2.1.0", + "version": "2.2.0", "description": "Hyperscript reviver that constructs a sanitized HTML string.", "main": "dist/vhtml.js", "minified:main": "dist/vhtml.min.js", pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy