Skip to content

Commit 8a0ae24

Browse files
ithinkihaveacatdevelopit
authored andcommitted
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)
1 parent 714c9e0 commit 8a0ae24

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
language: node_js
2-
node_js:
3-
- 4
2+
node_js: node
3+
cache: npm

src/vhtml.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let sanitized = {};
1313

1414
/** Hyperscript reviver that constructs a sanitized HTML string. */
1515
export default function h(name, attrs) {
16-
let stack=[], s = `<${name}`;
16+
let stack=[], s = '';
1717
attrs = attrs || {};
1818
for (let i=arguments.length; i-- > 2; ) {
1919
stack.push(arguments[i]);
@@ -26,12 +26,15 @@ export default function h(name, attrs) {
2626
// return name(attrs, stack.reverse());
2727
}
2828

29-
for (let i in attrs) {
30-
if (attrs[i]!==false && attrs[i]!=null && i !== setInnerHTMLAttr) {
31-
s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`;
29+
if (name) {
30+
s += '<' + name;
31+
if (attrs) for (let i in attrs) {
32+
if (attrs[i]!==false && attrs[i]!=null && i !== setInnerHTMLAttr) {
33+
s += ` ${DOMAttributeNames[i] ? DOMAttributeNames[i] : esc(i)}="${esc(attrs[i])}"`;
34+
}
3235
}
36+
s += '>';
3337
}
34-
s += '>';
3538

3639
if (emptyTags.indexOf(name) === -1) {
3740
if (attrs[setInnerHTMLAttr]) {
@@ -49,7 +52,7 @@ export default function h(name, attrs) {
4952
}
5053
}
5154

52-
s += `</${name}>`;
55+
s += name ? `</${name}>` : '';
5356
}
5457

5558
sanitized[s] = true;

test/vhtml.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,21 @@ describe('vhtml', () => {
173173
'<div class="my-class" for="id"></div>'
174174
);
175175
});
176+
177+
it('should support string fragments', () => {
178+
expect(
179+
h(null, null, "foo", "bar", "baz")
180+
).to.equal(
181+
'foobarbaz'
182+
);
183+
});
184+
185+
it('should support element fragments', () => {
186+
expect(
187+
h(null, null, <p>foo</p>, <em>bar</em>, <div class="qqqqqq">baz</div>)
188+
).to.equal(
189+
'<p>foo</p><em>bar</em><div class="qqqqqq">baz</div>'
190+
);
191+
});
192+
176193
});

0 commit comments

Comments
 (0)