Skip to content

Allow passing multiple child nodes as arguments #8

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Allow passing multiple child nodes as arguments
  • Loading branch information
brechtcs committed Sep 25, 2018
commit affa3f236ca608bb9bcd8fccabc826ee52a03d5f
7 changes: 4 additions & 3 deletions factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
42 changes: 42 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down