1
- 'use strict' ;
1
+ 'use strict'
2
2
3
- var trim = require ( 'trim' ) ;
4
- var paramCase = require ( 'kebab-case' ) ;
5
- var information = require ( 'property-information' ) ;
6
- var spaces = require ( 'space-separated-tokens' ) ;
7
- var commas = require ( 'comma-separated-tokens' ) ;
8
- var nan = require ( 'is-nan' ) ;
9
- var is = require ( 'unist-util-is' ) ;
3
+ var trim = require ( 'trim' )
4
+ var paramCase = require ( 'kebab-case' )
5
+ var information = require ( 'property-information' )
6
+ var spaces = require ( 'space-separated-tokens' )
7
+ var commas = require ( 'comma-separated-tokens' )
8
+ var nan = require ( 'is-nan' )
9
+ var is = require ( 'unist-util-is' )
10
10
11
- module . exports = wrapper ;
11
+ module . exports = wrapper
12
12
13
13
function wrapper ( h , node , prefix ) {
14
- var r ;
15
- var v ;
14
+ var r
15
+ var v
16
16
17
17
if ( typeof h !== 'function' ) {
18
- throw new Error ( 'h is not a function' ) ;
18
+ throw new Error ( 'h is not a function' )
19
19
}
20
20
21
- r = react ( h ) ;
22
- v = vdom ( h ) ;
21
+ r = react ( h )
22
+ v = vdom ( h )
23
23
24
24
if ( prefix === null || prefix === undefined ) {
25
- prefix = r === true || v === true ? 'h-' : false ;
25
+ prefix = r === true || v === true ? 'h-' : false
26
26
}
27
27
28
28
if ( is ( 'root' , node ) ) {
29
29
if ( node . children . length === 1 && is ( 'element' , node . children [ 0 ] ) ) {
30
- node = node . children [ 0 ] ;
30
+ node = node . children [ 0 ]
31
31
} else {
32
32
node = {
33
33
type : 'element' ,
34
34
tagName : 'div' ,
35
35
properties : { } ,
36
36
children : node . children
37
- } ;
37
+ }
38
38
}
39
39
} else if ( ! is ( 'element' , node ) ) {
40
- throw new Error ( 'Expected root or element, not `' + ( ( node && node . type ) || node ) + '`' ) ;
40
+ throw new Error (
41
+ 'Expected root or element, not `' + ( ( node && node . type ) || node ) + '`'
42
+ )
41
43
}
42
44
43
45
return toH ( h , node , {
@@ -46,41 +48,41 @@ function wrapper(h, node, prefix) {
46
48
react : r ,
47
49
vdom : v ,
48
50
hyperscript : hyperscript ( h )
49
- } ) ;
51
+ } )
50
52
}
51
53
52
54
/* Transform a HAST node through a hyperscript interface
53
55
* to *anything*! */
54
56
function toH ( h , node , ctx ) {
55
- var selector = node . tagName ;
56
- var properties ;
57
- var attributes ;
58
- var children ;
59
- var property ;
60
- var elements ;
61
- var length ;
62
- var index ;
63
- var value ;
64
-
65
- properties = node . properties ;
66
- attributes = { } ;
57
+ var selector = node . tagName
58
+ var properties
59
+ var attributes
60
+ var children
61
+ var property
62
+ var elements
63
+ var length
64
+ var index
65
+ var value
66
+
67
+ properties = node . properties
68
+ attributes = { }
67
69
68
70
for ( property in properties ) {
69
- addAttribute ( attributes , property , properties [ property ] , ctx ) ;
71
+ addAttribute ( attributes , property , properties [ property ] , ctx )
70
72
}
71
73
72
74
if ( ctx . vdom === true ) {
73
- selector = selector . toUpperCase ( ) ;
75
+ selector = selector . toUpperCase ( )
74
76
}
75
77
76
78
if ( ctx . hyperscript === true && attributes . id ) {
77
- selector += '#' + attributes . id ;
78
- delete attributes . id ;
79
+ selector += '#' + attributes . id
80
+ delete attributes . id
79
81
}
80
82
81
83
if ( ( ctx . hyperscript === true || ctx . vdom === true ) && attributes . className ) {
82
- selector += '.' + spaces . parse ( attributes . className ) . join ( '.' ) ;
83
- delete attributes . className ;
84
+ selector += '.' + spaces . parse ( attributes . className ) . join ( '.' )
85
+ delete attributes . className
84
86
}
85
87
86
88
if ( typeof attributes . style === 'string' ) {
@@ -89,46 +91,48 @@ function toH(h, node, ctx) {
89
91
* docs/vnode.md#propertiesstyle-vs-propertiesattributesstyle */
90
92
if ( ctx . vdom === true ) {
91
93
if ( ! attributes . attributes ) {
92
- attributes . attributes = { } ;
94
+ attributes . attributes = { }
93
95
}
94
96
95
- attributes . attributes . style = attributes . style ;
96
- delete attributes . style ;
97
- /* React only accepts `style` as object. */
97
+ attributes . attributes . style = attributes . style
98
+ delete attributes . style
99
+ /* React only accepts `style` as object. */
98
100
} else if ( ctx . react === true ) {
99
- attributes . style = parseStyle ( attributes . style ) ;
101
+ attributes . style = parseStyle ( attributes . style )
100
102
}
101
103
}
102
104
103
105
if ( ctx . prefix ) {
104
- ctx . key ++ ;
105
- attributes . key = ctx . prefix + ctx . key ;
106
+ ctx . key ++
107
+ attributes . key = ctx . prefix + ctx . key
106
108
}
107
109
108
- elements = [ ] ;
109
- children = node . children || [ ] ;
110
- length = children . length ;
111
- index = - 1 ;
110
+ elements = [ ]
111
+ children = node . children || [ ]
112
+ length = children . length
113
+ index = - 1
112
114
113
115
while ( ++ index < length ) {
114
- value = children [ index ] ;
116
+ value = children [ index ]
115
117
116
118
if ( is ( 'element' , value ) ) {
117
- elements . push ( toH ( h , value , ctx ) ) ;
119
+ elements . push ( toH ( h , value , ctx ) )
118
120
} else if ( is ( 'text' , value ) ) {
119
- elements . push ( value . value ) ;
121
+ elements . push ( value . value )
120
122
}
121
123
}
122
124
123
125
/* Ensure no React warnings are triggered for
124
126
* void elements having children passed in. */
125
- return elements . length === 0 ? h ( selector , attributes ) : h ( selector , attributes , elements ) ;
127
+ return elements . length === 0
128
+ ? h ( selector , attributes )
129
+ : h ( selector , attributes , elements )
126
130
}
127
131
128
132
/* Add `name` and its `value` to `props`. */
129
133
function addAttribute ( props , name , value , ctx ) {
130
- var info = information ( name ) || { } ;
131
- var subprop ;
134
+ var info = information ( name ) || { }
135
+ var subprop
132
136
133
137
/* Ignore nully, `false`, `NaN`, and falsey known
134
138
* booleans. */
@@ -139,109 +143,112 @@ function addAttribute(props, name, value, ctx) {
139
143
nan ( value ) ||
140
144
( info . boolean && ! value )
141
145
) {
142
- return ;
146
+ return
143
147
}
144
148
145
149
if ( info . name ) {
146
- name = info . name ;
150
+ name = info . name
147
151
} else if ( ctx . react && ! paramCasedReactProp ( name ) ) {
148
- name = camelCase ( name ) ;
152
+ name = camelCase ( name )
149
153
} else {
150
- name = paramCase ( name ) ;
154
+ name = paramCase ( name )
151
155
}
152
156
153
157
if ( value !== null && typeof value === 'object' && 'length' in value ) {
154
158
/* Accept `array`. Most props are space-separater. */
155
- value = ( info . commaSeparated ? commas : spaces ) . stringify ( value ) ;
159
+ value = ( info . commaSeparated ? commas : spaces ) . stringify ( value )
156
160
}
157
161
158
162
/* Treat `true` and truthy known booleans. */
159
163
if ( info . boolean && ctx . hyperscript === true ) {
160
- value = '' ;
164
+ value = ''
161
165
}
162
166
163
167
if ( info . name !== 'class' && ( info . mustUseAttribute || ! info . name ) ) {
164
168
if ( ctx . vdom === true ) {
165
- subprop = 'attributes' ;
169
+ subprop = 'attributes'
166
170
} else if ( ctx . hyperscript === true ) {
167
- subprop = 'attrs' ;
171
+ subprop = 'attrs'
168
172
}
169
173
170
174
if ( subprop ) {
171
175
if ( props [ subprop ] === undefined ) {
172
- props [ subprop ] = { } ;
176
+ props [ subprop ] = { }
173
177
}
174
178
175
- props [ subprop ] [ name ] = value ;
179
+ props [ subprop ] [ name ] = value
176
180
177
- return ;
181
+ return
178
182
}
179
183
}
180
184
181
- props [ info . propertyName || name ] = value ;
185
+ props [ info . propertyName || name ] = value
182
186
}
183
187
184
188
/* Check if `h` is `react.createElement`. It doesn’t accept
185
189
* `class` as an attribute, it must be added through the
186
190
* `selector`. */
187
191
function react ( h ) {
188
- var node = h && h ( 'div' ) ;
189
- return Boolean ( node && ( '_owner' in node || '_store' in node ) && node . key === null ) ;
192
+ var node = h && h ( 'div' )
193
+ return Boolean (
194
+ node && ( '_owner' in node || '_store' in node ) && node . key === null
195
+ )
190
196
}
191
197
192
198
/* Check if `h` is `hyperscript`. It doesn’t accept
193
199
* `class` as an attribute, it must be added through the
194
200
* `selector`. */
195
201
function hyperscript ( h ) {
196
- return Boolean ( h && h . context && h . cleanup ) ;
202
+ return Boolean ( h && h . context && h . cleanup )
197
203
}
198
204
199
- /**
200
- * Check if `h` is `virtual-dom/h`. It’s the only
205
+ /* Check if `h` is `virtual-dom/h`. It’s the only
201
206
* hyperscript “compatible” interface needing `attributes`. */
202
207
function vdom ( h ) {
203
208
try {
204
- return h ( 'div' ) . type === 'VirtualNode' ;
205
- } catch ( err ) { /* Empty */ }
209
+ return h ( 'div' ) . type === 'VirtualNode'
210
+ } catch ( err ) {
211
+ /* Empty */
212
+ }
206
213
207
214
/* istanbul ignore next */
208
- return false ;
215
+ return false
209
216
}
210
217
211
218
function parseStyle ( value ) {
212
- var result = { } ;
213
- var declarations = value . split ( ';' ) ;
214
- var length = declarations . length ;
215
- var index = - 1 ;
216
- var declaration ;
217
- var prop ;
218
- var pos ;
219
+ var result = { }
220
+ var declarations = value . split ( ';' )
221
+ var length = declarations . length
222
+ var index = - 1
223
+ var declaration
224
+ var prop
225
+ var pos
219
226
220
227
while ( ++ index < length ) {
221
- declaration = declarations [ index ] ;
222
- pos = declaration . indexOf ( ':' ) ;
228
+ declaration = declarations [ index ]
229
+ pos = declaration . indexOf ( ':' )
223
230
if ( pos !== - 1 ) {
224
- prop = camelCase ( trim ( declaration . slice ( 0 , pos ) ) ) ;
225
- result [ prop ] = trim ( declaration . slice ( pos + 1 ) ) ;
231
+ prop = camelCase ( trim ( declaration . slice ( 0 , pos ) ) )
232
+ result [ prop ] = trim ( declaration . slice ( pos + 1 ) )
226
233
}
227
234
}
228
235
229
- return result ;
236
+ return result
230
237
}
231
238
232
239
function paramCasedReactProp ( name ) {
233
- var head = name . slice ( 0 , 4 ) ;
234
- return ( head === 'data' || head === 'aria' ) && name . length > 4 ;
240
+ var head = name . slice ( 0 , 4 )
241
+ return ( head === 'data' || head === 'aria' ) && name . length > 4
235
242
}
236
243
237
244
function camelCase ( val ) {
238
245
if ( val . slice ( 0 , 4 ) === '-ms-' ) {
239
- val = 'ms-' + val . slice ( 4 ) ;
246
+ val = 'ms-' + val . slice ( 4 )
240
247
}
241
248
242
- return val . replace ( / - ( [ a - z ] ) / g, replace ) ;
249
+ return val . replace ( / - ( [ a - z ] ) / g, replace )
243
250
}
244
251
245
252
function replace ( $0 , $1 ) {
246
- return $1 . toUpperCase ( ) ;
253
+ return $1 . toUpperCase ( )
247
254
}
0 commit comments