|
9 | 9 | <script src="../../dist/jquery.min.js"></script> |
10 | 10 | <script src="iframeTest.js"></script> |
11 | 11 | <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, |
22 | 13 | results = [], |
23 | | - policy = trustedTypes.createPolicy( "jquery-test-policy", { |
24 | | - createHTML: function( html ) { |
25 | | - return html; |
26 | | - } |
27 | | - } ), |
28 | 14 | inputs = [ |
29 | 15 | [ "<div></div>", "<div class='test'></div>", [ "div" ] ], |
30 | 16 | [ "<div></div>", "<div class='test'></div><span class='test'></span>", |
|
33 | 19 | [ "<select></select>", "<option class='test'></option>", [ "option" ] ] |
34 | 20 | ]; |
35 | 21 |
|
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" ) ); |
43 | 39 | 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" |
47 | 43 | } ); |
48 | 44 | } |
49 | 45 |
|
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 | + } |
57 | 71 |
|
58 | 72 | startIframeTest( results ); |
59 | 73 | </script> |
|
0 commit comments