Document.currentScript.html (5371B)
1 <!DOCTYPE HTML> 2 <meta charset=utf-8> 3 <title>Document.currentScript</title> 4 <link rel=help href="https://html.spec.whatwg.org/multipage/#dom-document-currentscript"> 5 <link rel=help href="https://html.spec.whatwg.org/multipage/#execute-the-script-block"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <script src="/common/get-host-info.sub.js"></script> 9 <div id="log"></div> 10 <script> 11 var data = { 12 "parse-inline" : [], 13 "parse-ext" : [], 14 "dom-inline" : [], 15 "dom-ext" : [], 16 "nested" : ["nested-outer","nested-inner","nested-outer"], 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 "script-async-false" : [], 26 "iframe-src" : [], 27 "cross-origin" : [null], 28 "document-write" : [], 29 "microtask": [], 30 }; 31 32 var expected = {}; 33 var actual = {}; 34 35 Object.keys(data).forEach(function(id) { 36 var test_expected = data[id]; 37 if(test_expected.length == 0) { 38 test_expected = [id]; 39 } 40 expected[id] = test_expected; 41 actual[id] = []; 42 }); 43 44 var tests = {}; 45 setup({allow_uncaught_exception : true}); 46 47 Object.keys(expected).forEach(function(id) { 48 var testmsg = "Script " + id; 49 tests[id] = async_test(testmsg); 50 }); 51 52 function verify(id) { 53 tests[id].step(function() { 54 actual[id].push(document.currentScript); 55 }) 56 } 57 58 function finish(id) { 59 tests[id].step(function() { 60 assert_array_equals(actual[id],expected[id].map(function(id) { 61 return document.getElementById(id); 62 })); 63 this.done(); 64 }) 65 } 66 67 </script> 68 69 <!-- Test parser inserted scripts --> 70 <script id="parse-inline"> 71 verify('parse-inline'); 72 finish('parse-inline') 73 </script> 74 <script id="parse-ext" src="data:text/plain,verify('parse-ext')"></script> 75 <script>finish('parse-ext');</script> 76 77 <!-- Test DOM inserted scripts --> 78 <script> 79 var s = document.createElement("script"); 80 s.textContent = "verify('dom-inline');"; 81 s.id = "dom-inline"; 82 document.body.appendChild(s); 83 finish('dom-inline'); 84 85 s = document.createElement("script"); 86 s.src = "data:text/plain,verify('dom-ext');"; 87 s.id = "dom-ext"; 88 s.onload = function() { 89 finish('dom-ext'); 90 } 91 document.body.appendChild(s); 92 </script> 93 94 <!-- Test Nested scripts --> 95 <script id="nested-outer"> 96 verify("nested"); 97 var s = document.createElement("script"); 98 s.textContent = "verify('nested')"; 99 s.id = "nested-inner"; 100 document.body.appendChild(s); 101 verify("nested"); 102 finish('nested'); 103 </script> 104 105 <!-- Test script load error event listener --> 106 <script> 107 function testLoadFail() { 108 verify('script-load-error'); 109 finish('script-load-error'); 110 } 111 </script> 112 113 <script src="http://some.nonexistant.test/fail" id="script-load-error" onerror="testLoadFail()"> 114 </script> 115 116 <!-- Test for runtime and compile time errors --> 117 <script> 118 window.onerror = function() { 119 verify('script-window-error'); 120 } 121 122 var s = document.createElement("script"); 123 s.id = "script-error-compile"; 124 s.textContent = "{"; 125 document.body.appendChild(s); 126 127 window.onerror = function() { 128 verify('script-window-error'); 129 } 130 131 s = document.createElement("script"); 132 s.id = "script-error-runtime"; 133 s.textContent = "undefinedfn();"; 134 document.body.appendChild(s); 135 136 finish('script-window-error'); 137 </script> 138 139 <!-- Verify in setTimeout --> 140 <script> 141 setTimeout(function() { 142 verify('timeout'); 143 finish('timeout'); 144 },0); 145 </script> 146 147 <!-- Verify in eval --> 148 <script id="eval"> 149 eval('verify("eval")'); 150 finish("eval"); 151 </script> 152 153 <!-- Verify in synchronous xhr --> 154 <script id="xhr-test"> 155 var request = new XMLHttpRequest(); 156 request.open('GET','/',false); 157 request.send(null); 158 159 if(request.status === 200) { 160 verify('xhr-test'); 161 finish('xhr-test'); 162 } 163 </script> 164 165 <!-- Testing script within svg --> 166 <svg> 167 <script id="script-svg"> 168 verify('script-svg'); 169 finish('script-svg'); 170 </script> 171 </svg> 172 173 <!-- Test script async and defer --> 174 <script id='script-async' async src='data:text/plain,verify("script-async"),finish("script-async")'></script> 175 176 <script id='script-defer' defer src='data:text/plain,verify("script-defer"),finish("script-defer")'></script> 177 178 <!-- Test async = false dynamic script loading --> 179 <script> 180 var s = document.createElement("script"); 181 s.id = "script-async-false"; 182 s.src = "data:text/plain,verify('script-async-false');" 183 s.onload = function() { 184 finish('script-async-false'); 185 } 186 s.async = false; 187 document.body.appendChild(s); 188 </script> 189 190 <!-- Verify in iframe javascript uri scheme --> 191 <iframe src="javascript:parent.verify('iframe-src'),parent.finish('iframe-src')" 192 style="visibility:hidden;display:none"> 193 </iframe> 194 195 <!-- Testing cross origin script --> 196 <script> 197 var s = document.createElement("script"); 198 s.id = "cross-origin"; 199 s.src = get_host_info(). HTTP_REMOTE_ORIGIN + "/html/dom/documents/dom-tree-accessors/cross-domain.js" 200 s.onload = function() { 201 verify('cross-origin') 202 finish('cross-origin'); 203 } 204 document.body.appendChild(s); 205 206 </script> 207 208 <!-- Testing document.write --> 209 <script> 210 document.write('<script id="document-write">verify("document-write"); finish("document-write");</' + 'script>'); 211 </script> 212 213 <!-- Testing microtask --> 214 <script id="microtask"> 215 Promise.resolve().then(() => { 216 verify("microtask"); 217 finish("microtask"); 218 }); 219 </script>