diff --git a/packages/ui/src/css-select.ts b/packages/ui/src/css-select.ts index 28fdad8..18d2e38 100644 --- a/packages/ui/src/css-select.ts +++ b/packages/ui/src/css-select.ts @@ -1,5 +1,5 @@ import type { Options } from 'css-select'; -import { compile as compileSelector } from 'css-select'; +import { compile as compileSelector, selectAll, selectOne } from 'css-select'; import { Element, Node } from './dom.ts'; export type Adapter = NonNullable['adapter']>; @@ -71,3 +71,13 @@ export function compile(selector: string): (node: Node) => boolean { ); } } + +export function querySelector(nodes: Node[], selector: string): Element | null { + const query = compile(selector); + return selectOne(query, nodes, { adapter }); +} + +export function querySelectorAll(nodes: Node[], selector: string): Element[] { + const query = compile(selector); + return selectAll(query, nodes, { adapter }); +} diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index bdf5a06..928860b 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -1,5 +1,5 @@ /* oxlint-disable bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments. */ -import { compile } from './css-select.ts'; +import { compile, querySelector, querySelectorAll } from './css-select.ts'; import { asciiUppercase, asciiLowercase } from './ascii.ts'; import { NamedNodeMap } from './named-node-map.ts'; import { HTMLCollection } from './html-collection.ts'; @@ -9,6 +9,7 @@ import { validateAttributeName, validateAttributeNamespace, } from './validation.ts'; +import { NodeList } from './node-list.ts'; const HTML_NS = 'http://www.w3.org/1999/xhtml'; @@ -82,6 +83,14 @@ export class Document { return null; } + querySelector(selector: string): Element | null { + return querySelector([this.documentElement], selector); + } + + querySelectorAll(selector: string): NodeList { + return new NodeList(querySelectorAll([this.documentElement], selector)); + } + createElement(tagName: string): Element { if (arguments.length === 0) { throw new TypeError('createElement requires a tag name'); @@ -445,6 +454,14 @@ export class Element extends Node { return compile(selector)(this); } + querySelector(selector: string): Element | null { + return querySelector(Array.from(this.childNodes), selector); + } + + querySelectorAll(selector: string): NodeList { + return new NodeList(querySelectorAll(Array.from(this.childNodes), selector)); + } + get id(): string { return this.#attributes.getNamedItemNS(null, 'id')?.value ?? ''; } diff --git a/packages/ui/src/node-list.ts b/packages/ui/src/node-list.ts new file mode 100644 index 0000000..2faa053 --- /dev/null +++ b/packages/ui/src/node-list.ts @@ -0,0 +1,59 @@ +/* oxlint-disable bombshell-dev/no-generic-error -- Web IDL requires TypeError for missing arguments. */ +import type { Element } from './dom.ts'; + +/** + * A static snapshot of nodes, as returned by querySelectorAll. + * Full Web IDL indexed-property deletion and redefinition are not supported. + */ +export class NodeList implements Iterable { + readonly [index: number]: Element; + #elements: Element[]; + + constructor(nodes: Element[]) { + this.#elements = nodes; + for (const [index, node] of nodes.entries()) { + Object.defineProperty(this, index, { + value: node, + writable: false, + enumerable: true, + configurable: true, + }); + } + } + + get length(): number { + return this.#elements.length; + } + + item(index: number): Element | null { + if (arguments.length === 0) { + throw new TypeError('item requires an index'); + } + return this.#elements[index >>> 0] ?? null; + } + + keys(): ArrayIterator { + return this.#elements.keys(); + } + + values(): ArrayIterator { + return this.#elements.values(); + } + + entries(): ArrayIterator<[number, Element]> { + return this.#elements.entries(); + } + + forEach( + callback: (value: Element, index: number, list: NodeList) => void, + thisArg?: unknown, + ): void { + this.#elements.forEach((element, index) => { + callback.call(thisArg, element, index, this); + }); + } + + [Symbol.iterator](): ArrayIterator { + return this.#elements[Symbol.iterator](); + } +} diff --git a/packages/ui/test/wpt.config.ts b/packages/ui/test/wpt.config.ts index 38763a9..eb87c71 100644 --- a/packages/ui/test/wpt.config.ts +++ b/packages/ui/test/wpt.config.ts @@ -111,17 +111,74 @@ const config: WPTConfig = { 'WPT Element-matches-namespaced-elements.html: empty string namespace, matches', 'WPT Element-matches-namespaced-elements.html: has a namespace, matches', ], - 'dom/nodes/Element-matches-active-pseudo-class.html': [ - 'WPT Element-matches-active-pseudo-class.html: Regular div should not match :active by default', - 'WPT Element-matches-active-pseudo-class.html: Button should not match :active by default', - 'WPT Element-matches-active-pseudo-class.html: Link should not match :active by default', + 'dom/nodes/Element-matches-active-pseudo-class.html': true, + 'dom/nodes/Element-querySelectorAll-context-element.html': true, + 'dom/nodes/ParentNode-querySelector-All-comma-selector.html': true, + 'dom/nodes/ParentNode-querySelector-escapes.html': [ + 'WPT ParentNode-querySelector-escapes.html: "nonescaped" should match with "#nonescaped"', + 'WPT ParentNode-querySelector-escapes.html: "0nextIsWhiteSpace" should match with "#\\\\30 nextIsWhiteSpace"', + 'WPT ParentNode-querySelector-escapes.html: "0nextIsNotHexLetters" should match with "#\\\\30nextIsNotHexLetters"', + 'WPT ParentNode-querySelector-escapes.html: "0connectHexMoreThan6Hex" should match with "#\\\\000030connectHexMoreThan6Hex"', + 'WPT ParentNode-querySelector-escapes.html: "0spaceMoreThan6Hex" should match with "#\\\\000030 spaceMoreThan6Hex"', + 'WPT ParentNode-querySelector-escapes.html: "outOfRange0" should never match with "#outOfRange\\\\110030"', + 'WPT ParentNode-querySelector-escapes.html: ".comma" should match with "#\\\\.comma"', + 'WPT ParentNode-querySelector-escapes.html: "-minus" should match with "#\\\\-minus"', + 'WPT ParentNode-querySelector-escapes.html: "g" should match with "#\\\\g"', + 'WPT ParentNode-querySelector-escapes.html: "aBMPRegular" should match with "#\\\\61 BMPRegular"', + 'WPT ParentNode-querySelector-escapes.html: "\ud83d\udd11nonBMP" should match with "#\\\\1f511 nonBMP"', + 'WPT ParentNode-querySelector-escapes.html: "00continueEscapes" should match with "#\\\\30\\\\30 continueEscapes"', + 'WPT ParentNode-querySelector-escapes.html: "00continueEscapes" should match with "#\\\\30 \\\\30 continueEscapes"', + 'WPT ParentNode-querySelector-escapes.html: "continueEscapes00" should match with "#continueEscapes\\\\30 \\\\30 "', + 'WPT ParentNode-querySelector-escapes.html: "continueEscapes00" should match with "#continueEscapes\\\\30 \\\\30"', + 'WPT ParentNode-querySelector-escapes.html: "continueEscapes00" should match with "#continueEscapes\\\\30\\\\30 "', + 'WPT ParentNode-querySelector-escapes.html: "continueEscapes00" should match with "#continueEscapes\\\\30\\\\30"', + 'WPT ParentNode-querySelector-escapes.html: "hello" should match with "#hel\\\\6Co"', + 'WPT ParentNode-querySelector-escapes.html: "&B" should match with "#\\\\26 B"', + 'WPT ParentNode-querySelector-escapes.html: "spaces" should match with "#sp\\\\61\\tc\\\\65\\fs"', + 'WPT ParentNode-querySelector-escapes.html: "test\ud799" should match with "#test\\\\D799"', + 'WPT ParentNode-querySelector-escapes.html: "\ue000" should match with "#\\\\E000"', + 'WPT ParentNode-querySelector-escapes.html: "test" should match with "#te\\\\s\\\\t"', + 'WPT ParentNode-querySelector-escapes.html: "spaces in\\tident" should match with "#spaces\\\\ in\\\\\\tident"', + 'WPT ParentNode-querySelector-escapes.html: ".,:!" should match with "#\\\\.\\\\,\\\\:\\\\!"', + 'WPT ParentNode-querySelector-escapes.html: "\udbff\udfff" should match with "#\\\\10fFfF"', + 'WPT ParentNode-querySelector-escapes.html: "\udbff\udfff0" should match with "#\\\\10fFfF0"', + 'WPT ParentNode-querySelector-escapes.html: "\udbc0\udc0000" should match with "#\\\\10000000"', + 'WPT ParentNode-querySelector-escapes.html: "simple-ident" should match with "#simple-ident"', + 'WPT ParentNode-querySelector-escapes.html: "testing123" should match with "#testing123"', + 'WPT ParentNode-querySelector-escapes.html: "_underscore" should match with "#_underscore"', + 'WPT ParentNode-querySelector-escapes.html: "-text" should match with "#-text"', + 'WPT ParentNode-querySelector-escapes.html: "-m" should match with "#-\\\\6d"', + 'WPT ParentNode-querySelector-escapes.html: "--abc" should match with "#--abc"', + 'WPT ParentNode-querySelector-escapes.html: "--" should match with "#--"', + 'WPT ParentNode-querySelector-escapes.html: "--11" should match with "#--11"', + 'WPT ParentNode-querySelector-escapes.html: "---" should match with "#---"', + 'WPT ParentNode-querySelector-escapes.html: "\u2003" should match with "#\u2003"', + 'WPT ParentNode-querySelector-escapes.html: "\u1234" should match with "#\u1234"', + 'WPT ParentNode-querySelector-escapes.html: "\ud808\udf45" should match with "#\ud808\udf45"', + // Remaining cases expose escape parsing and invalid code point handling gaps. ], - 'dom/nodes/ParentNode-querySelector-invalid-comment-like.html': [ - 'WPT ParentNode-querySelector-invalid-comment-like.html: matches throws SyntaxError for comment-like selector', + 'dom/nodes/ParentNode-querySelector-invalid-comment-like.html': true, + 'dom/nodes/ParentNode-querySelector-nested.html': true, + 'dom/nodes/ParentNode-querySelector-orphan-scope.html': true, + 'dom/nodes/ParentNode-querySelector-tag-case.html': [ + 'WPT ParentNode-querySelector-tag-case.html: Tag name case-insensitivity with child combinator (>) dynamically created', + ], + 'dom/nodes/ParentNode-querySelectorAll-not-descendant.html': true, + 'dom/nodes/NodeList-Iterable.html': [ + 'NodeList has length method.', + 'NodeList has values method.', + 'NodeList has entries method.', + 'NodeList has forEach method.', + 'NodeList has Symbol.iterator.', + 'NodeList is iterable via for-of loop.', + 'NodeList responds to Object.keys correctly', + // The remaining case exercises live childNodes rather than querySelectorAll results. ], 'css/selectors/child-indexed-pseudo-class.html': [ - // Defer the four detached :nth-*(n) cases until https://github.com/fb55/css-select/issues/1945 is fixed. + // Defer :nth-*(n) cases for detached elements and the document root until + // https://github.com/fb55/css-select/issues/1945 is fixed. /^Expected DIV element to (?!.*\(n\)).* with matches$/, + /^Expected HTML element to (?!.*\(n\)).* with matches, querySelector\(\), and querySelectorAll\(\)$/, ], 'dom/nodes/Element-setAttribute.html': true, 'dom/nodes/Element-setAttributeNodeNS.html': true, diff --git a/packages/web-platform-tests/src/shared/registry.ts b/packages/web-platform-tests/src/shared/registry.ts index 2bc27ba..1b4de48 100644 --- a/packages/web-platform-tests/src/shared/registry.ts +++ b/packages/web-platform-tests/src/shared/registry.ts @@ -144,6 +144,7 @@ import { register as register_dom_nodes_Node_parentNode_html } from '../wpt/dom/ import { register as register_dom_nodes_Node_removeChild_html } from '../wpt/dom/nodes/Node-removeChild.html.ts'; import { register as register_dom_nodes_Node_replaceChild_html } from '../wpt/dom/nodes/Node-replaceChild.html.ts'; import { register as register_dom_nodes_Node_textContent_html } from '../wpt/dom/nodes/Node-textContent.html.ts'; +import { register as register_dom_nodes_NodeList_Iterable_html } from '../wpt/dom/nodes/NodeList-Iterable.html.ts'; import { register as register_dom_nodes_ParentNode_append_html } from '../wpt/dom/nodes/ParentNode-append.html.ts'; import { register as register_dom_nodes_ParentNode_children_html } from '../wpt/dom/nodes/ParentNode-children.html.ts'; import { register as register_dom_nodes_ParentNode_prepend_html } from '../wpt/dom/nodes/ParentNode-prepend.html.ts'; @@ -591,6 +592,7 @@ export const registry = { 'dom/nodes/Node-removeChild.html': { register: register_dom_nodes_Node_removeChild_html }, 'dom/nodes/Node-replaceChild.html': { register: register_dom_nodes_Node_replaceChild_html }, 'dom/nodes/Node-textContent.html': { register: register_dom_nodes_Node_textContent_html }, + 'dom/nodes/NodeList-Iterable.html': { register: register_dom_nodes_NodeList_Iterable_html }, 'dom/nodes/ParentNode-append.html': { register: register_dom_nodes_ParentNode_append_html }, 'dom/nodes/ParentNode-children.html': { register: register_dom_nodes_ParentNode_children_html }, 'dom/nodes/ParentNode-prepend.html': { register: register_dom_nodes_ParentNode_prepend_html }, diff --git a/packages/web-platform-tests/src/wpt/dom/nodes/Element-querySelectorAll-context-element.html.ts b/packages/web-platform-tests/src/wpt/dom/nodes/Element-querySelectorAll-context-element.html.ts index ab019c5..fd937db 100644 --- a/packages/web-platform-tests/src/wpt/dom/nodes/Element-querySelectorAll-context-element.html.ts +++ b/packages/web-platform-tests/src/wpt/dom/nodes/Element-querySelectorAll-context-element.html.ts @@ -1,34 +1,40 @@ -import type { CompatibilityContext, DOMFixture } from '../../../shared/context.ts'; +import type { CompatibilityContext } from '../../../shared/context.ts'; export function register(context: CompatibilityContext): void { - function title(source: string, name: string): string { - return `WPT ${source}: ${name}`; - } - function mount({ document, connect }: DOMFixture, markup: string): HTMLElement { - const container = document.createElement('main'); - container.innerHTML = markup; - connect(container); - return container; - } - const { expect } = context.runner; + const content = ` + +element.querySelectorAll() with the context element in the selector + + + +
+
+ + + + +
+
+
`; for (const [name, selector, fromDocument] of [ ['Query from document', '.root table tbody tr', true], ['Query without root class', 'table tbody tr', false], ['Query with root class', '.root table tbody tr', false], ] as const) { - context.test(title('Element-querySelectorAll-context-element.html', name), (fixture) => { - const { document } = fixture; - const container = mount( - fixture, - '
', - ); - const root = container.firstElementChild!.firstElementChild!; - const target = root.firstElementChild!.firstElementChild!.firstElementChild!; - expect(Array.from((fromDocument ? document : root).querySelectorAll(selector))).toEqual([ - target, - ]); - }); + context.test( + `WPT Element-querySelectorAll-context-element.html: ${name}`, + ({ document }) => { + const root = document.getElementById('root')!; + const target = document.getElementById('row')!; + const result = (fromDocument ? document : root).querySelectorAll(selector); + // Upstream assert_array_equals checks own indices and node identity. + expect(result.length).toBe(1); + expect(Object.hasOwn(result, 0)).toBe(true); + expect(result[0]).toBe(target); + }, + { document: { contentType: 'text/html', content } }, + ); } } diff --git a/packages/web-platform-tests/src/wpt/dom/nodes/NodeList-Iterable.html.ts b/packages/web-platform-tests/src/wpt/dom/nodes/NodeList-Iterable.html.ts new file mode 100644 index 0000000..1aef731 --- /dev/null +++ b/packages/web-platform-tests/src/wpt/dom/nodes/NodeList-Iterable.html.ts @@ -0,0 +1,95 @@ +import type { CompatibilityContext } from '../../../shared/context.ts'; + +export function register(context: CompatibilityContext): void { + const { expect } = context.runner; + const content = ` + +NodeList Iterable Test +

+

+

+

+

+ +
123
`; + + context.test( + 'NodeList has length method.', + ({ document }) => { + const paragraphs = document.querySelectorAll('p'); + expect('length' in paragraphs).toBe(true); + }, + { document: { contentType: 'text/html', content } }, + ); + context.test( + 'NodeList has values method.', + ({ document }) => { + const paragraphs = document.querySelectorAll('p'); + expect('values' in paragraphs).toBe(true); + }, + { document: { contentType: 'text/html', content } }, + ); + context.test( + 'NodeList has entries method.', + ({ document }) => { + const paragraphs = document.querySelectorAll('p'); + expect('entries' in paragraphs).toBe(true); + }, + { document: { contentType: 'text/html', content } }, + ); + context.test( + 'NodeList has forEach method.', + ({ document }) => { + const paragraphs = document.querySelectorAll('p'); + expect('forEach' in paragraphs).toBe(true); + }, + { document: { contentType: 'text/html', content } }, + ); + context.test( + 'NodeList has Symbol.iterator.', + ({ document }) => { + const paragraphs = document.querySelectorAll('p'); + expect(Symbol.iterator in paragraphs).toBe(true); + }, + { document: { contentType: 'text/html', content } }, + ); + context.test( + 'NodeList is iterable via for-of loop.', + ({ document }) => { + const paragraphs = document.querySelectorAll('p'); + const ids = '12345'; + let idx = 0; + for (const node of paragraphs) { + expect(node.getAttribute('id')).toBe(ids[idx++]); + } + }, + { document: { contentType: 'text/html', content } }, + ); + context.test( + 'NodeList responds to Object.keys correctly', + ({ document }) => { + const paragraphs = document.querySelectorAll('p'); + expect(Object.keys(paragraphs)).toEqual(['0', '1', '2', '3', '4']); + }, + { document: { contentType: 'text/html', content } }, + ); + context.test( + 'live NodeLists are for-of iterable and update appropriately', + ({ document }) => { + const container = document.getElementById('live')!; + const nodeList = container.childNodes as NodeListOf; + const ids = []; + for (const el of nodeList) { + ids.push(el.id); + expect(el.localName).toBe('b'); + if (ids.length < 3) { + const newEl = document.createElement('b'); + newEl.id = 'after' + el.id; + container.appendChild(newEl); + } + } + expect(ids).toEqual(['b1', 'b2', 'b3', 'afterb1', 'afterb2']); + }, + { document: { contentType: 'text/html', content } }, + ); +} diff --git a/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-All-comma-selector.html.ts b/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-All-comma-selector.html.ts index 70892f0..56e98bd 100644 --- a/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-All-comma-selector.html.ts +++ b/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-All-comma-selector.html.ts @@ -1,31 +1,25 @@ -import type { CompatibilityContext, DOMFixture } from '../../../shared/context.ts'; +import type { CompatibilityContext } from '../../../shared/context.ts'; export function register(context: CompatibilityContext): void { - function title(source: string, name: string): string { - return `WPT ${source}: ${name}`; - } - function mount({ document, connect }: DOMFixture, markup: string): HTMLElement { - const container = document.createElement('main'); - container.innerHTML = markup; - connect(container); - return container; - } - const { expect } = context.runner; + const content = ` +Comma selector + + + + +

Hello world

+

Goodbye, world

`; context.test( - title('ParentNode-querySelector-All-comma-selector.html', 'Basic comma-separated selector'), - (fixture) => { - const { document } = fixture; - mount(fixture, '

Hello world

Goodbye, world

'); + 'WPT ParentNode-querySelector-All-comma-selector.html: Basic comma-separated selector', + ({ document }) => { expect(document.querySelectorAll('h1,h2').length).toBe(2); }, + { document: { contentType: 'text/html', content } }, ); context.test( - title( - 'ParentNode-querySelector-All-comma-selector.html', - 'Repeated identical querySelectorAll calls with repeated selectors should work on empty elements', - ), + 'WPT ParentNode-querySelector-All-comma-selector.html: Repeated identical querySelectorAll calls with repeated selectors should work on empty elements', ({ document }) => { const el = document.createElement('div'); const selector = '.foo, .foo, .bar'; @@ -34,5 +28,6 @@ export function register(context: CompatibilityContext): void { const results2 = el.querySelectorAll(selector); expect(results2.length).toBe(0); }, + { document: { contentType: 'text/html', content } }, ); } diff --git a/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-nested.html.ts b/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-nested.html.ts index 99a9559..665a96b 100644 --- a/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-nested.html.ts +++ b/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-nested.html.ts @@ -1,46 +1,31 @@ -import type { CompatibilityContext, DOMFixture } from '../../../shared/context.ts'; +import type { CompatibilityContext } from '../../../shared/context.ts'; export function register(context: CompatibilityContext): void { - function prepare({ document, connect }: DOMFixture, markup: string): Document { - const root = document.createElement('div'); - root.innerHTML = markup; - connect(root); - return document; - } - const { expect } = context.runner; + const content = ` +querySelector on multi-level query selecting + + + +
+ Actions + +
`; context.test( 'WPT ParentNode-querySelector-nested.html: querySelector on multi-level query selecting', - (fixture) => { - const document = prepare( - fixture, - '\n\n
\n Actions\n \n
', - ); - + ({ document }) => { const target = document.getElementById('span2')!; const anchor = document.querySelector('a')!; const span = anchor.querySelector('span')!; expect(span).toBe(target); }, + { document: { contentType: 'text/html', content } }, ); - - // 4.3.7 from https://drafts.csswg.org/css-syntax/#consume-escaped-code-point - - // - escape hex digit - - // - hex digit special replacement - // 1. zero points - - // 2. surrogate points - - // 3. out of range points - - // - escape EOF - - // - escape anythong else - - // non edge cases - - // ident tests case from CSS tests of chromium source: https://goo.gl/3Cxdov } diff --git a/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-orphan-scope.html.ts b/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-orphan-scope.html.ts index 7bdb501..f254d87 100644 --- a/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-orphan-scope.html.ts +++ b/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-orphan-scope.html.ts @@ -21,12 +21,12 @@ export function register(context: CompatibilityContext): void { const child = document.createElement('span'); child.id = 'buz'; parent.appendChild(child); - if (self) + if (self) { expect( parent.querySelectorAll('#fuz').length, 'should not return the base element itself', ).toBe(0); - else { + } else { const results = parent.querySelectorAll('#fuz #buz'); expect(results.length).toBe(1); expect(results[0]).toBe(child); diff --git a/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-tag-case.html.ts b/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-tag-case.html.ts index f724bd1..47d1142 100644 --- a/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-tag-case.html.ts +++ b/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelector-tag-case.html.ts @@ -71,10 +71,18 @@ export function register(context: CompatibilityContext): void { li = document.createElement('li'); ul.appendChild(li); div.appendChild(ul); - expect(Array.from(div.querySelectorAll('ul'))).toEqual([ul]); - expect(Array.from(div.querySelectorAll('UL'))).toEqual([ul]); - expect(Array.from(div.querySelectorAll('ul > li'))).toEqual([li]); - expect(Array.from(div.querySelectorAll('UL > LI'))).toEqual([li]); + for (const [selector, expected, message] of [ + ['ul', ul, 'Lowercase ul'], + ['UL', ul, 'Uppercase UL'], + ['ul > li', li, 'Lowercase ul > li'], + ['UL > LI', li, 'Uppercase UL > LI'], + ] as const) { + const result = div.querySelectorAll(selector); + // Upstream assert_array_equals checks own indices and node identity. + expect(result.length, message).toBe(1); + expect(Object.hasOwn(result, 0), message).toBe(true); + expect(result[0], message).toBe(expected); + } }, ); context.test( diff --git a/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelectorAll-not-descendant.html.ts b/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelectorAll-not-descendant.html.ts index acdc540..98fdf1e 100644 --- a/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelectorAll-not-descendant.html.ts +++ b/packages/web-platform-tests/src/wpt/dom/nodes/ParentNode-querySelectorAll-not-descendant.html.ts @@ -1,31 +1,30 @@ -import type { CompatibilityContext, DOMFixture } from '../../../shared/context.ts'; +import type { CompatibilityContext } from '../../../shared/context.ts'; export function register(context: CompatibilityContext): void { - function prepare({ document, connect }: DOMFixture, markup: string): Document { - const root = document.createElement('div'); - root.innerHTML = markup; - connect(root); - return document; - } - const { expect } = context.runner; - function arrayEquals( - actual: ArrayLike, - options: { expected: ArrayLike; message?: string }, - ): void { - expect(actual.length, options.message).toBe(options.expected.length); - for (let i = 0; i < actual.length; i++) - expect(actual[i], options.message).toBe(options.expected[i]); - } + const content = ` + +querySelectorAll with :not() pseudo-class and descendant selector + + +
+

+ + + + + + +
+

+
+

+

+
`; context.test( 'WPT ParentNode-querySelectorAll-not-descendant.html: querySelectorAll with :not() pseudo-class and descendant selector', - (fixture) => { - const document = prepare( - fixture, - '\n\n
\n

\n \n \n \n \n \n \n
\n

\n
\n

\n

\n
', - ); - + ({ document }) => { const container = document.getElementById('container')!; const result = container.querySelectorAll('p:not(table p)'); @@ -34,26 +33,13 @@ export function register(context: CompatibilityContext): void { // and the closing

tag creates a *new* empty

element after the table. // Thus, the query should match p#p1, the empty p after the table, and p#p3. const resultIDs = Array.from(result, (el) => el.id); - arrayEquals(resultIDs, { expected: ['p1', '', 'p3'] }); + const expected = ['p1', '', 'p3']; + expect(resultIDs.length).toBe(expected.length); + for (let index = 0; index < expected.length; index++) { + expect(Object.hasOwn(resultIDs, index)).toBe(Object.hasOwn(expected, index)); + expect(resultIDs[index]).toBe(expected[index]); + } }, + { document: { contentType: 'text/html', content } }, ); - - // 4.3.7 from https://drafts.csswg.org/css-syntax/#consume-escaped-code-point - - // - escape hex digit - - // - hex digit special replacement - // 1. zero points - - // 2. surrogate points - - // 3. out of range points - - // - escape EOF - - // - escape anythong else - - // non edge cases - - // ident tests case from CSS tests of chromium source: https://goo.gl/3Cxdov }