Skip to content

Commit 15fd90e

Browse files
brechtcswooorm
authored andcommitted
Add support for passing children as arguments
Closes GH-8. Reviewed-by: Titus Wormer <[email protected]>
1 parent 20913a8 commit 15fd90e

File tree

3 files changed

+83
-10
lines changed

3 files changed

+83
-10
lines changed

factory.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ function factory(schema, defaultTagName) {
1212
return h
1313

1414
/* Hyperscript compatible DSL for creating virtual HAST trees. */
15-
function h(selector, properties, children) {
15+
function h(selector, properties) {
1616
var node = parseSelector(selector, defaultTagName)
17+
var children = Array.prototype.slice.call(arguments, 2)
1718
var property
1819

19-
if (!children && properties && isChildren(properties, node)) {
20-
children = properties
20+
if (properties && isChildren(properties, node)) {
21+
children.unshift(properties)
2122
properties = null
2223
}
2324

@@ -113,10 +114,6 @@ function addChild(nodes, value) {
113114
var index
114115
var length
115116

116-
if (value === null || value === undefined) {
117-
return
118-
}
119-
120117
if (typeof value === 'string' || typeof value === 'number') {
121118
nodes.push({type: 'text', value: String(value)})
122119
return

readme.md

+31-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ npm install hastscript
1717
var h = require('hastscript')
1818
var s = require('hastscript/svg')
1919

20+
// Child nodes as an array
2021
console.log(
2122
h('.foo#some-id', [
2223
h('span', 'some text'),
@@ -28,6 +29,17 @@ console.log(
2829
])
2930
)
3031

32+
// Child nodes as arguments
33+
console.log(
34+
h(
35+
'form',
36+
{method: 'POST'},
37+
h('input', {type: 'text', name: 'foo'}),
38+
h('input', {type: 'text', name: 'bar'}),
39+
h('input', {type: 'submit', value: 'send'})
40+
)
41+
)
42+
3143
console.log(
3244
s('svg', {xmlns: 'http://www.w3.org/2000/svg', viewbox: '0 0 500 500'}, [
3345
s('title', 'SVG `<circle>` element'),
@@ -57,6 +69,22 @@ Yields:
5769
children:
5870
[ { type: 'text', value: 'delta' },
5971
{ type: 'text', value: 'echo' } ] } ] }
72+
{ type: 'element',
73+
tagName: 'form',
74+
properties: { method: 'POST' },
75+
children:
76+
[ { type: 'element',
77+
tagName: 'input',
78+
properties: { type: 'text', name: 'foo' },
79+
children: [] },
80+
{ type: 'element',
81+
tagName: 'input',
82+
properties: { type: 'text', name: 'bar' },
83+
children: [] },
84+
{ type: 'element',
85+
tagName: 'input',
86+
properties: { type: 'submit', value: 'send' },
87+
children: [] } ] }
6088
{ type: 'element',
6189
tagName: 'svg',
6290
properties: { xmlns: 'http://www.w3.org/2000/svg', viewBox: '0 0 500 500' },
@@ -73,9 +101,9 @@ Yields:
73101

74102
## API
75103

76-
### `h(selector?[, properties][, children])`
104+
### `h(selector?[, properties][, ...children])`
77105

78-
### `s(selector?[, properties][, children])`
106+
### `s(selector?[, properties][, ...children])`
79107

80108
DSL to create virtual [HAST][] trees for HTML or SVG.
81109

@@ -94,7 +122,7 @@ Map of properties (`Object.<*>`, optional).
94122

95123
###### `children`
96124

97-
(List of) child nodes (`string`, `Node`, `Array.<string|Node>`, optional).
125+
(Lists of) child nodes (`string`, `Node`, `Array.<string|Node>`, optional).
98126
When strings are encountered, they are normalised to [`text`][text] nodes.
99127

100128
##### Returns

test.js

+48
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,54 @@ test('hastscript', function(t) {
882882
'should allow omitting `properties` when a button has an invalid type'
883883
)
884884

885+
st.deepEqual(
886+
h('section', {id: 'test'}, h('p', 'first'), h('p', 'second')),
887+
{
888+
type: 'element',
889+
tagName: 'section',
890+
properties: {id: 'test'},
891+
children: [
892+
{
893+
type: 'element',
894+
tagName: 'p',
895+
properties: {},
896+
children: [{type: 'text', value: 'first'}]
897+
},
898+
{
899+
type: 'element',
900+
tagName: 'p',
901+
properties: {},
902+
children: [{type: 'text', value: 'second'}]
903+
}
904+
]
905+
},
906+
'should allow passing multiple child nodes as arguments'
907+
)
908+
909+
st.deepEqual(
910+
h('section', h('p', 'first'), h('p', 'second')),
911+
{
912+
type: 'element',
913+
tagName: 'section',
914+
properties: {},
915+
children: [
916+
{
917+
type: 'element',
918+
tagName: 'p',
919+
properties: {},
920+
children: [{type: 'text', value: 'first'}]
921+
},
922+
{
923+
type: 'element',
924+
tagName: 'p',
925+
properties: {},
926+
children: [{type: 'text', value: 'second'}]
927+
}
928+
]
929+
},
930+
'should allow passing multiple child nodes as arguments when there is no properties argument present'
931+
)
932+
885933
st.throws(
886934
function() {
887935
h('foo', {}, true)

0 commit comments

Comments
 (0)