@@ -438,17 +438,20 @@ function replaceHtmlWithPlaceholders(
438
438
const components : Array < string | Placeholder > = [ ] ;
439
439
440
440
const traverse = ( node : parse5 . DefaultTreeNode ) : void => {
441
- const text =
442
- node . nodeName === '#text'
443
- ? ( node as parse5 . DefaultTreeTextNode ) . value
444
- : null ;
445
- if ( text !== null ) {
441
+ if ( node . nodeName === '#text' ) {
442
+ const text = ( node as parse5 . DefaultTreeTextNode ) . value ;
446
443
components . push ( text ) ;
444
+ } else if ( node . nodeName === '#comment' ) {
445
+ components . push ( {
446
+ untranslatable : serializeComment ( node as parse5 . DefaultTreeCommentNode ) ,
447
+ } ) ;
447
448
} else {
448
449
const { open, close} = serializeOpenCloseTags ( node ) ;
449
450
components . push ( { untranslatable : open } ) ;
450
- for ( const child of ( node as parse5 . DefaultTreeParentNode ) . childNodes ) {
451
- traverse ( child ) ;
451
+ if ( 'childNodes' in node ) {
452
+ for ( const child of ( node as parse5 . DefaultTreeParentNode ) . childNodes ) {
453
+ traverse ( child ) ;
454
+ }
452
455
}
453
456
components . push ( { untranslatable : close } ) ;
454
457
}
@@ -481,6 +484,17 @@ function serializeOpenCloseTags(
481
484
return { open, close} ;
482
485
}
483
486
487
+ /**
488
+ * Serialize an HTML comment node.
489
+ *
490
+ * Example:
491
+ *
492
+ * {data: "foo"} --> "<!-- foo -->"
493
+ */
494
+ function serializeComment ( comment : parse5 . DefaultTreeCommentNode ) : string {
495
+ return parse5 . serialize ( { childNodes : [ comment ] } ) ;
496
+ }
497
+
484
498
/**
485
499
* E.g. "foo", 'foo', or `foo`, but not `foo${bar}`.
486
500
*/
0 commit comments