Skip to content

Commit db70c94

Browse files
authored
Support live named access in WPT Window fixtures
1 parent 870ad06 commit db70c94

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎packages/ui/wpt.config.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,27 @@ const config: WPTConfig = {
184184
'Calling insertBefore with a reference child whose parent is not the context node must throw a NotFoundError.',
185185
'Inserting a node before itself should not move the node',
186186
],
187+
'html/browsers/the-window-object/named-access-on-the-window-object/basics.html': [
188+
'A named property must return an element as-is if its name attribute matches',
189+
'A named property must not be set if the element is not reachable from the Document',
190+
'A named property must not be enumerable',
191+
'A named property must return any element for which the id attribute matches',
192+
'A non element node in the document should not cause errors',
193+
// Multiple matches require live HTMLCollection support.
194+
],
195+
'html/browsers/the-window-object/named-access-on-the-window-object/changing.html': true,
196+
'html/browsers/the-window-object/named-access-on-the-window-object/removing.html': true,
197+
'html/browsers/the-window-object/named-access-on-the-window-object/existing-prop.html': true,
198+
'html/browsers/the-window-object/named-access-on-the-window-object/name-attribute-elements.html':
199+
[
200+
'Only elements with specific local names may be returned if their name attribute matches (negative test)',
201+
'Removing an element for which the name attribute is not tracked, must not cause errors upon removing it',
202+
// The positive case requires a live HTMLCollection.
203+
],
204+
'html/browsers/the-window-object/named-access-on-the-window-object/id-non-html-elements.html': [
205+
'Parsed svg and math elements with an id are exposed',
206+
// Other cases require append() or live HTMLCollection support.
207+
],
187208
},
188209
exclude: { '**': true },
189210
};

‎packages/web-platform-tests/README.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ Window event delegation currently requires extensible event objects.
129129
Unsupported source features fail explicitly. Selecting part of a file does not
130130
claim coverage of its remaining subtests or complete standards conformance.
131131

132+
Window named access resolves a single element by its nonempty `id`, or an HTML
133+
`embed`, `form`, `img`, or `object` by its nonempty `name`. Lookup follows the
134+
associated document's current tree, so insertion, removal, and attribute changes
135+
take effect immediately. Existing globals and inherited properties take priority;
136+
assignments shadow named access, and names are not enumerated. The worker installs
137+
this lookup in its global prototype chain and restores that chain during cleanup.
138+
Multiple matching elements and child browsing-context names fail explicitly:
139+
live `HTMLCollection` results and iframe windows are not implemented. An iframe's
140+
`id` can still expose the element itself. Window prototype descriptors and the
141+
full named-properties object are outside this fixture's supported scope.
142+
132143
Parsed `<template>` and `<frameset>` fixtures are currently rejected. This blocks
133144
`shadow-dom/event-composed-path.html`, selected by the React, Solid, and Svelte
134145
profiles, even for its case without shadow roots. These profiles describe the
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const HTML_NS = 'http://www.w3.org/1999/xhtml';
2+
const namedElements = new Set(['embed', 'form', 'img', 'object']);
3+
4+
class UnsupportedNamedAccessError extends Error {}
5+
6+
/** Resolve fixture names through the worker global without changing its realm. */
7+
export function installNamedProperties(scope: object, document: Document): () => void {
8+
const prototype = Object.getPrototypeOf(scope);
9+
const namedProperties = Object.create(prototype) as object;
10+
Object.setPrototypeOf(
11+
scope,
12+
new Proxy(namedProperties, {
13+
get(target, key, receiver) {
14+
return Reflect.has(target, key)
15+
? Reflect.get(target, key, receiver)
16+
: namedElement(document, key);
17+
},
18+
has(target, key) {
19+
return Reflect.has(target, key) || namedElement(document, key) !== undefined;
20+
},
21+
}),
22+
);
23+
// Names are never own properties, so enumeration omits them and assignments
24+
// create ordinary globals that shadow subsequent named lookups.
25+
return () => Object.setPrototypeOf(scope, prototype);
26+
}
27+
28+
function namedElement(document: Document, key: PropertyKey): Element | undefined {
29+
if (typeof key !== 'string' || key === '') {
30+
return undefined;
31+
}
32+
let match: Element | undefined;
33+
const pending: Node[] = Array.from(document.childNodes);
34+
while (pending.length > 0) {
35+
const node = pending.pop()!;
36+
pending.push(...node.childNodes);
37+
if (node.nodeType !== 1) {
38+
continue;
39+
}
40+
const element = node as Element;
41+
const isHTML = element.namespaceURI === HTML_NS;
42+
const nameMatches = isHTML && element.getAttributeNS(null, 'name') === key;
43+
if (nameMatches && (element.localName === 'iframe' || element.localName === 'frame')) {
44+
throw new UnsupportedNamedAccessError(
45+
`Window named access to child browsing contexts is not supported: ${JSON.stringify(key)}`,
46+
);
47+
}
48+
if (
49+
element.getAttributeNS(null, 'id') === key ||
50+
(nameMatches && namedElements.has(element.localName))
51+
) {
52+
if (match) {
53+
throw new UnsupportedNamedAccessError(
54+
`Window named access with multiple matching elements is not supported: ${JSON.stringify(key)}`,
55+
);
56+
}
57+
match = element;
58+
}
59+
}
60+
return match;
61+
}

‎packages/web-platform-tests/src/node/run.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Script } from 'node:vm';
66
import type { WPTConfig } from '../config.ts';
77
import { selectsTest } from '../vitest/selection.ts';
88
import { populateDocument } from './document.ts';
9+
import { installNamedProperties } from './named-properties.ts';
910
import { loadSource, type SourceScript } from './source.ts';
1011
import { Window } from './window.ts';
1112

@@ -59,6 +60,7 @@ export async function runWPTFile({
5960
const timers = new Set<ReturnType<typeof setTimeout>>();
6061
const completion = Promise.withResolvers<WPTFileResult>();
6162
let watchdog: ReturnType<typeof setTimeout> | undefined;
63+
let restoreNamedProperties: (() => void) | undefined;
6264
let finished = false;
6365
let failure: WPTHarnessStatus | undefined;
6466
let harnessStatus: WPTHarnessStatus | undefined;
@@ -213,6 +215,7 @@ export async function runWPTFile({
213215
scope.META_TITLE = source.title;
214216
}
215217
populateDocument(document, source.html);
218+
restoreNamedProperties = installNamedProperties(scope, document);
216219
watchdog = setTimeout(
217220
() => {
218221
// Upstream retains any earlier harness error when timing out pending tests.
@@ -259,6 +262,7 @@ export async function runWPTFile({
259262
}
260263
process.off('uncaughtException', onError);
261264
process.off('unhandledRejection', onRejection);
265+
restoreNamedProperties?.();
262266
}
263267
}
264268

0 commit comments

Comments
 (0)