| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 (function() { | |
| 6 var undoReplaceScripts = []; | |
| 7 | |
| 8 var flags = {}; | |
| 9 // populate flags from location | |
| 10 location.search.slice(1).split('&').forEach(function(o) { | |
| 11 o = o.split('='); | |
| 12 o[0] && (flags[o[0]] = o[1] || true); | |
| 13 }); | |
| 14 | |
| 15 // Webkit is migrating from layoutTestController to testRunner, we use | |
| 16 // layoutTestController as a fallback until that settles in. | |
| 17 var runner = window.testRunner || window.layoutTestController; | |
| 18 | |
| 19 if (runner) { | |
| 20 runner.dumpAsText(); | |
| 21 runner.waitUntilDone(); | |
| 22 } | |
| 23 | |
| 24 function dumpDOM() { | |
| 25 // Undo any scripts that were modified. | |
| 26 undoReplaceScripts.forEach(function(undo) { undo(); }); | |
| 27 | |
| 28 function expandShadowRoot(node) { | |
| 29 for (var n = node.firstChild; n; n = n.nextSibling) { | |
| 30 expandShadowRoot(n); | |
| 31 } | |
| 32 var shadow = node.shadowRoot || node.webkitShadowRoot || | |
| 33 node.olderShadowRoot; | |
| 34 | |
| 35 if (shadow) { | |
| 36 expandShadowRoot(shadow); | |
| 37 | |
| 38 var name = 'shadow-root'; | |
| 39 if (shadow == node.olderShadowRoot) name = 'older-' + name; | |
| 40 | |
| 41 var fakeShadow = document.createElement(name); | |
| 42 while (shadow.firstChild) fakeShadow.appendChild(shadow.firstChild); | |
| 43 node.insertBefore(fakeShadow, node.firstChild); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 // TODO(jmesserly): use querySelector to workaround unwrapped "document". | |
| 48 expandShadowRoot(document.querySelector('body')); | |
| 49 | |
| 50 // Clean up all of the junk added to the DOM by js-interop and shadow CSS | |
| 51 // TODO(jmesserly): it seems like we're leaking lots of dart-port attributes | |
| 52 // for the document elemenet | |
| 53 function cleanTree(node) { | |
| 54 for (var n = node.firstChild; n; n = n.nextSibling) { | |
| 55 cleanTree(n); | |
| 56 } | |
| 57 | |
| 58 // TODO(terry): Need to remove attributes in the dart-port: namespace | |
| 59 // these are added for JS interop. See bug | |
| 60 // https://code.google.com/p/dart/issues/detail?id=12645 | |
| 61 // The new dart:js shouldn't need these attrs for dart2js or | |
| 62 // Dartium (won't need with native support) then remove the | |
| 63 // below code. | |
| 64 // Remove JS interop dart-port attributes, | |
| 65 if (node.tagName == 'HTML' && node.attributes) { | |
| 66 for (var i = node.attributes.length; i--; i >= 0) { | |
| 67 var attrNode = node.attributes[i]; | |
| 68 if (attrNode && attrNode.name.indexOf('dart-port:') == 0) { | |
| 69 node.removeAttributeNode(attrNode); | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 // We remove the contents of style tags so that we can compare both runs | |
| 75 // with and without the runtmie css shim. We keep the STYLE right under | |
| 76 // HEAD, because it is not affected by the shim. | |
| 77 if (node.tagName == 'STYLE') { | |
| 78 if (node.attributes['shadowcssshim'] != null) { | |
| 79 node.parentNode.removeChild(node); | |
| 80 } else if (node.parentNode.tagName != "HEAD") { | |
| 81 node.textContent = '/* style hidden by testing.js */' | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 if (node.tagName == 'SCRIPT') { | |
| 86 if (node.textContent.indexOf('_DART_TEMPORARY_ATTACHED') >= 0) { | |
| 87 node.parentNode.removeChild(node); | |
| 88 } else { | |
| 89 // Remove the JS Interop script. | |
| 90 var typeAttr = node.getAttributeNode("type"); | |
| 91 if (typeAttr && typeAttr.value == "text/javascript") { | |
| 92 if (node.textContent.indexOf( | |
| 93 "(function() {\n // Proxy support for js.dart.\n\n") == 0) { | |
| 94 node.parentNode.removeChild(node); | |
| 95 } | |
| 96 } | |
| 97 } | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 // TODO(jmesserly): use querySelector to workaround unwrapped "document". | |
| 102 cleanTree(document.querySelector('html')); | |
| 103 | |
| 104 var out = document.createElement('pre'); | |
| 105 out.textContent = document.documentElement.outerHTML; | |
| 106 document.body.innerHTML = ''; | |
| 107 document.body.appendChild(out); | |
| 108 } | |
| 109 | |
| 110 function messageHandler(e) { | |
| 111 if (e.data == 'done' && runner) { | |
| 112 // On success, dump the DOM. Convert shadowRoot contents into | |
| 113 // <shadow-root> | |
| 114 dumpDOM(); | |
| 115 runner.notifyDone(); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 window.addEventListener('message', messageHandler, false); | |
| 120 | |
| 121 function errorHandler(e) { | |
| 122 if (runner) { | |
| 123 window.setTimeout(function() { runner.notifyDone(); }, 0); | |
| 124 } | |
| 125 window.console.log('FAIL'); | |
| 126 } | |
| 127 | |
| 128 window.addEventListener('error', errorHandler, false); | |
| 129 | |
| 130 if (navigator.webkitStartDart && !flags.js) { | |
| 131 // TODO(jmesserly): fix this so we don't need to copy from browser/dart.js | |
| 132 if (!navigator.webkitStartDart()) { | |
| 133 document.body.innerHTML = 'This build has expired. Please download a new
Dartium at http://www.dartlang.org/dartium/index.html'; | |
| 134 } | |
| 135 } else { | |
| 136 if (flags.shadowdomjs) { | |
| 137 // Allow flags to force polyfill of ShadowDOM so we can test it. | |
| 138 window.__forceShadowDomPolyfill = true; | |
| 139 } | |
| 140 | |
| 141 // TODO: | |
| 142 // - Support in-browser compilation. | |
| 143 // - Handle inline Dart scripts. | |
| 144 window.addEventListener("DOMContentLoaded", function (e) { | |
| 145 // Fall back to compiled JS. Run through all the scripts and | |
| 146 // replace them if they have a type that indicate that they source | |
| 147 // in Dart code. | |
| 148 // | |
| 149 // <script type="application/dart" src="..."></script> | |
| 150 // | |
| 151 var scripts = document.getElementsByTagName("script"); | |
| 152 var length = scripts.length; | |
| 153 for (var i = 0; i < length; ++i) { | |
| 154 var script = scripts[i]; | |
| 155 if (script.type == "application/dart") { | |
| 156 // Remap foo.dart to foo.dart.js. | |
| 157 if (script.src && script.src != '') { | |
| 158 var jsScript = document.createElement('script'); | |
| 159 jsScript.src = script.src.replace(/\.dart(?=\?|$)/, '.dart.js'); | |
| 160 var parent = script.parentNode; | |
| 161 // TODO(vsm): Find a solution for issue 8455 that works with more | |
| 162 // than one script. | |
| 163 document.currentScript = jsScript; | |
| 164 | |
| 165 undoReplaceScripts.push(function() { | |
| 166 parent.replaceChild(script, jsScript); | |
| 167 }); | |
| 168 parent.replaceChild(jsScript, script); | |
| 169 } | |
| 170 } | |
| 171 } | |
| 172 }, false); | |
| 173 } | |
| 174 | |
| 175 })(); | |
| OLD | NEW |