@@ -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,30 @@ 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+ return getTextContent ( this ) ;
405+ }
406+
407+ override set textContent ( value : string | null ) {
408+ setTextContent ( this , value == null ? '' : `${ value } ` ) ;
409+ }
410+ }
411+
375412export class DocumentType extends Node {
376413 declare readonly ownerDocument : Document ;
377414
@@ -442,6 +479,14 @@ export class Element extends Node {
442479 return this . nodeName ;
443480 }
444481
482+ override get textContent ( ) : string {
483+ return getTextContent ( this ) ;
484+ }
485+
486+ override set textContent ( value : string | null ) {
487+ setTextContent ( this , value == null ? '' : `${ value } ` ) ;
488+ }
489+
445490 get children ( ) : Element [ ] {
446491 return this . childNodes . filter ( ( node ) => node instanceof Element ) ;
447492 }
@@ -771,3 +816,29 @@ export class HTMLUnknownElement extends HTMLElement {}
771816/** XML */
772817
773818export class XMLDocument extends Document { }
819+
820+ /** internal helpers **/
821+
822+ function getTextContent ( node : Element | DocumentFragment ) : string {
823+ const parts : string [ ] = [ ] ;
824+ const stack = node . childNodes . toReversed ( ) ;
825+ while ( stack . length > 0 ) {
826+ const descendant = stack . pop ( ) ! ;
827+ if ( descendant instanceof Text ) {
828+ parts . push ( descendant . data ) ;
829+ }
830+ for ( let index = descendant . childNodes . length - 1 ; index >= 0 ; index -- ) {
831+ stack . push ( descendant . childNodes [ index ] ! ) ;
832+ }
833+ }
834+ return parts . join ( '' ) ;
835+ }
836+
837+ function setTextContent ( node : Element | DocumentFragment , content : string ) : void {
838+ while ( node . firstChild ) {
839+ node . removeChild ( node . firstChild ) ;
840+ }
841+ if ( content !== '' ) {
842+ node . appendChild ( node . ownerDocument . createTextNode ( content ) ) ;
843+ }
844+ }
0 commit comments