diff --git a/index.js b/index.js index de6fc07..ab16378 100644 --- a/index.js +++ b/index.js @@ -137,6 +137,16 @@ module.exports = function (h, opts) { tree[2].shift() } + // handle single top-level array template part + if (( + // hx`${[ ...els ]}` + tree[2].length === 1 || + // trailing whitespace: hx`${[ ...els ]} ` + tree[2].length === 2 && /^\s*$/.test(tree[2][1]) + ) && Array.isArray(tree[2][0])) { + tree[2] = tree[2][0] + } + if (tree[2].length > 2 || (tree[2].length === 2 && /\S/.test(tree[2][1]))) { if (opts.createFragment) return opts.createFragment(tree[2]) diff --git a/test/fragments.js b/test/fragments.js index f6770cc..1855a6b 100644 --- a/test/fragments.js +++ b/test/fragments.js @@ -4,14 +4,26 @@ var hyperx = require('../') var hx = hyperx(vdom.h, {createFragment: createFragment}) function createFragment (nodes) { - return nodes + return { frag: nodes } } -test('mutliple root, fragments as array', function (t) { +test('multiple root, fragments as array', function (t) { var list = hx`
  • 1
  • 2
    _
  • ` - t.equal(list.length, 3, '3 elements') - t.equal(vdom.create(list[0]).toString(), '
  • 1
  • ') - t.equal(list[1], ' ') - t.equal(vdom.create(list[2]).toString(), '
  • 2
    _
  • ') + t.ok(Array.isArray(list.frag)) + t.equal(list.frag.length, 3, '3 elements') + t.equal(vdom.create(list.frag[0]).toString(), '
  • 1
  • ') + t.equal(list.frag[1], ' ') + t.equal(vdom.create(list.frag[2]).toString(), '
  • 2
    _
  • ') + t.end() +}) + +test('multiple root embeds, fragments as array', function (t) { + var list = hx` + ${[1,2]} + ` + t.ok(Array.isArray(list.frag)) + t.deepEqual(list, { + frag: [1, 2] + }) t.end() })