OLD | NEW |
1 <!DOCTYPE HTML> | 1 <!DOCTYPE HTML> |
2 <meta charset=utf-8> | 2 <meta charset=utf-8> |
3 <title>Document.currentScript</title> | 3 <title>Document.currentScript</title> |
4 <link rel=help href="https://html.spec.whatwg.org/multipage/#dom-document-curren
tscript"> | 4 <link rel=help href="https://html.spec.whatwg.org/multipage/#dom-document-curren
tscript"> |
5 <link rel=help href="https://html.spec.whatwg.org/multipage/#execute-the-script-
block"> | 5 <link rel=help href="https://html.spec.whatwg.org/multipage/#execute-the-script-
block"> |
6 <script src="../../../../../../resources/testharness.js"></script> | 6 <script src="../../../../../../resources/testharness.js"></script> |
7 <script src="../../../../../../resources/testharnessreport.js"></script> | 7 <script src="../../../../../../resources/testharnessreport.js"></script> |
8 <div id="log"></div> | 8 <div id="log"></div> |
9 <script> | 9 <script> |
10 var expected = [ | 10 var data = { |
11 "parse-inline", | 11 "parse-inline" : [], |
12 "parse-ext", | 12 "parse-ext" : [], |
13 "dom-inline", | 13 "dom-inline" : [], |
14 "dom-ext", | 14 "dom-ext" : [], |
15 ]; | 15 "nested" : ["nested-outer","nested-inner","nested-outer"], |
| 16 "script-exec" : ["script-exec-before-after","script-exec-before-after"], |
| 17 "script-load-error" : [null], |
| 18 "script-window-error" : ["script-error-compile","script-error-runtime"], |
| 19 "timeout" : [null], |
| 20 "eval" : [], |
| 21 "xhr-test" : [], |
| 22 "script-svg" : [], |
| 23 "script-async" : [], |
| 24 "script-defer" : [] |
| 25 }; |
| 26 |
| 27 var expected = {}; |
| 28 var actual = {}; |
| 29 |
| 30 Object.keys(data).forEach(function(id) { |
| 31 var test_expected = data[id]; |
| 32 if(test_expected.length == 0) { |
| 33 test_expected = [id]; |
| 34 } |
| 35 expected[id] = test_expected; |
| 36 actual[id] = []; |
| 37 }); |
| 38 |
16 var tests = {}; | 39 var tests = {}; |
17 expected.forEach(function(id) { | 40 setup({allow_uncaught_exception : true}); |
18 tests[id] = async_test("Script " + id); | 41 |
| 42 Object.keys(expected).forEach(function(id) { |
| 43 var testmsg = "Script " + id; |
| 44 tests[id] = async_test(testmsg); |
19 }); | 45 }); |
20 function verifyScript(id) { | 46 |
| 47 function verify(id) { |
21 tests[id].step(function() { | 48 tests[id].step(function() { |
22 assert_equals(document.currentScript, document.getElementById(id)); | 49 actual[id].push(document.currentScript); |
| 50 }) |
| 51 } |
| 52 |
| 53 function finish(id) { |
| 54 tests[id].step(function() { |
| 55 assert_array_equals(actual[id],expected[id].map(function(id) { |
| 56 return document.getElementById(id); |
| 57 })); |
23 this.done(); | 58 this.done(); |
24 }); | 59 }) |
25 } | 60 } |
| 61 |
26 </script> | 62 </script> |
27 | 63 |
28 <!-- Test parser inserted scripts --> | 64 <!-- Test parser inserted scripts --> |
29 <script id="parse-inline"> | 65 <script id="parse-inline"> |
30 verifyScript("parse-inline"); | 66 verify('parse-inline'); |
| 67 finish('parse-inline') |
31 </script> | 68 </script> |
32 <script id="parse-ext" src="data:text/plain,verifyScript('parse-ext');"></script
> | 69 <script id="parse-ext" src="data:text/plain,verify('parse-ext')"></script> |
| 70 <script>finish('parse-ext');</script> |
33 | 71 |
34 <!-- Test DOM inserted scripts --> | 72 <!-- Test DOM inserted scripts --> |
35 <script> | 73 <script> |
36 var s = document.createElement("script"); | 74 var s = document.createElement("script"); |
37 s.textContent = "verifyScript('dom-inline');"; | 75 s.textContent = "verify('dom-inline');"; |
38 s.id = "dom-inline"; | 76 s.id = "dom-inline"; |
39 document.body.appendChild(s); | 77 document.body.appendChild(s); |
| 78 finish('dom-inline'); |
40 | 79 |
41 s = document.createElement("script"); | 80 s = document.createElement("script"); |
42 s.src = "data:text/plain,verifyScript('dom-ext');"; | 81 s.src = "data:text/plain,verify('dom-ext');"; |
43 s.id = "dom-ext"; | 82 s.id = "dom-ext"; |
| 83 s.onload = function() { |
| 84 finish('dom-ext'); |
| 85 } |
44 document.body.appendChild(s); | 86 document.body.appendChild(s); |
45 </script> | 87 </script> |
| 88 |
| 89 <!-- Test Nested scripts --> |
| 90 <script id="nested-outer"> |
| 91 verify("nested"); |
| 92 var s = document.createElement("script"); |
| 93 s.textContent = "verify('nested')"; |
| 94 s.id = "nested-inner"; |
| 95 document.body.appendChild(s); |
| 96 verify("nested"); |
| 97 finish('nested'); |
| 98 </script> |
| 99 |
| 100 <!-- Test beforescriptexecute and afterscriptexecute --> |
| 101 <script id="script-exec-before-after"> |
| 102 function verifyScriptExec(e) { |
| 103 verify('script-exec'); |
| 104 } |
| 105 |
| 106 document.addEventListener('beforescriptexecute', verifyScriptExec, false); |
| 107 document.addEventListener('afterscriptexecute', verifyScriptExec, false); |
| 108 |
| 109 var s = document.createElement("script"); |
| 110 s.id = "script-exec-test"; |
| 111 s.textContent = "function nop() { return false }"; |
| 112 document.body.appendChild(s); |
| 113 |
| 114 document.removeEventListener('beforescriptexecute', verifyScriptExec); |
| 115 document.removeEventListener('afterscriptexecute', verifyScriptExec); |
| 116 |
| 117 finish('script-exec'); |
| 118 </script> |
| 119 |
| 120 <!-- Test script load error event listener --> |
| 121 <script> |
| 122 function testLoadFail() { |
| 123 verify('script-load-error'); |
| 124 finish('script-load-error'); |
| 125 } |
| 126 </script> |
| 127 |
| 128 <script src="http://some.nonexistant.test/fail" id="script-load-error" onerror="
testLoadFail()"> |
| 129 </script> |
| 130 |
| 131 <!-- Test for runtime and compile time errors --> |
| 132 <script> |
| 133 window.onerror = function() { |
| 134 verify('script-window-error'); |
| 135 } |
| 136 |
| 137 var s = document.createElement("script"); |
| 138 s.id = "script-error-compile"; |
| 139 s.textContent = "{"; |
| 140 document.body.appendChild(s); |
| 141 |
| 142 window.onerror = function() { |
| 143 verify('script-window-error'); |
| 144 } |
| 145 |
| 146 s = document.createElement("script"); |
| 147 s.id = "script-error-runtime"; |
| 148 s.textContent = "undefinedfn();"; |
| 149 document.body.appendChild(s); |
| 150 |
| 151 finish('script-window-error'); |
| 152 </script> |
| 153 |
| 154 <!-- Verify in setTimeout --> |
| 155 <script> |
| 156 setTimeout(function() { |
| 157 verify('timeout'); |
| 158 finish('timeout'); |
| 159 },0); |
| 160 </script> |
| 161 |
| 162 <!-- Verify in eval --> |
| 163 <script id="eval"> |
| 164 eval('verify("eval")'); |
| 165 finish("eval"); |
| 166 </script> |
| 167 |
| 168 <!-- Verify in synchronous xhr --> |
| 169 <script id="xhr-test"> |
| 170 var request = new XMLHttpRequest(); |
| 171 request.open('GET','/',false); |
| 172 request.send(null); |
| 173 |
| 174 if(request.status === 200) { |
| 175 verify('xhr-test'); |
| 176 finish('xhr-test'); |
| 177 } |
| 178 </script> |
| 179 |
| 180 <!-- Testing script within svg --> |
| 181 <svg> |
| 182 <script id="script-svg"> |
| 183 verify('script-svg'); |
| 184 finish('script-svg'); |
| 185 </script> |
| 186 </svg> |
| 187 |
| 188 <!-- Test script async and defer --> |
| 189 <script id='script-async' async src='data:text/plain,verify("script-async"),fini
sh("script-async")'></script> |
| 190 |
| 191 <script id='script-defer' defer src='data:text/plain,verify("script-defer"),fini
sh("script-defer")'></script> |
OLD | NEW |