Skip to content
Closed
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
21 changes: 21 additions & 0 deletions packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,27 @@ const config: WPTConfig = {
'Calling insertBefore with a reference child whose parent is not the context node must throw a NotFoundError.',
'Inserting a node before itself should not move the node',
],
'html/browsers/the-window-object/named-access-on-the-window-object/basics.html': [
'A named property must return an element as-is if its name attribute matches',
'A named property must not be set if the element is not reachable from the Document',
'A named property must not be enumerable',
'A named property must return any element for which the id attribute matches',
'A non element node in the document should not cause errors',
// Multiple matches require live HTMLCollection support.
],
'html/browsers/the-window-object/named-access-on-the-window-object/changing.html': true,
'html/browsers/the-window-object/named-access-on-the-window-object/removing.html': true,
'html/browsers/the-window-object/named-access-on-the-window-object/existing-prop.html': true,
'html/browsers/the-window-object/named-access-on-the-window-object/name-attribute-elements.html':
[
'Only elements with specific local names may be returned if their name attribute matches (negative test)',
'Removing an element for which the name attribute is not tracked, must not cause errors upon removing it',
// The positive case requires a live HTMLCollection.
],
'html/browsers/the-window-object/named-access-on-the-window-object/id-non-html-elements.html': [
'Parsed svg and math elements with an id are exposed',
// Other cases require append() or live HTMLCollection support.
],
},
exclude: { '**': true },
};
Expand Down
11 changes: 11 additions & 0 deletions packages/web-platform-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ Window event delegation currently requires extensible event objects.
Unsupported source features fail explicitly. Selecting part of a file does not
claim coverage of its remaining subtests or complete standards conformance.

Window named access resolves a single element by its nonempty `id`, or an HTML
`embed`, `form`, `img`, or `object` by its nonempty `name`. Lookup follows the
associated document's current tree, so insertion, removal, and attribute changes
take effect immediately. Existing globals and inherited properties take priority;
assignments shadow named access, and names are not enumerated. The worker installs
this lookup in its global prototype chain and restores that chain during cleanup.
Multiple matching elements and child browsing-context names fail explicitly:
live `HTMLCollection` results and iframe windows are not implemented. An iframe's
`id` can still expose the element itself. Window prototype descriptors and the
full named-properties object are outside this fixture's supported scope.

Parsed `<template>` and `<frameset>` fixtures are currently rejected. This blocks
`shadow-dom/event-composed-path.html`, selected by the React, Solid, and Svelte
profiles, even for its case without shadow roots. These profiles describe the
Expand Down
61 changes: 61 additions & 0 deletions packages/web-platform-tests/src/node/named-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const HTML_NS = 'http://www.w3.org/1999/xhtml';
const namedElements = new Set(['embed', 'form', 'img', 'object']);

class UnsupportedNamedAccessError extends Error {}

/** Resolve fixture names through the worker global without changing its realm. */
export function installNamedProperties(scope: object, document: Document): () => void {
const prototype = Object.getPrototypeOf(scope);
const namedProperties = Object.create(prototype) as object;
Object.setPrototypeOf(
scope,
new Proxy(namedProperties, {
get(target, key, receiver) {
return Reflect.has(target, key)
? Reflect.get(target, key, receiver)
: namedElement(document, key);
},
has(target, key) {
return Reflect.has(target, key) || namedElement(document, key) !== undefined;
},
}),
);
// Names are never own properties, so enumeration omits them and assignments
// create ordinary globals that shadow subsequent named lookups.
return () => Object.setPrototypeOf(scope, prototype);
}

function namedElement(document: Document, key: PropertyKey): Element | undefined {
if (typeof key !== 'string' || key === '') {
return undefined;
}
let match: Element | undefined;
const pending: Node[] = Array.from(document.childNodes);
while (pending.length > 0) {
const node = pending.pop()!;
pending.push(...node.childNodes);
if (node.nodeType !== 1) {
continue;
}
const element = node as Element;
const isHTML = element.namespaceURI === HTML_NS;
const nameMatches = isHTML && element.getAttributeNS(null, 'name') === key;
if (nameMatches && (element.localName === 'iframe' || element.localName === 'frame')) {
throw new UnsupportedNamedAccessError(
`Window named access to child browsing contexts is not supported: ${JSON.stringify(key)}`,
);
}
if (
element.getAttributeNS(null, 'id') === key ||
(nameMatches && namedElements.has(element.localName))
) {
if (match) {
throw new UnsupportedNamedAccessError(
`Window named access with multiple matching elements is not supported: ${JSON.stringify(key)}`,
);
}
match = element;
}
}
return match;
}
4 changes: 4 additions & 0 deletions packages/web-platform-tests/src/node/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Script } from 'node:vm';
import type { WPTConfig } from '../config.ts';
import { selectsTest } from '../vitest/selection.ts';
import { populateDocument } from './document.ts';
import { installNamedProperties } from './named-properties.ts';
import { loadSource, type SourceScript } from './source.ts';
import { Window } from './window.ts';

Expand Down Expand Up @@ -59,6 +60,7 @@ export async function runWPTFile({
const timers = new Set<ReturnType<typeof setTimeout>>();
const completion = Promise.withResolvers<WPTFileResult>();
let watchdog: ReturnType<typeof setTimeout> | undefined;
let restoreNamedProperties: (() => void) | undefined;
let finished = false;
let failure: WPTHarnessStatus | undefined;
let harnessStatus: WPTHarnessStatus | undefined;
Expand Down Expand Up @@ -213,6 +215,7 @@ export async function runWPTFile({
scope.META_TITLE = source.title;
}
populateDocument(document, source.html);
restoreNamedProperties = installNamedProperties(scope, document);
watchdog = setTimeout(
() => {
// Upstream retains any earlier harness error when timing out pending tests.
Expand Down Expand Up @@ -259,6 +262,7 @@ export async function runWPTFile({
}
process.off('uncaughtException', onError);
process.off('unhandledRejection', onRejection);
restoreNamedProperties?.();
}
}

Expand Down
Loading