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-child-node-remove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': minor
---

Add node removal and processing-instruction nodes, and enable upstream removal tests for elements, text, comments, and processing instructions.
26 changes: 26 additions & 0 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ export class Document {
createComment(data: string): Text {
return new Comment(this, String(data), this.notify);
}

createProcessingInstruction(target: string, data: string): ProcessingInstruction {
return new ProcessingInstruction(this, target, data, this.notify);
}
}

export abstract class Node {
static readonly ELEMENT_NODE = 1;
static readonly ATTRIBUTE_NODE = 2;
static readonly TEXT_NODE = 3;
static readonly PROCESSING_INSTRUCTION_NODE = 7;
static readonly COMMENT_NODE = 8;

readonly #childNodes: Node[] = [];
Expand Down Expand Up @@ -109,6 +114,10 @@ export abstract class Node {
return node;
}

remove(): void {
this.parentNode?.removeChild(this);
}

insertBefore(node: Node, anchor: Node | null): Node {
// oxlint-disable-next-line typescript/no-this-alias -- This cursor walks the insertion parent's ancestry.
for (let current: Node | null = this; current; current = current.parentNode) {
Expand Down Expand Up @@ -237,3 +246,20 @@ export class Comment extends CharacterData {
return '#comment';
}
}

export class ProcessingInstruction extends CharacterData {
readonly target: string;

constructor(owner: Document, target: string, data: string, notify: Node['notify']) {
super(owner, data, notify);
this.target = target;
}

get nodeType(): number {
return Node.PROCESSING_INSTRUCTION_NODE;
}

get nodeName(): string {
return this.target;
}
}
2 changes: 2 additions & 0 deletions packages/ui/test/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export default {
'dom/nodes/Document-createElement.html': true,
'dom/nodes/Document-createComment.html': true,
'dom/nodes/CharacterData-data.html': true,
'dom/nodes/CharacterData-remove.html': true,
'dom/nodes/Element-remove.html': true,
'dom/nodes/Node-nodeValue.html': ['Text.nodeValue', 'Comment.nodeValue'],
'dom/nodes/Node-textContent.html': [
'For an empty Text, textContent should be the empty string',
Expand Down
4 changes: 4 additions & 0 deletions packages/web-platform-tests/src/shared/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import { register as register_dom_nodes_CharacterData_appendData_html } from '..
import { register as register_dom_nodes_CharacterData_data_html } from '../wpt/dom/nodes/CharacterData-data.html.ts';
import { register as register_dom_nodes_CharacterData_deleteData_html } from '../wpt/dom/nodes/CharacterData-deleteData.html.ts';
import { register as register_dom_nodes_CharacterData_insertData_html } from '../wpt/dom/nodes/CharacterData-insertData.html.ts';
import { register as register_dom_nodes_CharacterData_remove_html } from '../wpt/dom/nodes/CharacterData-remove.html.ts';
import { register as register_dom_nodes_CharacterData_replaceData_html } from '../wpt/dom/nodes/CharacterData-replaceData.html.ts';
import { register as register_dom_nodes_CharacterData_substringData_html } from '../wpt/dom/nodes/CharacterData-substringData.html.ts';
import { register as register_dom_nodes_DOMImplementation_createDocument_html } from '../wpt/dom/nodes/DOMImplementation-createDocument.html.ts';
Expand All @@ -102,6 +103,7 @@ import { register as register_dom_nodes_Element_matches_disabled_pseudo_class_ht
import { register as register_dom_nodes_Element_matches_namespaced_elements_html } from '../wpt/dom/nodes/Element-matches-namespaced-elements.html.ts';
import { register as register_dom_nodes_Element_matches_html } from '../wpt/dom/nodes/Element-matches.html.ts';
import { register as register_dom_nodes_Element_querySelectorAll_context_element_html } from '../wpt/dom/nodes/Element-querySelectorAll-context-element.html.ts';
import { register as register_dom_nodes_Element_remove_html } from '../wpt/dom/nodes/Element-remove.html.ts';
import { register as register_dom_nodes_Element_setAttribute_html } from '../wpt/dom/nodes/Element-setAttribute.html.ts';
import { register as register_dom_nodes_Element_setAttributeNodeNS_html } from '../wpt/dom/nodes/Element-setAttributeNodeNS.html.ts';
import { register as register_dom_nodes_MutationObserver_attributes_html } from '../wpt/dom/nodes/MutationObserver-attributes.html.ts';
Expand Down Expand Up @@ -510,6 +512,7 @@ export const registry = {
'dom/nodes/CharacterData-data.html': { register: register_dom_nodes_CharacterData_data_html },
'dom/nodes/CharacterData-deleteData.html': { register: register_dom_nodes_CharacterData_deleteData_html },
'dom/nodes/CharacterData-insertData.html': { register: register_dom_nodes_CharacterData_insertData_html },
'dom/nodes/CharacterData-remove.html': { register: register_dom_nodes_CharacterData_remove_html },
'dom/nodes/CharacterData-replaceData.html': { register: register_dom_nodes_CharacterData_replaceData_html },
'dom/nodes/CharacterData-substringData.html': { register: register_dom_nodes_CharacterData_substringData_html },
'dom/nodes/DOMImplementation-createDocument.html': { register: register_dom_nodes_DOMImplementation_createDocument_html },
Expand All @@ -533,6 +536,7 @@ export const registry = {
'dom/nodes/Element-matches-namespaced-elements.html': { register: register_dom_nodes_Element_matches_namespaced_elements_html },
'dom/nodes/Element-matches.html': { register: register_dom_nodes_Element_matches_html },
'dom/nodes/Element-querySelectorAll-context-element.html': { register: register_dom_nodes_Element_querySelectorAll_context_element_html },
'dom/nodes/Element-remove.html': { register: register_dom_nodes_Element_remove_html },
'dom/nodes/Element-setAttribute.html': { register: register_dom_nodes_Element_setAttribute_html },
'dom/nodes/Element-setAttributeNodeNS.html': { register: register_dom_nodes_Element_setAttributeNodeNS_html },
'dom/nodes/MutationObserver-attributes.html': { register: register_dom_nodes_MutationObserver_attributes_html },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { CompatibilityContext } from '../../../shared/context.ts';
import { testRemove } from './ChildNode-remove.js.ts';

export function register(context: CompatibilityContext): void {
for (const type of ['text', 'comment', 'PI'] as const) {
testRemove(
context,
(document) => {
const text = document.createTextNode('text');
const text_parent = document.createElement('div');
const comment = document.createComment('comment');
const comment_parent = document.createElement('div');
const pi = document.createProcessingInstruction('foo', 'bar');
const pi_parent = document.createElement('div');
return {
text: { node: text, parent: text_parent },
comment: { node: comment, parent: comment_parent },
PI: { node: pi, parent: pi_parent },
}[type];
},
type,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { CompatibilityContext } from '../../../shared/context.ts';

// Each compatibility case recreates the upstream setup in its own document.
export function testRemove(
context: CompatibilityContext,
setup: (document: Document) => { node: Element | CharacterData; parent: Element },
type: string,
): void {
const { expect } = context.runner;

context.test(type + ' should support remove()', ({ document }) => {
const { node } = setup(document);
expect('remove' in node).toBe(true);
expect(typeof node.remove).toBe('function');
expect(node.remove.length).toBe(0);
});
context.test('remove() should work if ' + type + " doesn't have a parent", ({ document }) => {
const { node } = setup(document);
expect(node.parentNode, 'Node should not have a parent').toBe(null);
expect(node.remove()).toBe(undefined);
expect(node.parentNode, 'Removed new node should not have a parent').toBe(null);
});
context.test('remove() should work if ' + type + ' does have a parent', ({ document }) => {
const { node, parent } = setup(document);
expect(node.parentNode, 'Node should not have a parent').toBe(null);
parent.appendChild(node);
expect(node.parentNode, 'Appended node should have a parent').toBe(parent);
expect(node.remove()).toBe(undefined);
expect(node.parentNode, 'Removed node should not have a parent').toBe(null);
expect(parent.childNodes, 'Parent should not have children').toHaveLength(0);
});
context.test(
'remove() should work if ' + type + ' does have a parent and siblings',
({ document }) => {
const { node, parent } = setup(document);
expect(node.parentNode, 'Node should not have a parent').toBe(null);
const before = parent.appendChild(document.createComment('before'));
parent.appendChild(node);
const after = parent.appendChild(document.createComment('after'));
expect(node.parentNode, 'Appended node should have a parent').toBe(parent);
expect(node.remove()).toBe(undefined);
expect(node.parentNode, 'Removed node should not have a parent').toBe(null);
expect(parent.childNodes, 'Parent should have two children left').toHaveLength(2);
expect(parent.childNodes[0], 'Parent should have two children left').toBe(before);
expect(parent.childNodes[1], 'Parent should have two children left').toBe(after);
},
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { CompatibilityContext } from '../../../shared/context.ts';
import { testRemove } from './ChildNode-remove.js.ts';

export function register(context: CompatibilityContext): void {
testRemove(
context,
(document) => ({
node: document.createElement('div'),
parent: document.createElement('div'),
}),
'element',
);
}
Loading