summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/trusted-types/block-string-assignment-to-Element-insertAdjacentHTML.tentative.html
blob: 228887d0423d3144afd684ace223bc16a7124f9f (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html>
<head>
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
  <script src="support/helper.sub.js"></script>

  <meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script';">
</head>
<body>
<div id="container"></div>
<script>
  var container = document.querySelector('#container');

  // Trusted HTML assignments do not throw.
  test(t => {
    let p = createHTML_policy(window, 1);
    let html = p.createHTML(INPUTS.HTML);
    let before = 'before';
    let after = 'after';
    let htmlBefore = p.createHTML(before);
    let htmlAfter = p.createHTML(after);

    var d = document.createElement('div');
    container.appendChild(d);

    d.insertAdjacentHTML('beforebegin', html);
    assert_equals(d.previousSibling.nodeType, Node.TEXT_NODE);
    assert_equals(d.previousSibling.data, RESULTS.HTML);

    d.insertAdjacentHTML('afterbegin', htmlBefore);
    d.insertAdjacentHTML('beforeend', htmlAfter);
    assert_equals(d.innerHTML, before + after);

    d.insertAdjacentHTML('afterend', html);
    assert_equals(d.nextSibling.nodeType, Node.TEXT_NODE);
    assert_equals(d.nextSibling.data, RESULTS.HTML);

    while (container.firstChild)
      container.firstChild.remove();
  }, "insertAdjacentHTML with html assigned via policy (successful HTML transformation).");

  // String assignments throw.
  test(t => {
    var d = document.createElement('div');
    container.appendChild(d);

    assert_throws_js(TypeError, _ => {
      d.insertAdjacentHTML('beforebegin', "<p>Fail</p>");
    });
    assert_throws_js(TypeError, _ => {
      d.insertAdjacentHTML('afterbegin', "<p>Fail</p>");
    });
    assert_throws_js(TypeError, _ => {
      d.insertAdjacentHTML('beforeend', "<p>Fail</p>");
    });
    assert_throws_js(TypeError, _ => {
      d.insertAdjacentHTML('afterend', "<p>Fail</p>");
    });

    assert_equals(d.previousSibling, null);
    assert_equals(d.firstChild, null);
    assert_equals(d.lastChild, null);
    assert_equals(d.nextSibling, null);

    while (container.firstChild)
      container.firstChild.remove();
  }, "`insertAdjacentHTML(string)` throws.");

  // Null assignment throws.
  test(t => {
    var d = document.createElement('div');
    container.appendChild(d);

    assert_throws_js(TypeError, _ => {
      d.insertAdjacentHTML('beforebegin', null);
    });
    assert_throws_js(TypeError, _ => {
      d.insertAdjacentHTML('afterbegin', null);
    });
    assert_throws_js(TypeError, _ => {
      d.insertAdjacentHTML('beforeend', null);
    });
    assert_throws_js(TypeError, _ => {
      d.insertAdjacentHTML('afterend', null);
    });

    assert_equals(d.previousSibling, null);
    assert_equals(d.firstChild, null);
    assert_equals(d.lastChild, null);
    assert_equals(d.nextSibling, null);
  }, "`insertAdjacentHTML(null)` throws.");

  // After default policy creation string assignment implicitly calls createHTML.
  test(t => {
    let p = window.trustedTypes.createPolicy("default", { createHTML: createHTMLJS }, true);

    var d = document.createElement('div');
    container.appendChild(d);

    d.insertAdjacentHTML('beforebegin', INPUTS.HTML);
    assert_equals(d.previousSibling.nodeType, Node.TEXT_NODE);
    assert_equals(d.previousSibling.data, RESULTS.HTML);

    d.insertAdjacentHTML('afterbegin', INPUTS.HTML);
    assert_equals(d.firstChild.nodeType, Node.TEXT_NODE);
    assert_equals(d.firstChild.data, RESULTS.HTML);

    d.insertAdjacentHTML('beforeend', INPUTS.HTML);
    assert_equals(d.lastChild.nodeType, Node.TEXT_NODE);
    assert_equals(d.lastChild.data, RESULTS.HTML);

    d.insertAdjacentHTML('afterend', INPUTS.HTML);
    assert_equals(d.nextSibling.nodeType, Node.TEXT_NODE);
    assert_equals(d.nextSibling.data, RESULTS.HTML);

    while (container.firstChild)
      container.firstChild.remove();
  }, "`insertAdjacentHTML(string)` assigned via default policy (successful HTML transformation).");

   // After default policy creation null assignment implicitly calls createHTML.
  test(t => {
    var d = document.createElement('div');
    container.appendChild(d);

    d.insertAdjacentHTML('beforebegin', null);
    assert_equals(d.previousSibling.nodeType, Node.TEXT_NODE);
    assert_equals(d.previousSibling.data, "null");

    d.insertAdjacentHTML('afterbegin', null);
    assert_equals(d.firstChild.nodeType, Node.TEXT_NODE);
    assert_equals(d.firstChild.data, "null");

    d.insertAdjacentHTML('beforeend', null);
    assert_equals(d.lastChild.nodeType, Node.TEXT_NODE);
    assert_equals(d.lastChild.data, "null");

    d.insertAdjacentHTML('afterend', null);
    assert_equals(d.nextSibling.nodeType, Node.TEXT_NODE);
    assert_equals(d.nextSibling.data, "null");

    while (container.firstChild)
      container.firstChild.remove();
  }, "`insertAdjacentHTML(null)` assigned via default policy does not throw.");
</script>
</body>
</html>