Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for setAttribute/setAttributeNS and content event handler attributes #50025

Merged
merged 3 commits into from
Jan 13, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,19 @@
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/WebIDLParser.js"></script>
<script src="support/namespaces.js"></script>
<script>
promise_setup(async function() {
let attributeNamesWithInterfaceName = [];
function addOnAttributes(IDL, interfaceName) {
// Parsing the whole IDL file is slow, so use a small regexp to extract only
// the part that is relevant for this test.
let regexp = new RegExp(`^.*\(partial \)?interface \(mixin \)?${interfaceName}[^{]*{[^{}]*};$`, "m");
let parsedIDL = WebIDL2.parse(IDL.match(regexp)[0]);
parsedIDL.find(idl => idl.name === interfaceName)
.members.map(member => member.name)
.filter(name => name.length >= 3 && name.startsWith("on") &&
!name.startsWith("onwebkit"))
.forEach(name => attributeNamesWithInterfaceName.push({name, interfaceName}));
}

const htmlIDL = await (await fetch("/interfaces/html.idl")).text();
// GlobalEventHandlers exist on HTMLElement, SVGElement, and MathMLElement.
// WindowEventHandlers exist on HTMLBodyElement, and HTMLFrameSetElement.
["GlobalEventHandlers", "WindowEventHandlers"].forEach(interfaceName => {
addOnAttributes(htmlIDL, interfaceName);
});

const encryptedMediaIDL = await (await fetch("/interfaces/encrypted-media.idl")).text();
// HTMLMediaElement (the parent for <audio> and <video>) has extra event handlers.
addOnAttributes(encryptedMediaIDL, "HTMLMediaElement");

const svgAnimationsIDL = await (await fetch("/interfaces/svg-animations.idl")).text();
// SVGAnimationElement has extra event handlers.
addOnAttributes(svgAnimationsIDL, "SVGAnimationElement");

for (const attributeNameWithInterfaceName of attributeNamesWithInterfaceName) {
// Skip attributes that are not implemented in the browser.
if (attributeNameWithInterfaceName.interfaceName === 'GlobalEventHandlers' && !(attributeNameWithInterfaceName.name in HTMLElement.prototype)) {
continue;
}
if (attributeNameWithInterfaceName.interfaceName === 'WindowEventHandlers' && !(attributeNameWithInterfaceName.name in HTMLBodyElement.prototype)) {
continue;
}
if (attributeNameWithInterfaceName.interfaceName === 'HTMLMediaElement' && !(attributeNameWithInterfaceName.name in HTMLMediaElement.prototype)) {
continue;
<script type="module">
import { getEventHandlerAttributeWithInterfaceNames } from './support/event-handler-attributes.mjs';
promise_setup(async function() {
for (const attr of await getEventHandlerAttributeWithInterfaceNames()) {
promise_test(async () => {
NSURI_ARRAY.forEach(attrNs => {
assert_equals(trustedTypes.getAttributeType(
"dummy", attr.name, "dummyNs", attrNs),
attrNs === NSURI_EMPTY ? "TrustedScript" : null,
`for attrNs='${attrNs}'`);
});
}, `getAttributeType("dummy", "${attr.name}", "dummyNs", attrNs)`);
}
if (attributeNameWithInterfaceName.interfaceName === 'SVGAnimationElement' && !(attributeNameWithInterfaceName.name in SVGAnimationElement.prototype)) {
continue;
}

promise_test(async () => {
NSURI_ARRAY.forEach(attrNs => {
assert_equals(trustedTypes.getAttributeType(
"dummy", attributeNameWithInterfaceName.name, "dummyNs", attrNs),
attrNs === NSURI_EMPTY ? "TrustedScript" : null,
`for attrNs='${attrNs}'`);
});
}, `getAttributeType("dummy", "${attributeNameWithInterfaceName.name}", "dummyNs", attrNs)`);
}
});
});
</script>
67 changes: 67 additions & 0 deletions trusted-types/set-event-handlers-content-attributes.tentative.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/namespaces.js"></script>
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'">
</head>
<body>
<script type="module">
import { getEventHandlerAttributeWithInterfaceNames } from './support/event-handler-attributes.mjs';

promise_setup(async function() {
const input_string = "unsafe_handler()";
const output_string = "safe_handler()";
let seenSinkName;
function resetSeenSinkName() { seenSinkName = undefined; }
trustedTypes.createPolicy("default", {
createScript: (input, trustedTypeName, sinkName) => {
assert_equals(input, input_string);
assert_equals(trustedTypeName, "TrustedScript");
assert_equals(seenSinkName, undefined);
seenSinkName = sinkName;
return output_string;
}
});
function elementsImplementingInterface(interfaceName) {
switch (interfaceName) {
case 'GlobalEventHandlers':
return [document.createElement("div"),
document.createElementNS(NSURI_SVG, "g"),
document.createElementNS(NSURI_MATHML, "mrow")];
case 'WindowEventHandlers':
return [document.createElement("body"),
document.createElement("frameset")];
case 'HTMLMediaElement':
return [document.createElement("audio"),
document.createElement("video")];
case 'SVGAnimationElement':
return [document.createElementNS(NSURI_SVG, "animation")];
default:
throw "Unknown interface";
}
}

for (const attr of await getEventHandlerAttributeWithInterfaceNames()) {
elementsImplementingInterface(attr.interfaceName).forEach(element => {
function cleanup() {
resetSeenSinkName();
element.removeAttribute(attr.name);
}
promise_test(async t => {
t.add_cleanup(cleanup);
element.setAttribute(attr.name, input_string);
assert_equals(seenSinkName, `Element ${attr.name}`);
assert_equals(element.getAttribute(attr.name),output_string);
}, `${element.tagName}.setAttribute(${attr.name}, "${input_string}") calls default policy`);
promise_test(async t => {
t.add_cleanup(cleanup);
element.setAttributeNS(null, attr.name, input_string);
assert_equals(seenSinkName, `Element ${attr.name}`);
assert_equals(element.getAttribute(attr.name),output_string);
}, `${element.tagName}.setAttributeNS(${attr.name}, "${input_string}") calls default policy`);
});
}
});
</script>
</body>
47 changes: 47 additions & 0 deletions trusted-types/support/event-handler-attributes.mjs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs an mjs extension? Isn't that a node paradigm rather than a browser one?

If it's used elsewhere in WPT then no worries.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no strong opinion on mjs, this is used by many other web platform tests though, so should be fine. IIRC, this was an MDN example too.

About implementing as a module: the rationale was to import WebIDLParser.js from event-handler-attributes.mjs directly, rather than having to do that from all HTML files.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import '/resources/WebIDLParser.js';

export async function getEventHandlerAttributeWithInterfaceNames() {
let attributeNamesWithInterfaceName = [];
function isAttributeImplemented(interfaceName, name) {
switch (interfaceName) {
case 'GlobalEventHandlers':
return name in HTMLElement.prototype;
case 'WindowEventHandlers':
return name in HTMLBodyElement.prototype;
case 'HTMLMediaElement':
return name in HTMLMediaElement.prototype;
case 'SVGAnimationElement':
return name in SVGAnimationElement.prototype;
default:
throw "Unknown interface";
}
}
function addOnAttributes(IDL, interfaceName) {
// Parsing the whole IDL file is slow, so use a small regexp to extract only
// the part that is relevant for this test.
let regexp = new RegExp(`^.*\(partial \)?interface \(mixin \)?${interfaceName}[^{]*{[^{}]*};$`, "m");
let parsedIDL = WebIDL2.parse(IDL.match(regexp)[0]);
parsedIDL.find(idl => idl.name === interfaceName)
.members.map(member => member.name)
.filter(name => name.length >= 3 && name.startsWith("on") &&
!name.startsWith("onwebkit") &&
isAttributeImplemented(interfaceName, name))
.forEach(name => attributeNamesWithInterfaceName.push({name, interfaceName}));
}
const htmlIDL = await (await fetch("/interfaces/html.idl")).text();
// GlobalEventHandlers exist on HTMLElement, SVGElement, and MathMLElement.
// WindowEventHandlers exist on HTMLBodyElement, and HTMLFrameSetElement.
["GlobalEventHandlers", "WindowEventHandlers"].forEach(interfaceName => {
addOnAttributes(htmlIDL, interfaceName);
});

const encryptedMediaIDL = await (await fetch("/interfaces/encrypted-media.idl")).text();
// HTMLMediaElement (the parent for <audio> and <video>) has extra event handlers.
addOnAttributes(encryptedMediaIDL, "HTMLMediaElement");

const svgAnimationsIDL = await (await fetch("/interfaces/svg-animations.idl")).text();
// SVGAnimationElement has extra event handlers.
addOnAttributes(svgAnimationsIDL, "SVGAnimationElement");

return attributeNamesWithInterfaceName;
}