Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dom-document-fragment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': patch
---

Add document fragment creation, text content, and child transfer during insertion.
54 changes: 54 additions & 0 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;

Expand Down
17 changes: 16 additions & 1 deletion packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CharacterData,
Comment,
Document,
DocumentFragment,
DocumentType,
Element,
HTMLBodyElement,
Expand All @@ -29,6 +30,7 @@ const config: WPTConfig = {
Comment,
CustomEvent: globalThis.CustomEvent,
Document,
DocumentFragment,
DocumentType,
DOMException: globalThis.DOMException,
Element,
Expand Down Expand Up @@ -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',
Expand All @@ -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)',
],
Expand Down
Loading