11import info from 'property-information' ;
22
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-
93function transform ( node , options = { } ) {
104 switch ( node . type ) {
11- case ROOT_NODE :
5+ case 'root' :
126 return root ( node , options ) ;
13- case TEXT_NODE :
7+ case 'text' :
148 return text ( node , options ) ;
15- case ELEMENT_NODE :
9+ case 'element' :
1610 return element ( node , options ) ;
17- case DOCUMENT_TYPE_NODE :
11+ case 'doctype' :
1812 return doctype ( node , options ) ;
19- case COMMENT_NODE :
13+ case 'comment' :
2014 return comment ( node , options ) ;
2115 default :
2216 return element ( node , options ) ;
2317 }
2418}
2519
26- /**
27- * Transform a document
28- */
20+ // Create a document.
2921function root ( node , options = { } ) {
30- const {
31- fragment,
32- namespace : optionsNamespace ,
33- } = options ;
22+ const { fragment, namespace : optionsNamespace } = options ;
3423 const { children = [ ] } = node ;
3524 const { length : childrenLength } = children ;
3625
3726 let namespace = optionsNamespace ;
3827 let rootIsDocument = childrenLength === 0 ;
3928
4029 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+
4732 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.
4934 rootIsDocument = true ;
50- // Take namespace of first child
35+
36+ // Take namespace of the first child.
5137 if ( typeof optionsNamespace === 'undefined' ) {
5238 if ( xmlns ) {
5339 namespace = xmlns ;
@@ -58,8 +44,9 @@ function root(node, options = {}) {
5844 }
5945 }
6046
61- // The root node will be a Document, DocumentFragment, or HTMLElement
47+ // The root node will be a Document, DocumentFragment, or HTMLElement.
6248 let el ;
49+
6350 if ( rootIsDocument ) {
6451 el = document . implementation . createDocument ( namespace , '' , null ) ;
6552 } else if ( fragment ) {
@@ -68,10 +55,12 @@ function root(node, options = {}) {
6855 el = document . createElement ( 'html' ) ;
6956 }
7057
71- // Transform children
58+ // Transform children.
7259 const childOptions = Object . assign ( { fragment, namespace } , options ) ;
60+
7361 for ( let i = 0 ; i < childrenLength ; i += 1 ) {
7462 const childEl = transform ( children [ i ] , childOptions ) ;
63+
7564 if ( childEl ) {
7665 el . appendChild ( childEl ) ;
7766 }
@@ -80,9 +69,7 @@ function root(node, options = {}) {
8069 return el ;
8170}
8271
83- /**
84- * Transform a DOCTYPE
85- */
72+ // Create a `doctype`.
8673function doctype ( node ) {
8774 return document . implementation . createDocumentType (
8875 node . name || 'html' ,
@@ -91,35 +78,31 @@ function doctype(node) {
9178 ) ;
9279}
9380
94- /**
95- * Transform text node
96- */
81+ // Create a `text`.
9782function text ( node ) {
9883 return document . createTextNode ( node . value ) ;
9984}
10085
101- /**
102- * Transform a comment node
103- */
86+ // Create a `comment`.
10487function comment ( node ) {
10588 return document . createComment ( node . value ) ;
10689}
10790
108- /**
109- * Transform an element
110- */
91+ // Create an `element`.
11192function element ( node , options = { } ) {
11293 const { namespace } = options ;
11394 const { tagName, properties, children = [ ] } = node ;
11495 const el = typeof namespace !== 'undefined'
11596 ? document . createElementNS ( namespace , tagName )
11697 : document . createElement ( tagName ) ;
11798
118- // Add HTML attributes
99+ // Add HTML attributes.
119100 const props = Object . keys ( properties ) ;
120101 const { length } = props ;
102+
121103 for ( let i = 0 ; i < length ; i += 1 ) {
122104 const key = props [ i ] ;
105+
123106 const {
124107 attribute,
125108 property,
@@ -128,22 +111,18 @@ function element(node, options = {}) {
128111 boolean,
129112 booleanish,
130113 overloadedBoolean,
131- // number,
132- // defined,
114+ // ` number` ,
115+ // ` defined` ,
133116 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 } ;
140120
141121 let value = properties [ key ] ;
122+
142123 if ( Array . isArray ( value ) ) {
143124 if ( commaSeparated ) {
144125 value = value . join ( ', ' ) ;
145- } else if ( spaceSeparated ) {
146- value = value . join ( ' ' ) ;
147126 } else {
148127 value = value . join ( ' ' ) ;
149128 }
@@ -153,6 +132,7 @@ function element(node, options = {}) {
153132 if ( mustUseProperty ) {
154133 el [ property ] = value ;
155134 }
135+
156136 if ( boolean || ( overloadedBoolean && typeof value === 'boolean' ) ) {
157137 if ( value ) {
158138 el . setAttribute ( attribute , '' ) ;
@@ -170,14 +150,17 @@ function element(node, options = {}) {
170150 if ( ! mustUseAttribute && property ) {
171151 el [ property ] = value ;
172152 }
173- // Otherwise silently ignore
153+
154+ // Otherwise silently ignore.
174155 }
175156 }
176157
177- // Transform children
158+ // Transform children.
178159 const { length : childrenLength } = children ;
160+
179161 for ( let i = 0 ; i < childrenLength ; i += 1 ) {
180162 const childEl = transform ( children [ i ] , options ) ;
163+
181164 if ( childEl ) {
182165 el . appendChild ( childEl ) ;
183166 }
0 commit comments