1
+ import ns from 'web-namespaces' ;
1
2
import info from 'property-information' ;
2
3
3
- function transform ( node , options = { } ) {
4
+ function transform ( node , options ) {
4
5
switch ( node . type ) {
5
6
case 'root' :
6
7
return root ( node , options ) ;
@@ -18,7 +19,7 @@ function transform(node, options = {}) {
18
19
}
19
20
20
21
// Create a document.
21
- function root ( node , options = { } ) {
22
+ function root ( node , options ) {
22
23
const { fragment, namespace : optionsNamespace } = options ;
23
24
const { children = [ ] } = node ;
24
25
const { length : childrenLength } = children ;
@@ -27,19 +28,15 @@ function root(node, options = {}) {
27
28
let rootIsDocument = childrenLength === 0 ;
28
29
29
30
for ( let i = 0 ; i < childrenLength ; i += 1 ) {
30
- const { tagName, properties : { xmlns } = { } } = children [ i ] ;
31
+ const { tagName, properties = { } } = children [ i ] ;
31
32
32
33
if ( tagName === 'html' ) {
33
34
// If we have a root HTML node, we don’t need to render as a fragment.
34
35
rootIsDocument = true ;
35
36
36
37
// Take namespace of the first child.
37
38
if ( typeof optionsNamespace === 'undefined' ) {
38
- if ( xmlns ) {
39
- namespace = xmlns ;
40
- } else if ( children [ 0 ] . tagName === 'html' ) {
41
- namespace = 'http://www.w3.org/1999/xhtml' ;
42
- }
39
+ namespace = properties . xmlns || ns . html ;
43
40
}
44
41
}
45
42
}
@@ -55,18 +52,7 @@ function root(node, options = {}) {
55
52
el = document . createElement ( 'html' ) ;
56
53
}
57
54
58
- // Transform children.
59
- const childOptions = Object . assign ( { fragment, namespace } , options ) ;
60
-
61
- for ( let i = 0 ; i < childrenLength ; i += 1 ) {
62
- const childEl = transform ( children [ i ] , childOptions ) ;
63
-
64
- if ( childEl ) {
65
- el . appendChild ( childEl ) ;
66
- }
67
- }
68
-
69
- return el ;
55
+ return appendAll ( el , children , Object . assign ( { fragment, namespace } , options ) ) ;
70
56
}
71
57
72
58
// Create a `doctype`.
@@ -89,9 +75,10 @@ function comment(node) {
89
75
}
90
76
91
77
// Create an `element`.
92
- function element ( node , options = { } ) {
78
+ function element ( node , options ) {
93
79
const { namespace } = options ;
94
- const { tagName, properties, children = [ ] } = node ;
80
+ // TODO: use `g` in SVG space.
81
+ const { tagName = 'div' , properties = { } , children = [ ] } = node ;
95
82
const el = typeof namespace !== 'undefined'
96
83
? document . createElementNS ( namespace , tagName )
97
84
: document . createElement ( tagName ) ;
@@ -106,7 +93,7 @@ function element(node, options = {}) {
106
93
const {
107
94
attribute,
108
95
property,
109
- mustUseAttribute,
96
+ // ` mustUseAttribute` ,
110
97
mustUseProperty,
111
98
boolean,
112
99
booleanish,
@@ -116,59 +103,48 @@ function element(node, options = {}) {
116
103
commaSeparated,
117
104
// `spaceSeparated`,
118
105
// `commaOrSpaceSeparated`,
119
- } = info . find ( info . html , key ) || { attribute : key , property : key } ;
106
+ } = info . find ( info . html , key ) ;
120
107
121
108
let value = properties [ key ] ;
122
109
123
110
if ( Array . isArray ( value ) ) {
124
- if ( commaSeparated ) {
125
- value = value . join ( ', ' ) ;
126
- } else {
127
- value = value . join ( ' ' ) ;
128
- }
111
+ value = value . join ( commaSeparated ? ', ' : ' ' ) ;
129
112
}
130
113
131
- try {
132
- if ( mustUseProperty ) {
133
- el [ property ] = value ;
134
- }
114
+ if ( mustUseProperty ) {
115
+ el [ property ] = value ;
116
+ }
135
117
136
- if ( boolean || ( overloadedBoolean && typeof value === 'boolean' ) ) {
137
- if ( value ) {
138
- el . setAttribute ( attribute , '' ) ;
139
- } else {
140
- el . removeAttribute ( attribute ) ;
141
- }
142
- } else if ( booleanish ) {
143
- el . setAttribute ( attribute , value ) ;
144
- } else if ( value === true ) {
118
+ if ( boolean || ( overloadedBoolean && typeof value === 'boolean' ) ) {
119
+ if ( value ) {
145
120
el . setAttribute ( attribute , '' ) ;
146
- } else if ( value || value === 0 || value === '' ) {
147
- el . setAttribute ( attribute , value ) ;
148
- }
149
- } catch ( e ) {
150
- if ( ! mustUseAttribute && property ) {
151
- el [ property ] = value ;
121
+ } else {
122
+ el . removeAttribute ( attribute ) ;
152
123
}
153
-
154
- // Otherwise silently ignore.
124
+ } else if ( booleanish ) {
125
+ el . setAttribute ( attribute , value ) ;
126
+ } else if ( value === true ) {
127
+ el . setAttribute ( attribute , '' ) ;
128
+ } else if ( value || value === 0 || value === '' ) {
129
+ el . setAttribute ( attribute , value ) ;
155
130
}
156
131
}
157
132
158
- // Transform children.
159
- const { length : childrenLength } = children ;
133
+ return appendAll ( el , children , options ) ;
134
+ }
160
135
161
- for ( let i = 0 ; i < childrenLength ; i += 1 ) {
162
- const childEl = transform ( children [ i ] , options ) ;
136
+ // Add all children.
137
+ function appendAll ( node , children , options ) {
138
+ const childrenLength = children . length ;
163
139
164
- if ( childEl ) {
165
- el . appendChild ( childEl ) ;
166
- }
140
+ for ( let i = 0 ; i < childrenLength ; i += 1 ) {
141
+ node . appendChild ( transform ( children [ i ] , options ) ) ;
167
142
}
168
143
169
- return el ;
144
+ return node ;
170
145
}
171
146
147
+
172
148
export default function toDOM ( hast , options = { } ) {
173
149
return transform ( hast , options ) ;
174
150
}
0 commit comments