Skip to content

Commit

Permalink
Add .createFragment option (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
finnp authored and goto-bus-stop committed Dec 6, 2018
1 parent 5277ff4 commit a7f19a5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ module.exports = function (h, opts) {

if (tree[2].length > 2
|| (tree[2].length === 2 && /\S/.test(tree[2][1]))) {
if (opts.createFragment) return opts.createFragment(tree[2])
throw new Error(
'multiple root elements must be wrapped in an enclosing tag'
)
Expand Down
3 changes: 3 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ returned by the concatenation function and can make specific use of them. This
is useful if you want to implement a pre-processor to generate javascript from
hyperx syntax.
* `opts.attrToProp` - turn off attribute to property conversions when `false`
* `opts.createFragment` - if your template string has multiple root elements, they
will be provided as an array to this function. the return value will then be returned
by the template literal

# prior art

Expand Down
17 changes: 17 additions & 0 deletions test/fragments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var test = require('tape')
var vdom = require('virtual-dom')
var hyperx = require('../')
var hx = hyperx(vdom.h, {createFragment: createFragment})

function createFragment (nodes) {
return nodes
}

test('mutliple root, fragments as array', function (t) {
var list = hx`<li>1</li> <li>2<div>_</div></li>`
t.equal(list.length, 3, '3 elements')
t.equal(vdom.create(list[0]).toString(), '<li>1</li>')
t.equal(list[1], ' ')
t.equal(vdom.create(list[2]).toString(), '<li>2<div>_</div></li>')
t.end()
})

0 comments on commit a7f19a5

Please sign in to comment.