@@ -21,6 +21,7 @@ export abstract class Node<Owner extends Document | null = Document | null> exte
2121 static readonly COMMENT_NODE = 8 ;
2222 static readonly DOCUMENT_NODE = 9 ;
2323 static readonly DOCUMENT_TYPE_NODE = 10 ;
24+ static readonly DOCUMENT_FRAGMENT_NODE = 11 ;
2425
2526 readonly ELEMENT_NODE = Node . ELEMENT_NODE ;
2627 readonly ATTRIBUTE_NODE = Node . ATTRIBUTE_NODE ;
@@ -29,6 +30,7 @@ export abstract class Node<Owner extends Document | null = Document | null> exte
2930 readonly COMMENT_NODE = Node . COMMENT_NODE ;
3031 readonly DOCUMENT_NODE = Node . DOCUMENT_NODE ;
3132 readonly DOCUMENT_TYPE_NODE = Node . DOCUMENT_TYPE_NODE ;
33+ readonly DOCUMENT_FRAGMENT_NODE = Node . DOCUMENT_FRAGMENT_NODE ;
3234
3335 readonly #childNodes: Node [ ] = [ ] ;
3436 readonly ownerDocument : Owner ;
@@ -194,6 +196,13 @@ export abstract class Node<Owner extends Document | null = Document | null> exte
194196 throw new DOMException ( 'cannot make a Node a child of itself' , 'HierarchyRequestError' ) ;
195197 }
196198
199+ if ( node instanceof DocumentFragment ) {
200+ for ( const child of node . childNodes . slice ( ) ) {
201+ this . insertBefore ( child , anchor ) ;
202+ }
203+ return node ;
204+ }
205+
197206 if ( node === anchor ) {
198207 return node ;
199208 }
@@ -353,6 +362,10 @@ export class Document extends Node<null> {
353362 return new Text ( this , String ( data ) , this . notify ) ;
354363 }
355364
365+ createDocumentFragment ( ) : DocumentFragment {
366+ return new DocumentFragment ( this , this . notify ) ;
367+ }
368+
356369 createComment ( data : string ) : Text {
357370 return new Comment ( this , String ( data ) , this . notify ) ;
358371 }
@@ -370,6 +383,41 @@ export class Document extends Node<null> {
370383 }
371384}
372385
386+ export class DocumentFragment extends Node < Document > {
387+ get nodeType ( ) : number {
388+ return Node . DOCUMENT_FRAGMENT_NODE ;
389+ }
390+
391+ get nodeName ( ) : string {
392+ return '#document-fragment' ;
393+ }
394+
395+ override get textContent ( ) : string {
396+ const parts : string [ ] = [ ] ;
397+ const stack = this . childNodes . toReversed ( ) ;
398+ while ( stack . length > 0 ) {
399+ const node = stack . pop ( ) ! ;
400+ if ( node instanceof Text ) {
401+ parts . push ( node . data ) ;
402+ }
403+ for ( let index = node . childNodes . length - 1 ; index >= 0 ; index -- ) {
404+ stack . push ( node . childNodes [ index ] ! ) ;
405+ }
406+ }
407+ return parts . join ( '' ) ;
408+ }
409+
410+ override set textContent ( value : string | null ) {
411+ const data = value == null ? '' : `${ value } ` ;
412+ while ( this . firstChild ) {
413+ this . removeChild ( this . firstChild ) ;
414+ }
415+ if ( data !== '' ) {
416+ this . appendChild ( this . ownerDocument . createTextNode ( data ) ) ;
417+ }
418+ }
419+ }
420+
373421export class DocumentType extends Node < Document > {
374422 readonly name : string ;
375423 readonly publicId : string ;
0 commit comments