@@ -21,6 +21,7 @@ export abstract class Node extends EventTarget {
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 extends EventTarget {
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 : Document | null ;
@@ -194,6 +196,13 @@ export abstract class Node extends EventTarget {
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 }
@@ -355,6 +364,10 @@ export class Document extends Node {
355364 return new Text ( this , String ( data ) , this . notify ) ;
356365 }
357366
367+ createDocumentFragment ( ) : DocumentFragment {
368+ return new DocumentFragment ( this , this . notify ) ;
369+ }
370+
358371 createComment ( data : string ) : Text {
359372 return new Comment ( this , String ( data ) , this . notify ) ;
360373 }
@@ -372,6 +385,47 @@ export class Document extends Node {
372385 }
373386}
374387
388+ export class DocumentFragment extends Node {
389+ declare readonly ownerDocument : Document ;
390+
391+ constructor ( owner : Document , notify : Node [ 'notify' ] ) {
392+ super ( owner , notify ) ;
393+ }
394+
395+ get nodeType ( ) : number {
396+ return Node . DOCUMENT_FRAGMENT_NODE ;
397+ }
398+
399+ get nodeName ( ) : string {
400+ return '#document-fragment' ;
401+ }
402+
403+ override get textContent ( ) : string {
404+ const parts : string [ ] = [ ] ;
405+ const stack = this . childNodes . toReversed ( ) ;
406+ while ( stack . length > 0 ) {
407+ const node = stack . pop ( ) ! ;
408+ if ( node instanceof Text ) {
409+ parts . push ( node . data ) ;
410+ }
411+ for ( let index = node . childNodes . length - 1 ; index >= 0 ; index -- ) {
412+ stack . push ( node . childNodes [ index ] ! ) ;
413+ }
414+ }
415+ return parts . join ( '' ) ;
416+ }
417+
418+ override set textContent ( value : string | null ) {
419+ const data = value == null ? '' : `${ value } ` ;
420+ while ( this . firstChild ) {
421+ this . removeChild ( this . firstChild ) ;
422+ }
423+ if ( data !== '' ) {
424+ this . appendChild ( this . ownerDocument . createTextNode ( data ) ) ;
425+ }
426+ }
427+ }
428+
375429export class DocumentType extends Node {
376430 declare readonly ownerDocument : Document ;
377431
0 commit comments