Skip to content

Commit 78efeed

Browse files
committed
fixup! Core:Manipulation: Add basic TrustedHTML support
1 parent e3165e6 commit 78efeed

File tree

2 files changed

+50
-34
lines changed

2 files changed

+50
-34
lines changed

test/data/trusted-html.html

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,8 @@
99
<script src="../../dist/jquery.min.js"></script>
1010
<script src="iframeTest.js"></script>
1111
<script>
12-
if ( typeof trustedTypes === "undefined" ) {
13-
startIframeTest( [ {
14-
actual: "",
15-
expected: "trustedTypes support",
16-
message: "trustedTypes supported"
17-
} ] );
18-
throw new Error( "No trustedTypes support; this test should be skipped" );
19-
}
20-
21-
var i, input, elem, tags,
12+
var i, input, elem, tags, policy,
2213
results = [],
23-
policy = trustedTypes.createPolicy( "jquery-test-policy", {
24-
createHTML: function( html ) {
25-
return html;
26-
}
27-
} ),
2814
inputs = [
2915
[ "<div></div>", "<div class='test'></div>", [ "div" ] ],
3016
[ "<div></div>", "<div class='test'></div><span class='test'></span>",
@@ -33,27 +19,55 @@
3319
[ "<select></select>", "<option class='test'></option>", [ "option" ] ]
3420
];
3521

36-
for ( i = 0; i < inputs.length; i++ ) {
37-
input = inputs[ i ];
38-
elem = jQuery( policy.createHTML( input[ 0 ] ) );
39-
elem.append( policy.createHTML( input[ 1 ] ) );
40-
tags = elem.find( ".test" ).toArray().map( function( node ) {
41-
return node.nodeName.toLowerCase();
42-
} );
22+
function runTests( messagePrefix, getHtmlWrapper ) {
23+
for ( i = 0; i < inputs.length; i++ ) {
24+
input = inputs[ i ];
25+
elem = jQuery( getHtmlWrapper( input[ 0 ] ) );
26+
elem.append( getHtmlWrapper( input[ 1 ] ) );
27+
tags = elem.find( ".test" ).toArray().map( function( node ) {
28+
return node.nodeName.toLowerCase();
29+
} );
30+
results.push( {
31+
actual: tags,
32+
expected: input[ 2 ],
33+
message: messagePrefix + ": " + input[ 2 ].join( ", " )
34+
} );
35+
}
36+
37+
elem = jQuery( getHtmlWrapper( "<div></div>" ) );
38+
elem.append( getHtmlWrapper( "text content" ) );
4339
results.push( {
44-
actual: tags,
45-
expected: input[ 2 ],
46-
message: input[ 2 ].join( ", " )
40+
actual: elem.html(),
41+
expected: "text content",
42+
message: messagePrefix + ": text content properly appended"
4743
} );
4844
}
4945

50-
elem = jQuery( policy.createHTML( "<div></div>" ) );
51-
elem.append( policy.createHTML( "text content" ) );
52-
results.push( {
53-
actual: elem.html(),
54-
expected: "text content",
55-
message: "Text content properly appended"
56-
} );
46+
if ( typeof trustedTypes !== "undefined" ) {
47+
policy = trustedTypes.createPolicy( "jquery-test-policy", {
48+
createHTML: function( html ) {
49+
return html;
50+
}
51+
} );
52+
53+
runTests( "TrustedHTML", function wrapInTrustedHtml( input ) {
54+
return policy.createHTML( input );
55+
} );
56+
} else {
57+
58+
// No TrustedHTML support so let's at least run tests with object wrappers
59+
// with a proper `toString` function. This also shows that jQuery support
60+
// of TrustedHTML is generic and would work with similar APIs out of the box
61+
// as well. Ideally, we'd run these tests in browsers with TrustedHTML support
62+
// as well but due to the CSP TrustedHTML enforcement these tests would fail.
63+
runTests( "Object wrapper", function( input ) {
64+
return {
65+
toString: function toString() {
66+
return input;
67+
}
68+
};
69+
} );
70+
}
5771

5872
startIframeTest( results );
5973
</script>

test/unit/manipulation.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,15 +3010,17 @@ QUnit.test( "Works with invalid attempts to close the table wrapper", function(
30103010
} );
30113011

30123012
// Test trustedTypes support in browsers where they're supported (currently Chrome 83+).
3013+
// Browsers with no TrustedHTML support still run tests on object wrappers with
3014+
// a proper `toString` function.
30133015
testIframe(
30143016
"Basic TrustedHTML support (gh-4409)",
30153017
"mock.php?action=trustedHtml",
30163018
function( assert, jQuery, window, document, test ) {
3019+
30173020
assert.expect( 5 );
30183021

30193022
test.forEach( function( result ) {
30203023
assert.deepEqual( result.actual, result.expected, result.message );
30213024
} );
3022-
},
3023-
typeof trustedTypes === "undefined" ? QUnit.skip : QUnit.test
3025+
}
30243026
);

0 commit comments

Comments
 (0)