Skip to content

Commit cf99db9

Browse files
committed
Add document fragments for native WPT setup
1 parent 2f90e0a commit cf99db9

3 files changed

Lines changed: 69 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, text content, and child transfer during insertion.

‎packages/ui/src/dom.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export abstract class Node<Owner extends Document | null = Document | null> exte
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<Owner extends Document | null = Document | null> exte
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: Owner;
@@ -194,6 +196,13 @@ export abstract class Node<Owner extends Document | null = Document | null> exte
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
}
@@ -353,6 +362,10 @@ export class Document extends Node<null> {
353362
return new Text(this, String(data), this.notify);
354363
}
355364

365+
createDocumentFragment(): DocumentFragment {
366+
return new DocumentFragment(this, this.notify);
367+
}
368+
356369
createComment(data: string): Text {
357370
return new Comment(this, String(data), this.notify);
358371
}
@@ -370,6 +383,41 @@ export class Document extends Node<null> {
370383
}
371384
}
372385

386+
export class DocumentFragment extends Node<Document> {
387+
get nodeType(): number {
388+
return Node.DOCUMENT_FRAGMENT_NODE;
389+
}
390+
391+
get nodeName(): string {
392+
return '#document-fragment';
393+
}
394+
395+
override get textContent(): string {
396+
const parts: string[] = [];
397+
const stack = this.childNodes.toReversed();
398+
while (stack.length > 0) {
399+
const node = stack.pop()!;
400+
if (node instanceof Text) {
401+
parts.push(node.data);
402+
}
403+
for (let index = node.childNodes.length - 1; index >= 0; index--) {
404+
stack.push(node.childNodes[index]!);
405+
}
406+
}
407+
return parts.join('');
408+
}
409+
410+
override set textContent(value: string | null) {
411+
const data = value == null ? '' : `${value}`;
412+
while (this.firstChild) {
413+
this.removeChild(this.firstChild);
414+
}
415+
if (data !== '') {
416+
this.appendChild(this.ownerDocument.createTextNode(data));
417+
}
418+
}
419+
}
420+
373421
export class DocumentType extends Node<Document> {
374422
readonly name: string;
375423
readonly publicId: string;

‎packages/ui/wpt.config.ts‎

Lines changed: 16 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,
@@ -162,8 +164,17 @@ const config: WPTConfig = {
162164
'Attribute with prefix in local name',
163165
'getAttributeNames tests',
164166
],
165-
'dom/nodes/Node-nodeValue.html': ['Text.nodeValue', 'Comment.nodeValue'],
167+
'dom/nodes/Node-nodeName.html': [
168+
'For DocumentFragment nodes, nodeName should return "#document-fragment".',
169+
],
170+
'dom/nodes/Node-nodeValue.html': [
171+
'Text.nodeValue',
172+
'Comment.nodeValue',
173+
'DocumentFragment.nodeValue',
174+
],
166175
'dom/nodes/Node-textContent.html': [
176+
'For an empty DocumentFragment, textContent should be the empty string',
177+
/^DocumentFragment with(?:out)? /,
167178
'For an empty Text, textContent should be the empty string',
168179
'For an empty Comment, textContent should be the empty string',
169180
'For a Text with data, textContent should be that data',
@@ -178,6 +189,10 @@ const config: WPTConfig = {
178189
'When the parent is null, parentElement should be null',
179190
'When the parent is a document, parentElement should be null (element)',
180191
'When the parent is a document, parentElement should be null (comment)',
192+
'parentElement should return null for children of DocumentFragments (element)',
193+
'parentElement should return null for children of DocumentFragments (text)',
194+
'parentElement should work correctly with DocumentFragments (element)',
195+
'parentElement should work correctly with DocumentFragments (text)',
181196
'parentElement should work correctly in disconnected subtrees (element)',
182197
'parentElement should work correctly in disconnected subtrees (text)',
183198
],

0 commit comments

Comments
 (0)