Skip to content

Commit e3a96f0

Browse files
committed
Bug 1925468 - Extend and improve modify-attributes-in-callback.html. r=smaug
- Use cleanup functions to guarantee the state of the DOM tree before each test. - Make more explicit why the length of the div's attribute list is expected to be 2. - Add 2 tests for https://g-issues.chromium.org/issues/333739948 using setAttributeNS instead of setAttribute. Differential Revision: https://phabricator.services.mozilla.com/D228776
1 parent daa3d11 commit e3a96f0

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

testing/web-platform/tests/trusted-types/modify-attributes-in-callback.html

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
// This is a regression test for https://g-issues.chromium.org/issues/333739948
1313
// The test should hold true for any browser that supports Trusted Types.
1414

15-
let target = "data-x";
15+
let iframeAttributeRemovedInCallback = null;
1616
trustedTypes.createPolicy("default", {
1717
createHTML: (s, _, sink) => {
18-
assert_equals(sink, 'HTMLIFrameElement srcdoc');
19-
iframe.removeAttribute(target);
18+
if (iframeAttributeRemovedInCallback) {
19+
assert_equals(sink, 'HTMLIFrameElement srcdoc');
20+
iframe.removeAttribute(iframeAttributeRemovedInCallback);
21+
}
2022
return s;
2123
},
2224
createScript: (s) => {
@@ -29,8 +31,16 @@
2931
}
3032
});
3133

34+
function cleanUpIFrameTest() {
35+
iframeAttributeRemovedInCallback = null;
36+
iframe.setAttribute("srcdoc", "content");
37+
iframe.setAttribute("data-x", "");
38+
}
39+
3240
test(t => {
3341
// Original bug report: Delete an attribute *before* the current one.
42+
t.add_cleanup(cleanUpIFrameTest);
43+
iframeAttributeRemovedInCallback = "data-x";
3444
assert_equals(iframe.srcdoc, "content");
3545
assert_equals(iframe.getAttribute("onmouseover"), "");
3646
iframe.setAttribute("srcdoc", "alert(1)");
@@ -40,31 +50,55 @@
4050

4151
test(t => {
4252
// Second case: Delete the exact attribute. It still gets set.
43-
target = "srcdoc";
44-
assert_equals(iframe.srcdoc, "alert(1)");
53+
t.add_cleanup(cleanUpIFrameTest);
54+
iframeAttributeRemovedInCallback = "srcdoc";
55+
assert_equals(iframe.srcdoc, "content");
4556
iframe.setAttribute("srcdoc", "new srcdoc value");
4657
assert_equals(iframe.srcdoc, "new srcdoc value");
4758
}, "Ensure the deleted attributes is modified.");
4859

4960
test(t => {
61+
t.add_cleanup(cleanUpIFrameTest);
62+
iframeAttributeRemovedInCallback = "data-x";
63+
assert_equals(iframe.srcdoc, "content");
64+
assert_equals(iframe.getAttribute("onmouseover"), "");
65+
iframe.setAttributeNS(null, "srcdoc", "alert(1)");
66+
assert_equals(iframe.srcdoc, "alert(1)");
67+
assert_equals(iframe.getAttribute("onmouseover"), "");
68+
}, "Ensure the right attributes are modified (setAttributeNS).");
69+
70+
test(t => {
71+
t.add_cleanup(cleanUpIFrameTest);
72+
iframeAttributeRemovedInCallback = "srcdoc";
73+
assert_equals(iframe.srcdoc, "content");
74+
iframe.setAttributeNS(null, "srcdoc", "new srcdoc value");
75+
assert_equals(iframe.srcdoc, "new srcdoc value");
76+
}, "Ensure the deleted attributes is modified (setAttributeNS).");
77+
78+
function cleanUpDivTest() {
79+
div.removeAttribute('onmouseover');
80+
}
81+
const expectedAttributeCount = 2; // id and onmouseover.
82+
83+
test(t => {
84+
t.add_cleanup(cleanUpDivTest);
5085
div.toggleAttribute('onmouseover');
51-
assert_equals(div.attributes.length, 2);
86+
assert_equals(div.attributes.length, expectedAttributeCount);
5287
assert_equals(div.attributes.onmouseover.value, '');
53-
div.removeAttribute('onmouseover');
5488
}, "Ensure toggleAttribute results in an empty attribute.");
5589

5690
test(t => {
91+
t.add_cleanup(cleanUpDivTest);
5792
div.setAttribute('onmouseover', 'foo');
58-
assert_equals(div.attributes.length, 2);
93+
assert_equals(div.attributes.length, expectedAttributeCount);
5994
assert_equals(div.attributes.onmouseover.value, 'foo');
60-
div.removeAttribute('onmouseover');
6195
}, "Ensure setAttribute results in right attribute value.");
6296

6397
test(t => {
98+
t.add_cleanup(cleanUpDivTest);
6499
div.setAttributeNS(null, 'onmouseover', 'foo');
65-
assert_equals(div.attributes.length, 2);
100+
assert_equals(div.attributes.length, expectedAttributeCount);
66101
assert_equals(div.attributes.onmouseover.value, 'foo');
67-
div.removeAttribute('onmouseover');
68102
}, "Ensure setAttributeNS results in right attribute value.");
69103

70104
</script>

0 commit comments

Comments
 (0)