commit 34deed7f5d981404d24f3fcdd370c2443534e508
parent e659ad9ff884de5a6ba4148f922a13bbe18a65a0
Author: Luke Warlow <lwarlow@igalia.com>
Date: Fri, 31 Oct 2025 09:05:01 +0000
Bug 1997314 [wpt PR 55762] - Update trusted types attribute tests for latest changes to DOM PR, a=testonly
Automatic update from web-platform-tests
Update trusted types attribute tests for latest changes to DOM PR (#55762)
* Update trusted types attribute tests for latest changes to DOM PR
This fixes an existing issue where tests would pass even if the browser was wrong.
Also adds new tests to ensure some exceptions happen in the right order.
--
wpt-commits: fcbd42ef9838ccae6469e807b9bdc47a2fb5f2ff
wpt-pr: 55762
Diffstat:
3 files changed, 88 insertions(+), 6 deletions(-)
diff --git a/testing/web-platform/tests/trusted-types/set-attributes-mutations-in-callback.tentative.html b/testing/web-platform/tests/trusted-types/set-attributes-mutations-in-callback.tentative.html
@@ -10,12 +10,16 @@
const input_string = "unsafe_input";
const output_string = "safe_output";
const other_string = "other_value";
- let applyMutation, finalChecks;
+ let applyMutation;
+ let treatDefaultPolicyAsNoOp = false;
function testCleanup() {
applyMutation = undefined;
- finalChecks = undefined;
+ treatDefaultPolicyAsNoOp = false;
}
function createTrustedType(input) {
+ if (treatDefaultPolicyAsNoOp)
+ return input;
+
if (window.applyMutation) {
assert_equals(input, input_string);
window.applyMutation();
@@ -124,9 +128,11 @@
let element = testData.element();
let attributeNodeInCallback;
window.applyMutation = function() {
+ treatDefaultPolicyAsNoOp = true;
element.setAttributeNS(testData.attrNS, testData.attrName,
createTrustedOutput(testData.type,
other_string));
+ treatDefaultPolicyAsNoOp = false;
attributeNodeInCallback = findAttribute(element, testData.attrNS,
testData.attrName);
};
@@ -149,7 +155,9 @@
if (ownerElement) {
ownerElement.removeAttributeNode(setterData.lastAttributeNode);
}
+ treatDefaultPolicyAsNoOp = true;
otherElement.setAttributeNode(setterData.lastAttributeNode);
+ treatDefaultPolicyAsNoOp = false;
};
let returnValue;
switch (setterData.api) {
@@ -167,7 +175,7 @@
assert_equals(otherElement.attributes.length, 1);
assert_equals(otherElement.getAttributeNS(testData.attrNS,
testData.attrName),
- output_string);
+ other_string);
break;
case "Element.setAttributeNode":
case "Element.setAttributeNodeNS":
@@ -187,7 +195,7 @@
assert_equals(otherElement.attributes.length, 1);
assert_equals(otherElement.getAttributeNS(testData.attrNS,
testData.attrName),
- output_string);
+ input_string);
break;
case "Attr.value":
case "Node.nodeValue":
diff --git a/testing/web-platform/tests/trusted-types/set-attributes-require-trusted-types-no-default-policy-error-cases.html b/testing/web-platform/tests/trusted-types/set-attributes-require-trusted-types-no-default-policy-error-cases.html
@@ -0,0 +1,74 @@
+<!DOCTYPE html>
+<script src="/resources/testharness.js" ></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="support/passthroughpolicy.js"></script>
+<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script';">
+<script>
+ test(() => {
+ const element = document.createElement("div");
+ const otherElement = document.createElement("div");
+ element.setAttribute("onclick", passthroughpolicy.createScript("foo"));
+ assert_throws_js(TypeError, () => {
+ otherElement.setAttributeNode(element.attributes[0]);
+ });
+ }, "setAttributeNode runs Trusted Types check before inuse checks");
+
+ test(() => {
+ const element = document.createElement("div");
+ const otherElement = document.createElement("div");
+ element.setAttribute("onclick", passthroughpolicy.createScript("foo"));
+ assert_throws_js(TypeError, () => {
+ otherElement.setAttributeNodeNS(element.attributes[0]);
+ });
+ }, "setAttributeNodeNS runs Trusted Types check before inuse checks");
+
+ test(() => {
+ const element = document.createElement("div");
+ const otherElement = document.createElement("div");
+ element.setAttribute("onclick", passthroughpolicy.createScript("foo"));
+ assert_throws_js(TypeError, () => {
+ otherElement.attributes.setNamedItem(element.attributes[0]);
+ });
+ }, "setNamedItem runs Trusted Types check before inuse checks");
+
+ test(() => {
+ const element = document.createElement("div");
+ const otherElement = document.createElement("div");
+ element.setAttribute("onclick", passthroughpolicy.createScript("foo"));
+ assert_throws_js(TypeError, () => {
+ otherElement.attributes.setNamedItemNS(element.attributes[0]);
+ });
+ }, "setNamedItemNS runs Trusted Types check before inuse checks");
+
+ test(() => {
+ const element = document.createElement("div");
+ element.setAttribute("onclick", passthroughpolicy.createScript("foo"));
+ assert_throws_js(TypeError, () => {
+ element.setAttributeNode(element.attributes[0]);
+ });
+ }, "setAttributeNode runs Trusted Types check before same attribute early return");
+
+ test(() => {
+ const element = document.createElement("div");
+ element.setAttribute("onclick", passthroughpolicy.createScript("foo"));
+ assert_throws_js(TypeError, () => {
+ element.setAttributeNodeNS(element.attributes[0]);
+ });
+ }, "setAttributeNodeNS runs Trusted Types check before same attribute early return");
+
+ test(() => {
+ const element = document.createElement("div");
+ element.setAttribute("onclick", passthroughpolicy.createScript("foo"));
+ assert_throws_js(TypeError, () => {
+ element.attributes.setNamedItem(element.attributes[0]);
+ });
+ }, "setNamedItem runs Trusted Types check before same attribute early return");
+
+ test(() => {
+ const element = document.createElement("div");
+ element.setAttribute("onclick", passthroughpolicy.createScript("foo"));
+ assert_throws_js(TypeError, () => {
+ element.attributes.setNamedItemNS(element.attributes[0]);
+ });
+ }, "setNamedItemNS runs Trusted Types check before same attribute early return");
+</script>
diff --git a/testing/web-platform/tests/trusted-types/support/attributes.js b/testing/web-platform/tests/trusted-types/support/attributes.js
@@ -284,8 +284,8 @@ const attributeSetterData = [
{
api:"Attr.value",
acceptNS: true,
- acceptTrustedTypeArgumentInIDL: false,
- runSetter: function(element, attrNS, attrName, attrValue, type) {
+ acceptTrustedTypeArgumentInIDL: false,
+ runSetter: function(element, attrNS, attrName, attrValue, type) {
element.setAttributeNS(attrNS, attrName, createTrustedOutput(type, ""));
this.lastAttributeNode = findAttribute(element, attrNS, attrName);
assert_true(!!this.lastAttributeNode);