aboutsummaryrefslogtreecommitdiffstats
path: root/test/data/trusted-html.html
blob: 063779a6218c156e5c8f52f698c5e738e8b60c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html>
<head>
	<meta charset=utf-8 />
	<title>body</title>
</head>
<body>
<div id="qunit-fixture"></div>
<script src="../../dist/jquery.min.js"></script>
<script src="iframeTest.js"></script>
<script>
	var i, input, elem, tags, policy,
		results = [],
		inputs = [
			[ "<div></div>", "<div class='test'></div>", [ "div" ] ],
			[ "<div></div>", "<div class='test'></div><span class='test'></span>",
				[ "div", "span" ] ],
			[ "<table></table>", "<td class='test'></td>", [ "td" ] ],
			[ "<select></select>", "<option class='test'></option>", [ "option" ] ]
		];

	function runTests( messagePrefix, getHtmlWrapper ) {
		for ( i = 0; i < inputs.length; i++ ) {
			input = inputs[ i ];
			elem = jQuery( getHtmlWrapper( input[ 0 ] ) );
			elem.append( getHtmlWrapper( input[ 1 ] ) );
			tags = elem.find( ".test" ).toArray().map( function( node ) {
				return node.nodeName.toLowerCase();
			} );
			results.push( {
				actual: tags,
				expected: input[ 2 ],
				message: messagePrefix + ": " + input[ 2 ].join( ", " )
			} );
		}

		elem = jQuery( getHtmlWrapper( "<div></div>" ) );
		elem.append( getHtmlWrapper( "text content" ) );
		results.push( {
			actual: elem.html(),
			expected: "text content",
			message: messagePrefix + ": text content properly appended"
		} );
	}

	if ( typeof trustedTypes !== "undefined" ) {
		policy = trustedTypes.createPolicy( "jquery-test-policy", {
			createHTML: function( html ) {
				return html;
			}
		} );

		runTests( "TrustedHTML", function wrapInTrustedHtml( input ) {
			return policy.createHTML( input );
		} );
	} else {

		// No TrustedHTML support so let's at least run tests with object wrappers
		// with a proper `toString` function. This also shows that jQuery support
		// of TrustedHTML is generic and would work with similar APIs out of the box
		// as well. Ideally, we'd run these tests in browsers with TrustedHTML support
		// as well but due to the CSP TrustedHTML enforcement these tests would fail.
		runTests( "Object wrapper", function( input ) {
			return {
				toString: function toString() {
					return input;
				}
			};
		} );
	}

	startIframeTest( results );
</script>
</body>
</html>