From 3d233a4ca18d60738a4a5a4c3b6325cc742816a3 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Tue, 17 Nov 2026 15:30:32 -0600 Subject: [PATCH] Add document fragments for native WPT setup --- .changeset/dom-document-fragment.md | 5 +++ packages/ui/src/dom.ts | 54 +++++++++++++++++++++++++++++ packages/ui/wpt.config.ts | 17 ++++++++- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 .changeset/dom-document-fragment.md diff --git a/.changeset/dom-document-fragment.md b/.changeset/dom-document-fragment.md new file mode 100644 index 0000000..1b2bda0 --- /dev/null +++ b/.changeset/dom-document-fragment.md @@ -0,0 +1,5 @@ +--- +'@clack/ui': patch +--- + +Add document fragment creation, text content, and child transfer during insertion. diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 4833873..cf33be6 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -21,6 +21,7 @@ export abstract class Node extends EventTarget { static readonly COMMENT_NODE = 8; static readonly DOCUMENT_NODE = 9; static readonly DOCUMENT_TYPE_NODE = 10; + static readonly DOCUMENT_FRAGMENT_NODE = 11; readonly ELEMENT_NODE = Node.ELEMENT_NODE; readonly ATTRIBUTE_NODE = Node.ATTRIBUTE_NODE; @@ -29,6 +30,7 @@ export abstract class Node extends EventTarget { readonly COMMENT_NODE = Node.COMMENT_NODE; readonly DOCUMENT_NODE = Node.DOCUMENT_NODE; readonly DOCUMENT_TYPE_NODE = Node.DOCUMENT_TYPE_NODE; + readonly DOCUMENT_FRAGMENT_NODE = Node.DOCUMENT_FRAGMENT_NODE; readonly #childNodes: Node[] = []; readonly ownerDocument: Document | null; @@ -194,6 +196,13 @@ export abstract class Node extends EventTarget { throw new DOMException('cannot make a Node a child of itself', 'HierarchyRequestError'); } + if (node instanceof DocumentFragment) { + for (const child of node.childNodes.slice()) { + this.insertBefore(child, anchor); + } + return node; + } + if (node === anchor) { return node; } @@ -355,6 +364,10 @@ export class Document extends Node { return new Text(this, String(data), this.notify); } + createDocumentFragment(): DocumentFragment { + return new DocumentFragment(this, this.notify); + } + createComment(data: string): Text { return new Comment(this, String(data), this.notify); } @@ -372,6 +385,47 @@ export class Document extends Node { } } +export class DocumentFragment extends Node { + declare readonly ownerDocument: Document; + + constructor(owner: Document, notify: Node['notify']) { + super(owner, notify); + } + + get nodeType(): number { + return Node.DOCUMENT_FRAGMENT_NODE; + } + + get nodeName(): string { + return '#document-fragment'; + } + + override get textContent(): string { + const parts: string[] = []; + const stack = this.childNodes.toReversed(); + while (stack.length > 0) { + const node = stack.pop()!; + if (node instanceof Text) { + parts.push(node.data); + } + for (let index = node.childNodes.length - 1; index >= 0; index--) { + stack.push(node.childNodes[index]!); + } + } + return parts.join(''); + } + + override set textContent(value: string | null) { + const data = value == null ? '' : `${value}`; + while (this.firstChild) { + this.removeChild(this.firstChild); + } + if (data !== '') { + this.appendChild(this.ownerDocument.createTextNode(data)); + } + } +} + export class DocumentType extends Node { declare readonly ownerDocument: Document; diff --git a/packages/ui/wpt.config.ts b/packages/ui/wpt.config.ts index 90cec07..02ff92f 100644 --- a/packages/ui/wpt.config.ts +++ b/packages/ui/wpt.config.ts @@ -5,6 +5,7 @@ import { CharacterData, Comment, Document, + DocumentFragment, DocumentType, Element, HTMLBodyElement, @@ -29,6 +30,7 @@ const config: WPTConfig = { Comment, CustomEvent: globalThis.CustomEvent, Document, + DocumentFragment, DocumentType, DOMException: globalThis.DOMException, Element, @@ -161,8 +163,17 @@ const config: WPTConfig = { 'Attribute with prefix in local name', 'getAttributeNames tests', ], - 'dom/nodes/Node-nodeValue.html': ['Text.nodeValue', 'Comment.nodeValue'], + 'dom/nodes/Node-nodeName.html': [ + 'For DocumentFragment nodes, nodeName should return "#document-fragment".', + ], + 'dom/nodes/Node-nodeValue.html': [ + 'Text.nodeValue', + 'Comment.nodeValue', + 'DocumentFragment.nodeValue', + ], 'dom/nodes/Node-textContent.html': [ + 'For an empty DocumentFragment, textContent should be the empty string', + /^DocumentFragment with(?:out)? /, 'For an empty Text, textContent should be the empty string', 'For an empty Comment, textContent should be the empty string', 'For a Text with data, textContent should be that data', @@ -177,6 +188,10 @@ const config: WPTConfig = { 'When the parent is null, parentElement should be null', 'When the parent is a document, parentElement should be null (element)', 'When the parent is a document, parentElement should be null (comment)', + 'parentElement should return null for children of DocumentFragments (element)', + 'parentElement should return null for children of DocumentFragments (text)', + 'parentElement should work correctly with DocumentFragments (element)', + 'parentElement should work correctly with DocumentFragments (text)', 'parentElement should work correctly in disconnected subtrees (element)', 'parentElement should work correctly in disconnected subtrees (text)', ],