Skip to content

Commit

Permalink
delay exectuion so order follows nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancahill committed Apr 30, 2017
1 parent 8767d17 commit 8b46ec0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
9 changes: 7 additions & 2 deletions src/vhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ let sanitized = {};

/** Hyperscript reviver that constructs a sanitized HTML string. */
export default function h(name, attrs) {
return _h.bind(null, ...arguments);
}

function _h(name, attrs) {
let stack=[];
for (let i=arguments.length; i-- > 2; ) {
stack.push(arguments[i]);
Expand All @@ -16,7 +20,7 @@ export default function h(name, attrs) {
// Sortof component support!
if (typeof name==='function') {
(attrs || (attrs = {})).children = stack.reverse();
return name(attrs);
return name(attrs)();
// return name(attrs, stack.reverse());
}

Expand All @@ -37,7 +41,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 = typeof child === 'function' ? child() : child;
s += sanitized[resolved]===true ? resolved : esc(resolved);
}
}
}
Expand Down
36 changes: 18 additions & 18 deletions test/vhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { expect } from 'chai';
describe('vhtml', () => {
it('should stringify html', () => {
let items = ['one', 'two', 'three'];
expect(
expect((
<div class="foo">
<h1>Hi!</h1>
<p>Here is a list of {items.length} items:</p>
Expand All @@ -16,38 +16,38 @@ describe('vhtml', () => {
)) }
</ul>
</div>
).to.equal(
)()).to.equal(
`<div class="foo"><h1>Hi!</h1><p>Here is a list of 3 items:</p><ul><li>one</li><li>two</li><li>three</li></ul></div>`
);
});

it('should sanitize children', () => {
expect(
expect((
<div>
{ `<strong>blocked</strong>` }
<em>allowed</em>
</div>
).to.equal(
)()).to.equal(
`<div>&lt;strong&gt;blocked&lt;/strong&gt;<em>allowed</em></div>`
);
});

it('should sanitize attributes', () => {
expect(
expect((
<div onclick={`&<>"'`} />
).to.equal(
)()).to.equal(
`<div onclick="&amp;&lt;&gt;&quot;&apos;"></div>`
);
});

it('should flatten children', () => {
expect(
expect((
<div>
{[['a','b']]}
<c>d</c>
{['e',['f'],[['g']]]}
</div>
).to.equal(
)()).to.equal(
`<div>ab<c>d</c>efg</div>`
);
});
Expand All @@ -62,7 +62,7 @@ describe('vhtml', () => {
</li>
);

expect(
expect((
<div class="foo">
<h1>Hi!</h1>
<ul>
Expand All @@ -73,7 +73,7 @@ describe('vhtml', () => {
)) }
</ul>
</div>
).to.equal(
)()).to.equal(
`<div class="foo"><h1>Hi!</h1><ul><li id="0"><h4>one</h4>This is item one!</li><li id="1"><h4>two</h4>This is item two!</li></ul></div>`
);
});
Expand All @@ -87,7 +87,7 @@ describe('vhtml', () => {
</li>
);

expect(
expect((
<div class="foo">
<h1>Hi!</h1>
<ul>
Expand All @@ -98,7 +98,7 @@ describe('vhtml', () => {
)) }
</ul>
</div>
).to.equal(
)()).to.equal(
`<div class="foo"><h1>Hi!</h1><ul><li><h4></h4></li><li><h4></h4></li></ul></div>`
);
});
Expand All @@ -113,7 +113,7 @@ describe('vhtml', () => {
</li>
);

expect(
expect((
<div class="foo">
<h1>Hi!</h1>
<ul>
Expand All @@ -124,13 +124,13 @@ describe('vhtml', () => {
)) }
</ul>
</div>
).to.equal(
)()).to.equal(
`<div class="foo"><h1>Hi!</h1><ul><li><h4></h4>This is item one!</li><li><h4></h4>This is item two!</li></ul></div>`
);
});

it('should support empty (void) tags', () => {
expect(
expect((
<div>
<area />
<base />
Expand All @@ -153,15 +153,15 @@ describe('vhtml', () => {
<span />
<p />
</div>
).to.equal(
)()).to.equal(
`<div><area><base><br><col><command><embed><hr><img><input><keygen><link><meta><param><source><track><wbr><div></div><span></span><p></p></div>`
);
});

it('should handle className as class', () => {
expect(
expect((
<div className="my-class" />
).to.equal(
)()).to.equal(
'<div class="my-class"></div>'
);
});
Expand Down

0 comments on commit 8b46ec0

Please sign in to comment.