Skip to content
Merged
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 and child transfer during insertion, and support reading and replacing text content on elements and fragments.
71 changes: 71 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,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 {
Comment thread
cowboyd marked this conversation as resolved.
Resolved
return getTextContent(this);
}

override set textContent(value: string | null) {
setTextContent(this, value == null ? '' : `${value}`);
}
}

export class DocumentType extends Node {
declare readonly ownerDocument: Document;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
}
}
19 changes: 18 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,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',
Expand All @@ -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)',
],
Expand Down
Loading