OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 // This script runs in HTML files and loads the corresponding test scripts for |
| 6 // either Dartium or a JS browser. It's used by "pub serve" and user-authored |
| 7 // HTML files; when running without "pub serve", the default HTML file manually |
| 8 // chooses between serving a Dart or JS script tag. |
| 9 window.onload = function() { |
| 10 |
| 11 // Sends an error message to the server indicating that the script failed to |
| 12 // load. |
| 13 // |
| 14 // This mimics a MultiChannel-formatted message. |
| 15 var sendLoadException = function(message) { |
| 16 window.parent.postMessage({ |
| 17 "href": window.location.href, |
| 18 "data": [0, {"type": "loadException", "message": message}] |
| 19 }, window.location.origin); |
| 20 } |
| 21 |
| 22 // The basename of the current page. |
| 23 var name = window.location.href.replace(/.*\//, '').replace(/#.*/, ''); |
| 24 |
| 25 // Find <link rel="x-dart-test">. |
| 26 var links = document.getElementsByTagName("link"); |
| 27 var testLinks = []; |
| 28 var length = links.length; |
| 29 for (var i = 0; i < length; ++i) { |
| 30 if (links[i].rel == "x-dart-test") testLinks.push(links[i]); |
| 31 } |
| 32 |
| 33 if (links.length != 1) { |
| 34 sendLoadException( |
| 35 'Expected exactly 1 <link rel="x-dart-test"> in ' + name + ', found ' + |
| 36 links.length + '.'); |
| 37 return; |
| 38 } |
| 39 |
| 40 var link = links[0]; |
| 41 |
| 42 if (link.href == '') { |
| 43 sendLoadException( |
| 44 'Expected <link rel="x-dart-test"> in ' + name + ' to have an "href" ' + |
| 45 'attribute.'); |
| 46 return; |
| 47 } |
| 48 |
| 49 var script = document.createElement('script'); |
| 50 |
| 51 // Load the compiled JS for a normal browser, and the Dart code for Dartium. |
| 52 if (navigator.userAgent.indexOf('(Dart)') === -1) { |
| 53 script.src = link.href + '.browser_test.dart.js'; |
| 54 } else { |
| 55 script.src = link.href + '.browser_test.dart'; |
| 56 script.type = 'application/dart'; |
| 57 } |
| 58 |
| 59 script.onerror = function(event) { |
| 60 var message = "Failed to load script at " + script.src + |
| 61 (event.message ? ": " + event.message : "."); |
| 62 sendLoadException(message); |
| 63 } |
| 64 |
| 65 var parent = link.parentNode; |
| 66 document.currentScript = script; |
| 67 parent.replaceChild(script, link); |
| 68 |
| 69 }; |
OLD | NEW |