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(
Here is a list of {items.length} items:
@@ -16,38 +16,38 @@ describe('vhtml', () => { )) }Here is a list of 3 items: