Skip to content

Commit

Permalink
Support fragments via h(null, null, ...args) (#16)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
ithinkihaveacat authored and developit committed May 23, 2019
1 parent 714c9e0 commit 8a0ae24
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: node_js
node_js:
- 4
node_js: node
cache: npm
15 changes: 9 additions & 6 deletions src/vhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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]) {
Expand All @@ -49,7 +52,7 @@ export default function h(name, attrs) {
}
}

s += `</${name}>`;
s += name ? `</${name}>` : '';
}

sanitized[s] = true;
Expand Down
17 changes: 17 additions & 0 deletions test/vhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,21 @@ describe('vhtml', () => {
'<div class="my-class" for="id"></div>'
);
});

it('should support string fragments', () => {
expect(
h(null, null, "foo", "bar", "baz")
).to.equal(
'foobarbaz'
);
});

it('should support element fragments', () => {
expect(
h(null, null, <p>foo</p>, <em>bar</em>, <div class="qqqqqq">baz</div>)
).to.equal(
'<p>foo</p><em>bar</em><div class="qqqqqq">baz</div>'
);
});

});

0 comments on commit 8a0ae24

Please sign in to comment.