From 8ce47b652b87cc31abce6c00b5e26ba41d03d552 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 | 71 +++++++++++++++++++++++++++++ packages/ui/wpt.config.ts | 19 +++++++- 3 files changed, 94 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..8812f1f --- /dev/null +++ b/.changeset/dom-document-fragment.md @@ -0,0 +1,5 @@ +--- +'@clack/ui': patch +--- + +Add document fragment creation and child transfer during insertion, and support reading and replacing text content on elements and fragments. diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 4833873..43ed638 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,30 @@ 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 { + return getTextContent(this); + } + + override set textContent(value: string | null) { + setTextContent(this, value == null ? '' : `${value}`); + } +} + export class DocumentType extends Node { declare readonly ownerDocument: Document; @@ -442,6 +479,14 @@ export class Element extends Node { return this.nodeName; } + override get textContent(): string { + return getTextContent(this); + } + + override set textContent(value: string | null) { + setTextContent(this, value == null ? '' : `${value}`); + } + get children(): Element[] { return this.childNodes.filter((node) => node instanceof Element); } @@ -771,3 +816,29 @@ export class HTMLUnknownElement extends HTMLElement {} /** XML */ export class XMLDocument extends Document {} + +/** internal helpers **/ + +function getTextContent(node: Element | DocumentFragment): string { + const parts: string[] = []; + const stack = node.childNodes.toReversed(); + while (stack.length > 0) { + const descendant = stack.pop()!; + if (descendant instanceof Text) { + parts.push(descendant.data); + } + for (let index = descendant.childNodes.length - 1; index >= 0; index--) { + stack.push(descendant.childNodes[index]!); + } + } + return parts.join(''); +} + +function setTextContent(node: Element | DocumentFragment, content: string): void { + while (node.firstChild) { + node.removeChild(node.firstChild); + } + if (content !== '') { + node.appendChild(node.ownerDocument.createTextNode(content)); + } +} diff --git a/packages/ui/wpt.config.ts b/packages/ui/wpt.config.ts index 1193847..3aae9e5 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,19 @@ 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 Element, textContent should be the empty string', + /^Element with(?:out)? /, + '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 +190,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)', ],