From 5a5c0dc121af00556e23e5471900b3d4b0ac0698 Mon Sep 17 00:00:00 2001 From: Alexandre Galays Date: Fri, 25 Nov 2016 10:55:58 +0100 Subject: [PATCH] Allow h(sel, data, node) and h(sel, node) shortcut notations --- src/h.ts | 4 +++- test/core.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/h.ts b/src/h.ts index 716581c..3e0a3f8 100644 --- a/src/h.ts +++ b/src/h.ts @@ -22,9 +22,11 @@ export function h(sel: any, b?: any, c?: any): VNode { data = b; if (is.array(c)) { children = c; } else if (is.primitive(c)) { text = c; } + else if (c && c.sel) { children = [c]; } } else if (b !== undefined) { if (is.array(b)) { children = b; } else if (is.primitive(b)) { text = b; } + else if (b && b.sel) { children = [b]; } else { data = b; } } if (is.array(children)) { @@ -37,4 +39,4 @@ export function h(sel: any, b?: any, c?: any): VNode { } return vnode(sel, data, children, text, undefined); }; -export default h; \ No newline at end of file +export default h; diff --git a/test/core.js b/test/core.js index 6a410d0..2ae6f12 100644 --- a/test/core.js +++ b/test/core.js @@ -42,6 +42,16 @@ describe('snabbdom', function() { assert.equal(vnode.children[0].sel, 'span#hello'); assert.equal(vnode.children[1].sel, 'b.world'); }); + it('can create vnode with one child vnode', function() { + var vnode = h('div', h('span#hello')); + assert.equal(vnode.sel, 'div'); + assert.equal(vnode.children[0].sel, 'span#hello'); + }); + it('can create vnode with props and one child vnode', function() { + var vnode = h('div', {}, h('span#hello')); + assert.equal(vnode.sel, 'div'); + assert.equal(vnode.children[0].sel, 'span#hello'); + }); it('can create vnode with text content', function() { var vnode = h('a', ['I am a string']); assert.equal(vnode.children[0].text, 'I am a string');