diff --git a/src/vhtml.js b/src/vhtml.js index d42d9c9..cabbbee 100644 --- a/src/vhtml.js +++ b/src/vhtml.js @@ -10,6 +10,12 @@ let DOMAttributeNames = { let sanitized = {}; +function Element(name, attrs, stack) { + this.type = typeof name === 'function' ? name.name : name; + this.props = attrs || {}; + this.toString = _h.bind(this, name, stack); +} + /** Hyperscript reviver that constructs a sanitized HTML string. */ export default function h(name, attrs) { let stack=[]; @@ -17,11 +23,16 @@ export default function h(name, attrs) { stack.push(arguments[i]); } + return new Element(name, attrs, stack); +} + +function _h(name, stack) { + let attrs = this.props; + // Sortof component support! if (typeof name==='function') { - (attrs || (attrs = {})).children = stack.reverse(); - return name(attrs); - // return name(attrs, stack.reverse()); + attrs.children = stack.reverse(); + return String(name(attrs)); } let s = `<${name}`; @@ -41,7 +52,8 @@ export default function h(name, attrs) { for (let i=child.length; i--; ) stack.push(child[i]); } else { - s += sanitized[child]===true ? child : esc(child); + let resolved = String(child); + s += sanitized[resolved]===true ? resolved : esc(resolved); } } } diff --git a/test/vhtml.js b/test/vhtml.js index 606d762..cc3ff39 100644 --- a/test/vhtml.js +++ b/test/vhtml.js @@ -6,7 +6,7 @@ import { expect } from 'chai'; describe('vhtml', () => { it('should stringify html', () => { let items = ['one', 'two', 'three']; - expect( + expect(String(

Hi!

Here is a list of {items.length} items:

@@ -16,38 +16,38 @@ describe('vhtml', () => { )) }
- ).to.equal( + )).to.equal( `

Hi!

Here is a list of 3 items:

` ); }); it('should sanitize children', () => { - expect( + expect(String(
{ `blocked` } allowed
- ).to.equal( + )).to.equal( `
<strong>blocked</strong>allowed
` ); }); it('should sanitize attributes', () => { - expect( + expect(String(
"'`} /> - ).to.equal( + )).to.equal( `
` ); }); it('should flatten children', () => { - expect( + expect(String(
{[['a','b']]} d {['e',['f'],[['g']]]}
- ).to.equal( + )).to.equal( `
abdefg
` ); }); @@ -62,7 +62,7 @@ describe('vhtml', () => { ); - expect( + expect(String(

Hi!

- ).to.equal( + )).to.equal( `

Hi!

` ); }); @@ -87,7 +87,7 @@ describe('vhtml', () => { ); - expect( + expect(String(

Hi!

- ).to.equal( + )).to.equal( `

Hi!

` ); }); @@ -113,7 +113,7 @@ describe('vhtml', () => { ); - expect( + expect(String(

Hi!

- ).to.equal( + )).to.equal( `

Hi!

` ); }); it('should support empty (void) tags', () => { - expect( + expect(String(
@@ -153,15 +153,15 @@ describe('vhtml', () => {

- ).to.equal( + )).to.equal( `


` ); }); it('should handle special prop names', () => { - expect( + expect(String(
- ).to.equal( + )).to.equal( '
' ); });