Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .createFragment option #66

Merged
merged 1 commit into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,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()
})