-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
trusted-types/set-event-handlers-content-attributes.tentative.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.