File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " ultrahtml " : minor
3
+ ---
4
+
5
+ Add ` renderSync ` export
Original file line number Diff line number Diff line change @@ -404,6 +404,36 @@ async function renderElement(node: Node): Promise<string> {
404
404
return `<${ node . name } ${ attrs ( attributes ) . value } >${ children } </${ node . name } >` ;
405
405
}
406
406
407
+ function renderElementSync ( node : Node ) : string {
408
+ const { name, attributes = { } } = node ;
409
+ const children = node . children . map ( ( child : Node ) => renderSync ( child ) ) . join ( "" ) ;
410
+ if ( RenderFn in node ) {
411
+ const value = ( node as any ) [ RenderFn ] ( attributes , mark ( children ) ) ;
412
+ if ( value && ( value as any ) [ HTMLString ] ) return value . value ;
413
+ return escapeHTML ( String ( value ) ) ;
414
+ }
415
+ if ( name === Fragment ) return children ;
416
+ if ( VOID_TAGS . has ( name ) ) {
417
+ return `<${ node . name } ${ attrs ( attributes ) . value } >` ;
418
+ }
419
+ return `<${ node . name } ${ attrs ( attributes ) . value } >${ children } </${ node . name } >` ;
420
+ }
421
+
422
+ export function renderSync ( node : Node ) : string {
423
+ switch ( node . type ) {
424
+ case DOCUMENT_NODE :
425
+ return node . children . map ( ( child : Node ) => renderSync ( child ) ) . join ( "" ) ;
426
+ case ELEMENT_NODE :
427
+ return renderElementSync ( node ) ;
428
+ case TEXT_NODE :
429
+ return `${ node . value } ` ;
430
+ case COMMENT_NODE :
431
+ return `<!--${ node . value } -->` ;
432
+ case DOCTYPE_NODE :
433
+ return `<!${ node . value } >` ;
434
+ }
435
+ }
436
+
407
437
export async function render ( node : Node ) : Promise < string > {
408
438
switch ( node . type ) {
409
439
case DOCUMENT_NODE :
You can’t perform that action at this time.
0 commit comments