@@ -14,16 +14,14 @@ var root = convert('root')
14
14
var element = convert ( 'element' )
15
15
var text = convert ( 'text' )
16
16
17
- var dashes = / - ( [ a - z ] ) / g
18
-
19
17
module . exports = wrapper
20
18
21
19
function wrapper ( h , node , options ) {
22
20
var settings = options || { }
21
+ var r = react ( h )
22
+ var v = vue ( h )
23
+ var vd = vdom ( h )
23
24
var prefix
24
- var r
25
- var v
26
- var vd
27
25
28
26
if ( typeof h !== 'function' ) {
29
27
throw new Error ( 'h is not a function' )
@@ -36,14 +34,6 @@ function wrapper(h, node, options) {
36
34
prefix = settings . prefix
37
35
}
38
36
39
- r = react ( h )
40
- v = vue ( h )
41
- vd = vdom ( h )
42
-
43
- if ( prefix === null || prefix === undefined ) {
44
- prefix = r === true || v === true || vd === true ? 'h-' : false
45
- }
46
-
47
37
if ( root ( node ) ) {
48
38
node =
49
39
node . children . length === 1 && element ( node . children [ 0 ] )
@@ -62,7 +52,7 @@ function wrapper(h, node, options) {
62
52
63
53
return toH ( h , node , {
64
54
schema : settings . space === 'svg' ? svg : html ,
65
- prefix : prefix ,
55
+ prefix : prefix == null ? ( r || v || vd ? 'h-' : null ) : prefix ,
66
56
key : 0 ,
67
57
react : r ,
68
58
vue : v ,
@@ -76,125 +66,105 @@ function toH(h, node, ctx) {
76
66
var parentSchema = ctx . schema
77
67
var schema = parentSchema
78
68
var name = node . tagName
79
- var properties
80
- var attributes
81
- var children
82
- var property
83
- var elements
84
- var length
85
- var index
69
+ var attributes = { }
70
+ var nodes = [ ]
71
+ var index = - 1
72
+ var key
86
73
var value
87
- var result
88
74
89
75
if ( parentSchema . space === 'html' && name . toLowerCase ( ) === 'svg' ) {
90
76
schema = svg
91
77
ctx . schema = schema
92
78
}
93
79
94
- if ( ctx . vdom === true && schema . space === 'html' ) {
95
- name = name . toUpperCase ( )
80
+ for ( key in node . properties ) {
81
+ addAttribute ( attributes , key , node . properties [ key ] , ctx , name )
96
82
}
97
83
98
- properties = node . properties
99
- attributes = { }
100
-
101
- for ( property in properties ) {
102
- addAttribute ( attributes , property , properties [ property ] , ctx )
103
- }
104
-
105
- if (
106
- typeof attributes . style === 'string' &&
107
- ( ctx . vdom === true || ctx . vue === true || ctx . react === true )
108
- ) {
109
- // VDOM, Vue, and React accept `style` as object.
110
- attributes . style = parseStyle ( attributes . style , name )
84
+ if ( ctx . vdom ) {
85
+ if ( schema . space === 'html' ) {
86
+ name = name . toUpperCase ( )
87
+ } else {
88
+ attributes . namespace = ns [ schema . space ]
89
+ }
111
90
}
112
91
113
92
if ( ctx . prefix ) {
114
93
ctx . key ++
115
94
attributes . key = ctx . prefix + ctx . key
116
95
}
117
96
118
- if ( ctx . vdom && schema . space !== 'html' ) {
119
- attributes . namespace = ns [ schema . space ]
120
- }
121
-
122
- elements = [ ]
123
- children = node . children
124
- length = children ? children . length : 0
125
- index = - 1
97
+ if ( node . children ) {
98
+ while ( ++ index < node . children . length ) {
99
+ value = node . children [ index ]
126
100
127
- while ( ++ index < length ) {
128
- value = children [ index ]
129
-
130
- if ( element ( value ) ) {
131
- elements . push ( toH ( h , value , ctx ) )
132
- } else if ( text ( value ) ) {
133
- elements . push ( value . value )
101
+ if ( element ( value ) ) {
102
+ nodes . push ( toH ( h , value , ctx ) )
103
+ } else if ( text ( value ) ) {
104
+ nodes . push ( value . value )
105
+ }
134
106
}
135
107
}
136
108
137
- // Ensure no React warnings are triggered for void elements having children
138
- // passed in.
139
- result =
140
- elements . length === 0
141
- ? h . call ( node , name , attributes )
142
- : h . call ( node , name , attributes , elements )
143
-
144
109
// Restore parent schema.
145
110
ctx . schema = parentSchema
146
111
147
- return result
112
+ // Ensure no React warnings are triggered for void elements having children
113
+ // passed in.
114
+ return nodes . length
115
+ ? h . call ( node , name , attributes , nodes )
116
+ : h . call ( node , name , attributes )
148
117
}
149
118
150
- function addAttribute ( props , prop , value , ctx ) {
151
- var hyperlike = ctx . hyperscript || ctx . vdom || ctx . vue
152
- var schema = ctx . schema
153
- var info = find ( schema , prop )
119
+ function addAttribute ( props , prop , value , ctx , name ) {
120
+ var info = find ( ctx . schema , prop )
154
121
var subprop
155
122
156
123
// Ignore nullish and `NaN` values.
157
124
// Ignore `false` and falsey known booleans for hyperlike DSLs.
158
125
if (
159
- value === null ||
160
- value === undefined ||
126
+ value == null ||
161
127
value !== value ||
162
- ( hyperlike && value === false ) ||
163
- ( hyperlike && info . boolean && ! value )
128
+ ( value === false && ( ctx . vue || ctx . vdom || ctx . hyperscript ) ) ||
129
+ ( ! value && info . boolean && ( ctx . vue || ctx . vdom || ctx . hyperscript ) )
164
130
) {
165
131
return
166
132
}
167
133
168
- if ( value !== null && typeof value === 'object' && 'length' in value ) {
134
+ if ( value && typeof value === 'object' && 'length' in value ) {
169
135
// Accept `array`.
170
136
// Most props are space-separated.
171
137
value = ( info . commaSeparated ? commas : spaces ) . stringify ( value )
172
138
}
173
139
174
140
// Treat `true` and truthy known booleans.
175
- if ( info . boolean && ctx . hyperscript === true ) {
141
+ if ( info . boolean && ctx . hyperscript ) {
176
142
value = ''
177
143
}
178
144
145
+ // VDOM, Vue, and React accept `style` as object.
146
+ if (
147
+ info . property === 'style' &&
148
+ typeof value === 'string' &&
149
+ ( ctx . react || ctx . vue || ctx . vdom )
150
+ ) {
151
+ value = parseStyle ( value , name )
152
+ }
153
+
179
154
if ( ctx . vue ) {
180
- if ( prop !== 'style' ) {
181
- subprop = 'attrs'
182
- }
155
+ if ( info . property !== 'style' ) subprop = 'attrs'
183
156
} else if ( ! info . mustUseProperty ) {
184
- if ( ctx . vdom === true ) {
185
- subprop = 'attributes'
186
- } else if ( ctx . hyperscript === true ) {
157
+ if ( ctx . vdom ) {
158
+ if ( info . property !== 'style' ) subprop = 'attributes'
159
+ } else if ( ctx . hyperscript ) {
187
160
subprop = 'attrs'
188
161
}
189
162
}
190
163
191
164
if ( subprop ) {
192
- if ( props [ subprop ] === undefined ) {
193
- props [ subprop ] = { }
194
- }
195
-
165
+ if ( ! props [ subprop ] ) props [ subprop ] = { }
196
166
props [ subprop ] [ info . attribute ] = value
197
- } else if ( ctx . react && info . space ) {
167
+ } else if ( info . space && ctx . react ) {
198
168
props [ hastToReact [ info . property ] || info . property ] = value
199
169
} else {
200
170
props [ info . attribute ] = value
@@ -205,7 +175,7 @@ function addAttribute(props, prop, value, ctx) {
205
175
function react ( h ) {
206
176
var node = h && h ( 'div' )
207
177
return Boolean (
208
- node && ( '_owner' in node || '_store' in node ) && node . key === null
178
+ node && ( '_owner' in node || '_store' in node ) && node . key == null
209
179
)
210
180
}
211
181
@@ -238,18 +208,11 @@ function parseStyle(value, tagName) {
238
208
return result
239
209
240
210
function iterator ( name , value ) {
241
- result [ styleCase ( name ) ] = value
211
+ if ( name . slice ( 0 , 4 ) === '-ms-' ) name = 'ms-' + name . slice ( 4 )
212
+ result [ name . replace ( / - ( [ a - z ] ) / g, styleReplacer ) ] = value
242
213
}
243
214
}
244
215
245
- function styleCase ( value ) {
246
- if ( value . slice ( 0 , 4 ) === '-ms-' ) {
247
- value = 'ms-' + value . slice ( 4 )
248
- }
249
-
250
- return value . replace ( dashes , styleReplacer )
251
- }
252
-
253
216
function styleReplacer ( $0 , $1 ) {
254
217
return $1 . toUpperCase ( )
255
218
}
0 commit comments