1
1
import info from 'property-information' ;
2
2
3
- const ROOT_NODE = 'root' ;
4
- const TEXT_NODE = 'text' ;
5
- const ELEMENT_NODE = 'element' ;
6
- const DOCUMENT_TYPE_NODE = 'doctype' ;
7
- const COMMENT_NODE = 'comment' ;
8
-
9
3
function transform ( node , options = { } ) {
10
4
switch ( node . type ) {
11
- case ROOT_NODE :
5
+ case 'root' :
12
6
return root ( node , options ) ;
13
- case TEXT_NODE :
7
+ case 'text' :
14
8
return text ( node , options ) ;
15
- case ELEMENT_NODE :
9
+ case 'element' :
16
10
return element ( node , options ) ;
17
- case DOCUMENT_TYPE_NODE :
11
+ case 'doctype' :
18
12
return doctype ( node , options ) ;
19
- case COMMENT_NODE :
13
+ case 'comment' :
20
14
return comment ( node , options ) ;
21
15
default :
22
16
return element ( node , options ) ;
23
17
}
24
18
}
25
19
26
- /**
27
- * Transform a document
28
- */
20
+ // Create a document.
29
21
function root ( node , options = { } ) {
30
- const {
31
- fragment,
32
- namespace : optionsNamespace ,
33
- } = options ;
22
+ const { fragment, namespace : optionsNamespace } = options ;
34
23
const { children = [ ] } = node ;
35
24
const { length : childrenLength } = children ;
36
25
37
26
let namespace = optionsNamespace ;
38
27
let rootIsDocument = childrenLength === 0 ;
39
28
40
29
for ( let i = 0 ; i < childrenLength ; i += 1 ) {
41
- const {
42
- tagName,
43
- properties : {
44
- xmlns,
45
- } = { } ,
46
- } = children [ i ] ;
30
+ const { tagName, properties : { xmlns } = { } } = children [ i ] ;
31
+
47
32
if ( tagName === 'html' ) {
48
- // If we have a root HTML node, we don' t need to render as a fragment
33
+ // If we have a root HTML node, we don’ t need to render as a fragment.
49
34
rootIsDocument = true ;
50
- // Take namespace of first child
35
+
36
+ // Take namespace of the first child.
51
37
if ( typeof optionsNamespace === 'undefined' ) {
52
38
if ( xmlns ) {
53
39
namespace = xmlns ;
@@ -58,8 +44,9 @@ function root(node, options = {}) {
58
44
}
59
45
}
60
46
61
- // The root node will be a Document, DocumentFragment, or HTMLElement
47
+ // The root node will be a Document, DocumentFragment, or HTMLElement.
62
48
let el ;
49
+
63
50
if ( rootIsDocument ) {
64
51
el = document . implementation . createDocument ( namespace , '' , null ) ;
65
52
} else if ( fragment ) {
@@ -68,10 +55,12 @@ function root(node, options = {}) {
68
55
el = document . createElement ( 'html' ) ;
69
56
}
70
57
71
- // Transform children
58
+ // Transform children.
72
59
const childOptions = Object . assign ( { fragment, namespace } , options ) ;
60
+
73
61
for ( let i = 0 ; i < childrenLength ; i += 1 ) {
74
62
const childEl = transform ( children [ i ] , childOptions ) ;
63
+
75
64
if ( childEl ) {
76
65
el . appendChild ( childEl ) ;
77
66
}
@@ -80,9 +69,7 @@ function root(node, options = {}) {
80
69
return el ;
81
70
}
82
71
83
- /**
84
- * Transform a DOCTYPE
85
- */
72
+ // Create a `doctype`.
86
73
function doctype ( node ) {
87
74
return document . implementation . createDocumentType (
88
75
node . name || 'html' ,
@@ -91,35 +78,31 @@ function doctype(node) {
91
78
) ;
92
79
}
93
80
94
- /**
95
- * Transform text node
96
- */
81
+ // Create a `text`.
97
82
function text ( node ) {
98
83
return document . createTextNode ( node . value ) ;
99
84
}
100
85
101
- /**
102
- * Transform a comment node
103
- */
86
+ // Create a `comment`.
104
87
function comment ( node ) {
105
88
return document . createComment ( node . value ) ;
106
89
}
107
90
108
- /**
109
- * Transform an element
110
- */
91
+ // Create an `element`.
111
92
function element ( node , options = { } ) {
112
93
const { namespace } = options ;
113
94
const { tagName, properties, children = [ ] } = node ;
114
95
const el = typeof namespace !== 'undefined'
115
96
? document . createElementNS ( namespace , tagName )
116
97
: document . createElement ( tagName ) ;
117
98
118
- // Add HTML attributes
99
+ // Add HTML attributes.
119
100
const props = Object . keys ( properties ) ;
120
101
const { length } = props ;
102
+
121
103
for ( let i = 0 ; i < length ; i += 1 ) {
122
104
const key = props [ i ] ;
105
+
123
106
const {
124
107
attribute,
125
108
property,
@@ -128,22 +111,18 @@ function element(node, options = {}) {
128
111
boolean,
129
112
booleanish,
130
113
overloadedBoolean,
131
- // number,
132
- // defined,
114
+ // ` number` ,
115
+ // ` defined` ,
133
116
commaSeparated,
134
- spaceSeparated,
135
- // commaOrSpaceSeparated,
136
- } = info . find ( info . html , key ) || {
137
- attribute : key ,
138
- property : key ,
139
- } ;
117
+ // `spaceSeparated`,
118
+ // `commaOrSpaceSeparated`,
119
+ } = info . find ( info . html , key ) || { attribute : key , property : key } ;
140
120
141
121
let value = properties [ key ] ;
122
+
142
123
if ( Array . isArray ( value ) ) {
143
124
if ( commaSeparated ) {
144
125
value = value . join ( ', ' ) ;
145
- } else if ( spaceSeparated ) {
146
- value = value . join ( ' ' ) ;
147
126
} else {
148
127
value = value . join ( ' ' ) ;
149
128
}
@@ -153,6 +132,7 @@ function element(node, options = {}) {
153
132
if ( mustUseProperty ) {
154
133
el [ property ] = value ;
155
134
}
135
+
156
136
if ( boolean || ( overloadedBoolean && typeof value === 'boolean' ) ) {
157
137
if ( value ) {
158
138
el . setAttribute ( attribute , '' ) ;
@@ -170,14 +150,17 @@ function element(node, options = {}) {
170
150
if ( ! mustUseAttribute && property ) {
171
151
el [ property ] = value ;
172
152
}
173
- // Otherwise silently ignore
153
+
154
+ // Otherwise silently ignore.
174
155
}
175
156
}
176
157
177
- // Transform children
158
+ // Transform children.
178
159
const { length : childrenLength } = children ;
160
+
179
161
for ( let i = 0 ; i < childrenLength ; i += 1 ) {
180
162
const childEl = transform ( children [ i ] , options ) ;
163
+
181
164
if ( childEl ) {
182
165
el . appendChild ( childEl ) ;
183
166
}
0 commit comments