Skip to content

Commit bd069ea

Browse files
authored
Support node removal (#62)
## What does this PR do? Preact requires `Node.remove()` Add `remove()` to detach nodes through the existing `removeChild()` implementation, with a no-op for detached nodes. Unfortunately, to use the WPT test for CharacterData, it requires a processing-instruction node because that's what the test cases use. I opted to implement it (it's tiny) rather than fork the WPT test or use a one-off test. ## Type of change - [ ] Bug fix - [x] Feature - [ ] Refactor (no behavior change) - [ ] Documentation - [ ] Performance improvement - [ ] Tests - [ ] Chore (dependencies, CI, tooling) ## Checklist - [x] All tests pass (`pnpm test`) - [x] Files are formatted (`pnpm format`) - [x] I have added/updated tests for my changes (if applicable) - [x] I have added a changeset ## AI-generated code disclosure - [x] This PR includes AI-generated code
1 parent 131a39c commit bd069ea

7 files changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/ui': minor
3+
---
4+
5+
Add node removal and processing-instruction nodes, and enable upstream removal tests for elements, text, comments, and processing instructions.

‎packages/ui/src/dom.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ export class Document {
3838
createComment(data: string): Text {
3939
return new Comment(this, String(data), this.notify);
4040
}
41+
42+
createProcessingInstruction(target: string, data: string): ProcessingInstruction {
43+
return new ProcessingInstruction(this, target, data, this.notify);
44+
}
4145
}
4246

4347
export abstract class Node {
4448
static readonly ELEMENT_NODE = 1;
4549
static readonly ATTRIBUTE_NODE = 2;
4650
static readonly TEXT_NODE = 3;
51+
static readonly PROCESSING_INSTRUCTION_NODE = 7;
4752
static readonly COMMENT_NODE = 8;
4853

4954
readonly #childNodes: Node[] = [];
@@ -109,6 +114,10 @@ export abstract class Node {
109114
return node;
110115
}
111116

117+
remove(): void {
118+
this.parentNode?.removeChild(this);
119+
}
120+
112121
insertBefore(node: Node, anchor: Node | null): Node {
113122
// oxlint-disable-next-line typescript/no-this-alias -- This cursor walks the insertion parent's ancestry.
114123
for (let current: Node | null = this; current; current = current.parentNode) {
@@ -237,3 +246,20 @@ export class Comment extends CharacterData {
237246
return '#comment';
238247
}
239248
}
249+
250+
export class ProcessingInstruction extends CharacterData {
251+
readonly target: string;
252+
253+
constructor(owner: Document, target: string, data: string, notify: Node['notify']) {
254+
super(owner, data, notify);
255+
this.target = target;
256+
}
257+
258+
get nodeType(): number {
259+
return Node.PROCESSING_INSTRUCTION_NODE;
260+
}
261+
262+
get nodeName(): string {
263+
return this.target;
264+
}
265+
}

‎packages/ui/test/wpt.config.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export default {
1616
'dom/nodes/Document-createElement.html': true,
1717
'dom/nodes/Document-createComment.html': true,
1818
'dom/nodes/CharacterData-data.html': true,
19+
'dom/nodes/CharacterData-remove.html': true,
20+
'dom/nodes/Element-remove.html': true,
1921
'dom/nodes/Node-nodeValue.html': ['Text.nodeValue', 'Comment.nodeValue'],
2022
'dom/nodes/Node-textContent.html': [
2123
'For an empty Text, textContent should be the empty string',

‎packages/web-platform-tests/src/shared/registry.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import { register as register_dom_nodes_CharacterData_appendData_html } from '..
7979
import { register as register_dom_nodes_CharacterData_data_html } from '../wpt/dom/nodes/CharacterData-data.html.ts';
8080
import { register as register_dom_nodes_CharacterData_deleteData_html } from '../wpt/dom/nodes/CharacterData-deleteData.html.ts';
8181
import { register as register_dom_nodes_CharacterData_insertData_html } from '../wpt/dom/nodes/CharacterData-insertData.html.ts';
82+
import { register as register_dom_nodes_CharacterData_remove_html } from '../wpt/dom/nodes/CharacterData-remove.html.ts';
8283
import { register as register_dom_nodes_CharacterData_replaceData_html } from '../wpt/dom/nodes/CharacterData-replaceData.html.ts';
8384
import { register as register_dom_nodes_CharacterData_substringData_html } from '../wpt/dom/nodes/CharacterData-substringData.html.ts';
8485
import { register as register_dom_nodes_DOMImplementation_createDocument_html } from '../wpt/dom/nodes/DOMImplementation-createDocument.html.ts';
@@ -102,6 +103,7 @@ import { register as register_dom_nodes_Element_matches_disabled_pseudo_class_ht
102103
import { register as register_dom_nodes_Element_matches_namespaced_elements_html } from '../wpt/dom/nodes/Element-matches-namespaced-elements.html.ts';
103104
import { register as register_dom_nodes_Element_matches_html } from '../wpt/dom/nodes/Element-matches.html.ts';
104105
import { register as register_dom_nodes_Element_querySelectorAll_context_element_html } from '../wpt/dom/nodes/Element-querySelectorAll-context-element.html.ts';
106+
import { register as register_dom_nodes_Element_remove_html } from '../wpt/dom/nodes/Element-remove.html.ts';
105107
import { register as register_dom_nodes_Element_setAttribute_html } from '../wpt/dom/nodes/Element-setAttribute.html.ts';
106108
import { register as register_dom_nodes_Element_setAttributeNodeNS_html } from '../wpt/dom/nodes/Element-setAttributeNodeNS.html.ts';
107109
import { register as register_dom_nodes_MutationObserver_attributes_html } from '../wpt/dom/nodes/MutationObserver-attributes.html.ts';
@@ -510,6 +512,7 @@ export const registry = {
510512
'dom/nodes/CharacterData-data.html': { register: register_dom_nodes_CharacterData_data_html },
511513
'dom/nodes/CharacterData-deleteData.html': { register: register_dom_nodes_CharacterData_deleteData_html },
512514
'dom/nodes/CharacterData-insertData.html': { register: register_dom_nodes_CharacterData_insertData_html },
515+
'dom/nodes/CharacterData-remove.html': { register: register_dom_nodes_CharacterData_remove_html },
513516
'dom/nodes/CharacterData-replaceData.html': { register: register_dom_nodes_CharacterData_replaceData_html },
514517
'dom/nodes/CharacterData-substringData.html': { register: register_dom_nodes_CharacterData_substringData_html },
515518
'dom/nodes/DOMImplementation-createDocument.html': { register: register_dom_nodes_DOMImplementation_createDocument_html },
@@ -533,6 +536,7 @@ export const registry = {
533536
'dom/nodes/Element-matches-namespaced-elements.html': { register: register_dom_nodes_Element_matches_namespaced_elements_html },
534537
'dom/nodes/Element-matches.html': { register: register_dom_nodes_Element_matches_html },
535538
'dom/nodes/Element-querySelectorAll-context-element.html': { register: register_dom_nodes_Element_querySelectorAll_context_element_html },
539+
'dom/nodes/Element-remove.html': { register: register_dom_nodes_Element_remove_html },
536540
'dom/nodes/Element-setAttribute.html': { register: register_dom_nodes_Element_setAttribute_html },
537541
'dom/nodes/Element-setAttributeNodeNS.html': { register: register_dom_nodes_Element_setAttributeNodeNS_html },
538542
'dom/nodes/MutationObserver-attributes.html': { register: register_dom_nodes_MutationObserver_attributes_html },
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { CompatibilityContext } from '../../../shared/context.ts';
2+
import { testRemove } from './ChildNode-remove.js.ts';
3+
4+
export function register(context: CompatibilityContext): void {
5+
for (const type of ['text', 'comment', 'PI'] as const) {
6+
testRemove(
7+
context,
8+
(document) => {
9+
const text = document.createTextNode('text');
10+
const text_parent = document.createElement('div');
11+
const comment = document.createComment('comment');
12+
const comment_parent = document.createElement('div');
13+
const pi = document.createProcessingInstruction('foo', 'bar');
14+
const pi_parent = document.createElement('div');
15+
return {
16+
text: { node: text, parent: text_parent },
17+
comment: { node: comment, parent: comment_parent },
18+
PI: { node: pi, parent: pi_parent },
19+
}[type];
20+
},
21+
type,
22+
);
23+
}
24+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import type { CompatibilityContext } from '../../../shared/context.ts';
2+
3+
// Each compatibility case recreates the upstream setup in its own document.
4+
export function testRemove(
5+
context: CompatibilityContext,
6+
setup: (document: Document) => { node: Element | CharacterData; parent: Element },
7+
type: string,
8+
): void {
9+
const { expect } = context.runner;
10+
11+
context.test(type + ' should support remove()', ({ document }) => {
12+
const { node } = setup(document);
13+
expect('remove' in node).toBe(true);
14+
expect(typeof node.remove).toBe('function');
15+
expect(node.remove.length).toBe(0);
16+
});
17+
context.test('remove() should work if ' + type + " doesn't have a parent", ({ document }) => {
18+
const { node } = setup(document);
19+
expect(node.parentNode, 'Node should not have a parent').toBe(null);
20+
expect(node.remove()).toBe(undefined);
21+
expect(node.parentNode, 'Removed new node should not have a parent').toBe(null);
22+
});
23+
context.test('remove() should work if ' + type + ' does have a parent', ({ document }) => {
24+
const { node, parent } = setup(document);
25+
expect(node.parentNode, 'Node should not have a parent').toBe(null);
26+
parent.appendChild(node);
27+
expect(node.parentNode, 'Appended node should have a parent').toBe(parent);
28+
expect(node.remove()).toBe(undefined);
29+
expect(node.parentNode, 'Removed node should not have a parent').toBe(null);
30+
expect(parent.childNodes, 'Parent should not have children').toHaveLength(0);
31+
});
32+
context.test(
33+
'remove() should work if ' + type + ' does have a parent and siblings',
34+
({ document }) => {
35+
const { node, parent } = setup(document);
36+
expect(node.parentNode, 'Node should not have a parent').toBe(null);
37+
const before = parent.appendChild(document.createComment('before'));
38+
parent.appendChild(node);
39+
const after = parent.appendChild(document.createComment('after'));
40+
expect(node.parentNode, 'Appended node should have a parent').toBe(parent);
41+
expect(node.remove()).toBe(undefined);
42+
expect(node.parentNode, 'Removed node should not have a parent').toBe(null);
43+
expect(parent.childNodes, 'Parent should have two children left').toHaveLength(2);
44+
expect(parent.childNodes[0], 'Parent should have two children left').toBe(before);
45+
expect(parent.childNodes[1], 'Parent should have two children left').toBe(after);
46+
},
47+
);
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { CompatibilityContext } from '../../../shared/context.ts';
2+
import { testRemove } from './ChildNode-remove.js.ts';
3+
4+
export function register(context: CompatibilityContext): void {
5+
testRemove(
6+
context,
7+
(document) => ({
8+
node: document.createElement('div'),
9+
parent: document.createElement('div'),
10+
}),
11+
'element',
12+
);
13+
}

0 commit comments

Comments
 (0)