Skip to content

Commit dae9afb

Browse files
committed
Add document fragments for native WPT setup
1 parent 8c4d931 commit dae9afb

3 files changed

Lines changed: 94 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/ui': patch
3+
---
4+
5+
Add document fragment creation and child transfer during insertion, and support reading and replacing text content on elements and fragments.

‎packages/ui/src/dom.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
375412
export 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

773818
export 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+
}

‎packages/ui/wpt.config.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
CharacterData,
66
Comment,
77
Document,
8+
DocumentFragment,
89
DocumentType,
910
Element,
1011
HTMLBodyElement,
@@ -29,6 +30,7 @@ const config: WPTConfig = {
2930
Comment,
3031
CustomEvent: globalThis.CustomEvent,
3132
Document,
33+
DocumentFragment,
3234
DocumentType,
3335
DOMException: globalThis.DOMException,
3436
Element,
@@ -161,8 +163,19 @@ const config: WPTConfig = {
161163
'Attribute with prefix in local name',
162164
'getAttributeNames tests',
163165
],
164-
'dom/nodes/Node-nodeValue.html': ['Text.nodeValue', 'Comment.nodeValue'],
166+
'dom/nodes/Node-nodeName.html': [
167+
'For DocumentFragment nodes, nodeName should return "#document-fragment".',
168+
],
169+
'dom/nodes/Node-nodeValue.html': [
170+
'Text.nodeValue',
171+
'Comment.nodeValue',
172+
'DocumentFragment.nodeValue',
173+
],
165174
'dom/nodes/Node-textContent.html': [
175+
'For an empty Element, textContent should be the empty string',
176+
/^Element with(?:out)? /,
177+
'For an empty DocumentFragment, textContent should be the empty string',
178+
/^DocumentFragment with(?:out)? /,
166179
'For an empty Text, textContent should be the empty string',
167180
'For an empty Comment, textContent should be the empty string',
168181
'For a Text with data, textContent should be that data',
@@ -177,6 +190,10 @@ const config: WPTConfig = {
177190
'When the parent is null, parentElement should be null',
178191
'When the parent is a document, parentElement should be null (element)',
179192
'When the parent is a document, parentElement should be null (comment)',
193+
'parentElement should return null for children of DocumentFragments (element)',
194+
'parentElement should return null for children of DocumentFragments (text)',
195+
'parentElement should work correctly with DocumentFragments (element)',
196+
'parentElement should work correctly with DocumentFragments (text)',
180197
'parentElement should work correctly in disconnected subtrees (element)',
181198
'parentElement should work correctly in disconnected subtrees (text)',
182199
],

0 commit comments

Comments
 (0)