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 // Remove dart-port attributes |
| 59 if (node.attributes) { |
| 60 for (var i = 0; i < node.attributes.length; i++) { |
| 61 if (node.attributes[i].value.indexOf('dart-port') == 0) { |
| 62 node.removeAttribute(i); |
| 63 } |
| 64 } |
| 65 } |
| 66 |
| 67 if (node.tagName == 'script' && |
| 68 node.textContent.indexOf('_DART_TEMPORARY_ATTACHED') >= 0) { |
| 69 node.parentNode.removeChild(node); |
| 70 } |
| 71 } |
| 72 |
| 73 // TODO(jmesserly): use querySelector to workaround unwrapped "document". |
| 74 cleanTree(document.querySelector('html')); |
| 75 |
| 76 var out = document.createElement('pre'); |
| 77 out.textContent = document.documentElement.outerHTML; |
| 78 document.body.innerHTML = ''; |
| 79 document.body.appendChild(out); |
| 80 } |
| 81 |
| 82 function messageHandler(e) { |
| 83 if (e.data == 'done' && runner) { |
| 84 // On success, dump the DOM. Convert shadowRoot contents into |
| 85 // <shadow-root> |
| 86 dumpDOM(); |
| 87 runner.notifyDone(); |
| 88 } |
| 89 } |
| 90 |
| 91 window.addEventListener('message', messageHandler, false); |
| 92 |
| 93 function errorHandler(e) { |
| 94 if (runner) { |
| 95 window.setTimeout(function() { runner.notifyDone(); }, 0); |
| 96 } |
| 97 window.console.log('FAIL'); |
| 98 } |
| 99 |
| 100 window.addEventListener('error', errorHandler, false); |
| 101 |
| 102 if (navigator.webkitStartDart && !flags.js) { |
| 103 // TODO(jmesserly): fix this so we don't need to copy from browser/dart.js |
| 104 if (!navigator.webkitStartDart()) { |
| 105 document.body.innerHTML = 'This build has expired. Please download a new
Dartium at http://www.dartlang.org/dartium/index.html'; |
| 106 } |
| 107 } else { |
| 108 if (flags.shadowdomjs) { |
| 109 // Allow flags to force polyfill of ShadowDOM so we can test it. |
| 110 window.__forceShadowDomPolyfill = true; |
| 111 } |
| 112 |
| 113 // TODO: |
| 114 // - Support in-browser compilation. |
| 115 // - Handle inline Dart scripts. |
| 116 window.addEventListener("DOMContentLoaded", function (e) { |
| 117 // Fall back to compiled JS. Run through all the scripts and |
| 118 // replace them if they have a type that indicate that they source |
| 119 // in Dart code. |
| 120 // |
| 121 // <script type="application/dart" src="..."></script> |
| 122 // |
| 123 var scripts = document.getElementsByTagName("script"); |
| 124 var length = scripts.length; |
| 125 for (var i = 0; i < length; ++i) { |
| 126 var script = scripts[i]; |
| 127 if (script.type == "application/dart") { |
| 128 // Remap foo.dart to foo.dart.js. |
| 129 if (script.src && script.src != '') { |
| 130 var jsScript = document.createElement('script'); |
| 131 jsScript.src = script.src.replace(/\.dart(?=\?|$)/, '.dart.js'); |
| 132 var parent = script.parentNode; |
| 133 // TODO(vsm): Find a solution for issue 8455 that works with more |
| 134 // than one script. |
| 135 document.currentScript = jsScript; |
| 136 |
| 137 undoReplaceScripts.push(function() { |
| 138 parent.replaceChild(script, jsScript); |
| 139 }); |
| 140 parent.replaceChild(jsScript, script); |
| 141 } |
| 142 } |
| 143 } |
| 144 }, false); |
| 145 } |
| 146 |
| 147 })(); |
OLD | NEW |