@@ -64,10 +64,14 @@ const NODE_TYPE = {
6464
6565// Convert a parsed node to dd<el> code
6666function nodeToDDE ( node , options = { } , level = 0 ) {
67+ const tab = options . indent === "-1" ? "\t" : " " . repeat ( options . indent ) ;
68+ const indent = tab . repeat ( level ) ;
69+ const nextIndent = tab . repeat ( level + 1 ) ;
70+
6771 const { nodeType } = node ;
6872 // Handle text nodes
6973 if ( nodeType === NODE_TYPE . TEXT ) {
70- const text = node . nodeValue ;
74+ const text = el ( "i" , { innerText : node . nodeValue } ) . textContent ;
7175 if ( ! text . trim ( ) ) return null ;
7276
7377 // Return as plain text or template string for longer text
@@ -78,15 +82,15 @@ function nodeToDDE(node, options = {}, level = 0) {
7882
7983 // Handle comment nodes
8084 if ( nodeType === NODE_TYPE . COMMENT ) {
81- return null ; // TODO: Skip comments?
85+ const text = node . nodeValue ;
86+ if ( ! text . trim ( ) ) return null ;
87+ return text . includes ( "\n" )
88+ ? [ "/*" , ...text . trim ( ) . split ( "\n" ) . map ( l => tab + l ) , "*/" ]
89+ : [ `// ${ text } ` ] ;
8290 }
8391
8492 // For element nodes
8593 if ( nodeType === NODE_TYPE . ELEMENT ) {
86- const tab = options . indent === "-1" ? "\t" : " " . repeat ( options . indent ) ;
87- const indent = tab . repeat ( level ) ;
88- const nextIndent = tab . repeat ( level + 1 ) ;
89-
9094 // Special case for SVG elements
9195 const isNS = node . tagName === "svg" ;
9296 const elFunction = isNS ? "elNS" : "el" ;
@@ -171,12 +175,14 @@ function nodeToDDE(node, options = {}, level = 0) {
171175 // Process children
172176 const children = [ ] ;
173177 for ( const child of node . childNodes ) {
174- const childCode = nodeToDDE ( child , options , level + 1 ) ;
175- if ( childCode ) children . push ( childCode ) ;
178+ const childCode = nodeToDDE ( child , options , level + 1 ) ;
179+ if ( ! childCode ) continue ;
180+
181+ children . push ( childCode ) ;
176182 }
177- if ( children . length === 1 && node . childNodes [ 0 ] . nodeType === NODE_TYPE . TEXT ) {
178- const textContent = children . pop ( ) . slice ( 1 , - 1 ) ;
179- attrs . unshift ( `textContent: "${ textContent } "` ) ;
183+ if ( node . childNodes . length === 1 && node . childNodes [ 0 ] . nodeType === NODE_TYPE . TEXT ) {
184+ const textContent = children . pop ( ) . slice ( 1 , - 1 ) ;
185+ attrs . unshift ( `textContent: "${ textContent } "` ) ;
180186 }
181187
182188 // Build the element creation code
@@ -189,15 +195,16 @@ function nodeToDDE(node, options = {}, level = 0) {
189195 result += `, {\n${ nextIndent } ${ attrs . join ( `,\n${ nextIndent } ` ) } ,\n${ indent } }` ;
190196 else
191197 result += `, { ${ attrs . join ( ", " ) } }` ;
192- } else if ( children . length > 0 ) {
193- result += ", null" ;
194198 }
195199
196200 // Add children if any
197201 if ( children . length > 0 ) {
198- result += `).append(\n${ nextIndent } ${ children . join ( `,\n${ nextIndent } ` ) } ,\n${ indent } )` ;
202+ const chs = children . map ( ch =>
203+ Array . isArray ( ch ) ? ch . map ( l => nextIndent + l ) . join ( "\n" ) :
204+ nextIndent + ch + "," ) ;
205+ result += `).append(\n${ chs . join ( "\n" ) } \n${ indent } )` ;
199206 } else {
200- result += ")" ;
207+ result += ")" ;
201208 }
202209
203210 return result ;
0 commit comments