summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/dom/documents/dom-tree-accessors/Document.currentScript.html
blob: 245bae98ee109a0a3034e840a4d85af9ab8e5036 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Document.currentScript</title>
<link rel=help href="https://html.spec.whatwg.org/multipage/#dom-document-currentscript">
<link rel=help href="https://html.spec.whatwg.org/multipage/#execute-the-script-block">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<div id="log"></div>
<script>
var data = {
  "parse-inline" : [],
  "parse-ext" : [],
  "dom-inline" : [],
  "dom-ext" : [],
  "nested" : ["nested-outer","nested-inner","nested-outer"],
  "script-load-error" : [null],
  "script-window-error" : ["script-error-compile","script-error-runtime"],
  "timeout" : [null],
  "eval" : [],
  "xhr-test" : [],
  "script-svg" : [],
  "script-async" : [],
  "script-defer" : [],
  "script-async-false" : [],
  "iframe-src" : [],
  "cross-origin" : [null],
  "document-write" : [],
  "microtask": [],
};

var expected = {};
var actual = {};

Object.keys(data).forEach(function(id) {
  var test_expected = data[id];
  if(test_expected.length == 0) {
    test_expected = [id];
  }
  expected[id] = test_expected;
  actual[id] = [];
});

var tests = {};
setup({allow_uncaught_exception : true});

Object.keys(expected).forEach(function(id) {
  var testmsg = "Script " + id;
  tests[id] = async_test(testmsg);
});

function verify(id) {
  tests[id].step(function() {
    actual[id].push(document.currentScript);
  })
}

function finish(id) {
  tests[id].step(function() {
    assert_array_equals(actual[id],expected[id].map(function(id) {
      return document.getElementById(id);
    }));
    this.done();
  })
}

</script>

<!-- Test parser inserted scripts -->
<script id="parse-inline">
verify('parse-inline');
finish('parse-inline')
</script>
<script id="parse-ext" src="data:text/plain,verify('parse-ext')"></script>
<script>finish('parse-ext');</script>

<!-- Test DOM inserted scripts -->
<script>
var s = document.createElement("script");
s.textContent = "verify('dom-inline');";
s.id = "dom-inline";
document.body.appendChild(s);
finish('dom-inline');

s = document.createElement("script");
s.src = "data:text/plain,verify('dom-ext');";
s.id = "dom-ext";
s.onload = function() {
  finish('dom-ext');
}
document.body.appendChild(s);
</script>

<!-- Test Nested scripts -->
<script id="nested-outer">
 verify("nested");
 var s = document.createElement("script");
 s.textContent = "verify('nested')";
 s.id = "nested-inner";
 document.body.appendChild(s);
 verify("nested");
 finish('nested');
</script>

<!-- Test script load error event listener -->
<script>
function testLoadFail() {
  verify('script-load-error');
  finish('script-load-error');
}
</script>

<script src="http://some.nonexistant.test/fail" id="script-load-error" onerror="testLoadFail()">
</script>

<!-- Test for runtime and compile time errors -->
<script>
  window.onerror = function() {
    verify('script-window-error');
  }

  var s = document.createElement("script");
  s.id = "script-error-compile";
  s.textContent = "{";
  document.body.appendChild(s);

  window.onerror = function() {
    verify('script-window-error');
  }

  s = document.createElement("script");
  s.id = "script-error-runtime";
  s.textContent = "undefinedfn();";
  document.body.appendChild(s);

  finish('script-window-error');
</script>

<!-- Verify in setTimeout -->
<script>
  setTimeout(function() {
    verify('timeout');
    finish('timeout');
  },0);
</script>

<!-- Verify in eval -->
<script id="eval">
  eval('verify("eval")');
  finish("eval");
</script>

<!-- Verify in synchronous xhr -->
<script id="xhr-test">
  var request = new XMLHttpRequest();
  request.open('GET','/',false);
  request.send(null);

  if(request.status === 200) {
    verify('xhr-test');
    finish('xhr-test');
  }
</script>

<!-- Testing script within svg -->
<svg>
  <script id="script-svg">
    verify('script-svg');
    finish('script-svg');
  </script>
</svg>

<!-- Test script async and defer -->
<script id='script-async' async src='data:text/plain,verify("script-async"),finish("script-async")'></script>

<script id='script-defer' defer src='data:text/plain,verify("script-defer"),finish("script-defer")'></script>

<!-- Test async = false dynamic script loading -->
<script>
  var s = document.createElement("script");
  s.id = "script-async-false";
  s.src = "data:text/plain,verify('script-async-false');"
  s.onload = function() {
    finish('script-async-false');
  }
  s.async = false;
  document.body.appendChild(s);
</script>

<!-- Verify in iframe javascript uri scheme -->
<iframe src="javascript:parent.verify('iframe-src'),parent.finish('iframe-src')"
        style="visibility:hidden;display:none">
</iframe>

<!-- Testing cross origin script -->
<script>
var s = document.createElement("script");
s.id = "cross-origin";
s.src = get_host_info(). HTTP_REMOTE_ORIGIN + "/html/dom/documents/dom-tree-accessors/cross-domain.js"
s.onload = function() {
  verify('cross-origin')
  finish('cross-origin');
}
document.body.appendChild(s);

</script>

<!-- Testing document.write -->
<script>
document.write('<script id="document-write">verify("document-write"); finish("document-write");</' + 'script>');
</script>

<!-- Testing microtask -->
<script id="microtask">
  Promise.resolve().then(() => {
    verify("microtask");
    finish("microtask");
  });
</script>