Skip to content

Commit 3b07424

Browse files
committed
Update test assertions when default policy moves an attribute during Attr.value like setters
The draft spec PR now early returns in the case the element of the attribute changes rather than updating the value. See whatwg/dom#1268
1 parent d51574b commit 3b07424

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

trusted-types/set-attributes-mutations-in-callback.tentative.html

+11-7
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
if (window.applyMutation) {
2020
assert_equals(input, input_string);
2121
window.applyMutation();
22+
return output_string;
2223
}
23-
return output_string;
24+
return input;
2425
}
2526
trustedTypes.createPolicy("default", {
2627
createHTML: createTrustedType,
@@ -165,9 +166,10 @@
165166
testData.attrName),
166167
output_string);
167168
assert_equals(otherElement.attributes.length, 1);
169+
// The attribute is moved to the other element before its value is updated.
168170
assert_equals(otherElement.getAttributeNS(testData.attrNS,
169171
testData.attrName),
170-
output_string);
172+
other_string);
171173
break;
172174
case "Element.setAttributeNode":
173175
case "Element.setAttributeNodeNS":
@@ -187,21 +189,23 @@
187189
assert_equals(otherElement.attributes.length, 1);
188190
assert_equals(otherElement.getAttributeNS(testData.attrNS,
189191
testData.attrName),
190-
output_string);
192+
input_string);
191193
break;
192194
case "Attr.value":
193195
case "Node.nodeValue":
194196
case "Node.textContent":
195-
// These APIs successfully sets the attribute node value, the default
196-
// policy's callback moved that node on the other element.
197+
// These APIs early return when the element changes, but the default policy's
198+
// callback still sets the attribute on the other element.
197199
returnValue = setterData.runSetter(element, testData.attrNS,
198200
testData.attrName,
199-
input_string, testData.type);
201+
input_string, testData.type, other_string);
202+
200203
assert_equals(element.attributes.length, 0);
201204
assert_equals(otherElement.attributes.length, 1);
205+
// The attribute is moved to the other element, with its initial value of an empty string.
202206
assert_equals(otherElement.getAttributeNS(testData.attrNS,
203207
testData.attrName),
204-
output_string);
208+
other_string);
205209
break;
206210
default:
207211
assert_unreached();

trusted-types/support/attributes.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ const attributeSetterData = [
284284
{
285285
api:"Attr.value",
286286
acceptNS: true,
287-
acceptTrustedTypeArgumentInIDL: false,
288-
runSetter: function(element, attrNS, attrName, attrValue, type) {
289-
element.setAttributeNS(attrNS, attrName, createTrustedOutput(type, ""));
287+
acceptTrustedTypeArgumentInIDL: false,
288+
runSetter: function(element, attrNS, attrName, attrValue, type, initialAttrValue = "") {
289+
element.setAttributeNS(attrNS, attrName, createTrustedOutput(type, initialAttrValue));
290290
this.lastAttributeNode = findAttribute(element, attrNS, attrName);
291291
assert_true(!!this.lastAttributeNode);
292292
return (this.lastAttributeNode.value = attrValue);
@@ -296,8 +296,8 @@ const attributeSetterData = [
296296
api: "Node.nodeValue",
297297
acceptNS: true,
298298
acceptTrustedTypeArgumentInIDL: false,
299-
runSetter: function(element, attrNS, attrName, attrValue, type) {
300-
element.setAttributeNS(attrNS, attrName, createTrustedOutput(type, ""));
299+
runSetter: function(element, attrNS, attrName, attrValue, type, initialAttrValue = "") {
300+
element.setAttributeNS(attrNS, attrName, createTrustedOutput(type, initialAttrValue));
301301
this.lastAttributeNode = findAttribute(element, attrNS, attrName);
302302
assert_true(!!this.lastAttributeNode);
303303
return (this.lastAttributeNode.nodeValue = attrValue);
@@ -307,8 +307,8 @@ const attributeSetterData = [
307307
api: "Node.textContent",
308308
acceptNS: true,
309309
acceptTrustedTypeArgumentInIDL: false,
310-
runSetter: function(element, attrNS, attrName, attrValue, type) {
311-
element.setAttributeNS(attrNS, attrName, createTrustedOutput(type, ""));
310+
runSetter: function(element, attrNS, attrName, attrValue, type, initialAttrValue = "") {
311+
element.setAttributeNS(attrNS, attrName, createTrustedOutput(type, initialAttrValue));
312312
this.lastAttributeNode = findAttribute(element, attrNS, attrName);
313313
assert_true(!!this.lastAttributeNode);
314314
return (this.lastAttributeNode.textContent = attrValue);

0 commit comments

Comments
 (0)