From affa3f236ca608bb9bcd8fccabc826ee52a03d5f Mon Sep 17 00:00:00 2001 From: Brecht Savelkoul Date: Tue, 25 Sep 2018 07:23:13 +0100 Subject: [PATCH 1/3] Allow passing multiple child nodes as arguments --- factory.js | 7 ++++--- test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/factory.js b/factory.js index fbcf3d5..b7e86bd 100644 --- a/factory.js +++ b/factory.js @@ -12,12 +12,13 @@ function factory(schema, defaultTagName) { return h /* Hyperscript compatible DSL for creating virtual HAST trees. */ - function h(selector, properties, children) { + function h(selector, properties) { var node = parseSelector(selector, defaultTagName) + var children = Array.prototype.slice.call(arguments, 2) var property - if (!children && properties && isChildren(properties, node)) { - children = properties + if (properties && isChildren(properties, node)) { + children.unshift(properties) properties = null } diff --git a/test.js b/test.js index e84f991..89332d6 100644 --- a/test.js +++ b/test.js @@ -882,6 +882,48 @@ test('hastscript', function(t) { 'should allow omitting `properties` when a button has an invalid type' ) + st.deepEqual( + h('section', {id: 'test'}, h('p', 'first'), h('p', 'second')), + { + type: 'element', + tagName: 'section', + properties: {id: 'test'}, + children: [{ + type: 'element', + tagName: 'p', + properties: {}, + children: [{type: 'text', value: 'first'}] + },{ + type: 'element', + tagName: 'p', + properties: {}, + children: [{type: 'text', value: 'second'}] + }] + }, + 'should allow passing multiple child nodes as arguments' + ) + + st.deepEqual( + h('section', h('p', 'first'), h('p', 'second')), + { + type: 'element', + tagName: 'section', + properties: {}, + children: [{ + type: 'element', + tagName: 'p', + properties: {}, + children: [{type: 'text', value: 'first'}] + },{ + type: 'element', + tagName: 'p', + properties: {}, + children: [{type: 'text', value: 'second'}] + }] + }, + 'should allow passing multiple child nodes as arguments when there is no properties argument present' + ) + st.throws( function() { h('foo', {}, true) From 232b0aed3fb8425919a30cefa6fa6844ac19bfe6 Mon Sep 17 00:00:00 2001 From: Brecht Savelkoul Date: Tue, 25 Sep 2018 16:15:37 +0100 Subject: [PATCH 2/3] Fix test coverage --- factory.js | 4 ---- test.js | 50 ++++++++++++++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/factory.js b/factory.js index b7e86bd..f41b5bb 100644 --- a/factory.js +++ b/factory.js @@ -114,10 +114,6 @@ function addChild(nodes, value) { var index var length - if (value === null || value === undefined) { - return - } - if (typeof value === 'string' || typeof value === 'number') { nodes.push({type: 'text', value: String(value)}) return diff --git a/test.js b/test.js index 89332d6..f612aa4 100644 --- a/test.js +++ b/test.js @@ -888,17 +888,20 @@ test('hastscript', function(t) { type: 'element', tagName: 'section', properties: {id: 'test'}, - children: [{ - type: 'element', - tagName: 'p', - properties: {}, - children: [{type: 'text', value: 'first'}] - },{ - type: 'element', - tagName: 'p', - properties: {}, - children: [{type: 'text', value: 'second'}] - }] + children: [ + { + type: 'element', + tagName: 'p', + properties: {}, + children: [{type: 'text', value: 'first'}] + }, + { + type: 'element', + tagName: 'p', + properties: {}, + children: [{type: 'text', value: 'second'}] + } + ] }, 'should allow passing multiple child nodes as arguments' ) @@ -909,17 +912,20 @@ test('hastscript', function(t) { type: 'element', tagName: 'section', properties: {}, - children: [{ - type: 'element', - tagName: 'p', - properties: {}, - children: [{type: 'text', value: 'first'}] - },{ - type: 'element', - tagName: 'p', - properties: {}, - children: [{type: 'text', value: 'second'}] - }] + children: [ + { + type: 'element', + tagName: 'p', + properties: {}, + children: [{type: 'text', value: 'first'}] + }, + { + type: 'element', + tagName: 'p', + properties: {}, + children: [{type: 'text', value: 'second'}] + } + ] }, 'should allow passing multiple child nodes as arguments when there is no properties argument present' ) From fa0083908568363b3ba66408b2d1d6058d9549f4 Mon Sep 17 00:00:00 2001 From: Brecht Savelkoul Date: Wed, 26 Sep 2018 08:10:06 +0100 Subject: [PATCH 3/3] Update readme --- readme.md | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index b519546..d1f0d15 100644 --- a/readme.md +++ b/readme.md @@ -17,6 +17,7 @@ npm install hastscript var h = require('hastscript') var s = require('hastscript/svg') +// Child nodes as an array console.log( h('.foo#some-id', [ h('span', 'some text'), @@ -28,6 +29,15 @@ console.log( ]) ) +// Child nodes as arguments +console.log( + h('form', {method: 'POST'}, + h('input', {type: 'text', name: 'foo'}), + h('input', {type: 'text', name: 'bar'}), + h('input', {type: 'submit', value: 'send'}) + ) +) + console.log( s('svg', {xmlns: 'http://www.w3.org/2000/svg', viewbox: '0 0 500 500'}, [ s('title', 'SVG `` element'), @@ -57,6 +67,22 @@ Yields: children: [ { type: 'text', value: 'delta' }, { type: 'text', value: 'echo' } ] } ] } +{ type: 'element', + tagName: 'form', + properties: { method: 'POST' }, + children: + [ { type: 'element', + tagName: 'input', + properties: { type: 'text', name: 'foo' }, + children: [] }, + { type: 'element', + tagName: 'input', + properties: { type: 'text', name: 'bar' }, + children: [] }, + { type: 'element', + tagName: 'input', + properties: { type: 'submit', value: 'send' }, + children: [] } ] } { type: 'element', tagName: 'svg', properties: { xmlns: 'http://www.w3.org/2000/svg', viewBox: '0 0 500 500' }, @@ -73,9 +99,9 @@ Yields: ## API -### `h(selector?[, properties][, children])` +### `h(selector?[, properties][, ...children])` -### `s(selector?[, properties][, children])` +### `s(selector?[, properties][, ...children])` DSL to create virtual [HAST][] trees for HTML or SVG.