href-script-element.html (4727B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <title>Href - script element tests</title> 4 <script src='/resources/testharness.js'></script> 5 <script src='/resources/testharnessreport.js'></script> 6 <script src='testcommon.js'></script> 7 <body> 8 <div id='log'></div> 9 <svg id='svg' width='100' height='100' viewBox='0 0 100 100'> 10 </svg> 11 <script> 12 'use strict'; 13 14 // Note: 15 // The order of these tests shouldn't be changed because we don't unload 16 // the external script file even if we expect the <script> element will be 17 // removed by childNode.remove() and Garbage Collection after a test has been 18 // finished. Therefore, I intentionally make them load externalScript1 and 19 // externalScript2 alternately, and we can check if the results are changed 20 // after reloading the other script. 21 // Throughout this test, we periodically need to verify that a script 22 // *does not load* after we've made a tweak. To do that, we have to 23 // wait "long enough for it to have loaded", and then make sure nothing 24 // has changed. We estimate "long enough" by adding an extra dummy 25 // <script> element and watching for its load event. 26 27 promise_test(function(t) { 28 var svg = document.getElementById('svg'); 29 var script = createSVGElement(t, 'script', svg); 30 31 script.setAttribute('type', 'text/javascript'); 32 script.setAttribute('href', 'testScripts/externalScript1.js'); 33 assert_equals(script.href.baseVal, 'testScripts/externalScript1.js'); 34 35 return waitEvent(script, 'load').then(function() { 36 assert_equals(loadedScript(), 'externalScript1', 37 'Link to correct external script'); 38 39 script.setAttributeNS(XLINKNS, 'xlink:href', 40 'testScripts/externalScript2.js'); 41 42 // Load an dummy script to trigger a load event. 43 var dummyScript = createSVGElement(t, 'script', svg); 44 dummyScript.setAttribute('href', 'testScripts/dummyScript.js'); 45 return waitEvent(dummyScript, 'load'); 46 }).then(function() { 47 assert_equals(script.href.baseVal, 'testScripts/externalScript1.js'); 48 assert_equals(loadedScript(), 'externalScript1', 49 'Still link to the external script from href'); 50 }); 51 }, 'Test for loading external script from href when setting href and ' + 52 'then xlink:href'); 53 54 promise_test(function(t) { 55 var svg = document.getElementById('svg'); 56 var script = createSVGElement(t, 'script', svg); 57 58 script.setAttribute('type', 'text/javascript'); 59 script.setAttributeNS(XLINKNS, 'xlink:href', 60 'testScripts/externalScript2.js'); 61 assert_equals(script.href.baseVal, 'testScripts/externalScript2.js'); 62 63 return waitEvent(script, 'load').then(function() { 64 assert_equals(loadedScript(), 'externalScript2', 65 'Link to the external script from xlink:href'); 66 67 script.setAttribute('href', 'testScripts/externalScript1.js'); 68 69 // Load an dummy script to trigger a load event. 70 var dummyScript = createSVGElement(t, 'script', svg); 71 dummyScript.setAttribute('href', 'testScripts/dummyScript.js'); 72 return waitEvent(dummyScript, 'load'); 73 }).then(function() { 74 assert_equals(script.href.baseVal, 'testScripts/externalScript1.js', 75 'href() should prefer href attribute over xlink:href'); 76 assert_equals(loadedScript(), 'externalScript2', 77 'Still link to the external script from xlink:href'); 78 }); 79 }, 'Test for loading external script from xlnk:href by adding xlink:href and ' + 80 'then href'); 81 82 promise_test(function(t) { 83 var svg = document.getElementById('svg'); 84 var script = createSVGElement(t, 'script', svg); 85 86 script.setAttribute('type', 'text/javascript'); 87 script.setAttribute('href', 'testScripts/externalScript1.js'); 88 script.setAttributeNS(XLINKNS, 'xlink:href', 89 'testScripts/externalScript2.js'); 90 assert_equals(script.href.baseVal, 'testScripts/externalScript1.js'); 91 92 return waitEvent(script, 'load').then(function() { 93 assert_equals(loadedScript(), 'externalScript1', 94 'Link to the external script by href'); 95 96 script.removeAttribute('href'); 97 assert_equals(script.href.baseVal, 'testScripts/externalScript2.js', 98 'href() returns xlink:href attribute because href was unset'); 99 100 // Load an dummy script to trigger a load event. 101 var dummyScript = createSVGElement(t, 'script', svg); 102 dummyScript.setAttribute('href', 'testScripts/dummyScript.js'); 103 return waitEvent(dummyScript, 'load'); 104 }).then(function() { 105 assert_equals(loadedScript(), 'externalScript1', 106 'The external script loaded from href is still loaded'); 107 }); 108 }, 'Test for loading external script from href by adding href and ' + 109 'then xlink:href, and then removing href'); 110 111 </script> 112 </body>