// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. // This script dynamically prepares a set of files to run polymer.dart. It uses // the html_import polyfill to search for all imported files, then // it inlines all definitions on the top-level page (needed by // registerPolymerElement), and it removes script tags that appear inside // those tags. It finally rewrites the main entrypoint to call an initialization // function on each of the declared . // // This script is needed only when running polymer.dart in Dartium. It should be // removed by the polymer deployment commands. // As an example, given an input of this form: // // // // // // // // // // // // // // This script will simplifies the page as follows: // // // // // // // // // (function() { // Only run in Dartium. if (!navigator.webkitStartDart) { // TODO(sigmund): rephrase when we split build.dart in two: analysis vs // deploy pieces. console.warn('boot.js only works in Dartium. Run the build.dart' + ' tool to compile a depolyable JavaScript version') return; } document.write( ''); // Extract a Dart import URL from a script tag, which is the 'src' attribute // of the script tag, or a data-url with the script contents for inlined code. function getScriptUrl(script) { var url = script.src; if (url) { // Normalize package: urls var index = url.indexOf('packages/'); if (index == 0 || (index > 0 && url[index - 1] == '/')) { url = "package:" + url.slice(index + 9); } return url; } else { // TODO(sigmund): investigate how to eliminate the warning in Dartium // (changing to text/javascript hides the warning, but seems wrong). return "data:application/dart;base64," + window.btoa(script.textContent); } } // Moves from imported documents into the top-level page. function inlinePolymerElements(content, ref, seen) { if (!seen) seen = {}; var links = content.querySelectorAll('link[rel="import"]'); for (var i = 0; i < links.length; i++) { var link = links[i]; // TODO(jmesserly): figure out why ".import" fails in content_shell but // works in Dartium. if (link.import && link.import.href) link = link.import; if (seen[link.href]) continue; seen[link.href] = link; inlinePolymerElements(link.content, ref, seen); } if (content != document) { // no need to do anything for the top-level page var elements = content.querySelectorAll('polymer-element'); for (var i = 0; i < elements.length; i++) { document.body.insertBefore(elements[i], ref); } } } // Creates a Dart program that imports [urls] and passes them to initPolymer // (which in turn will invoke their main function, their methods marked with // @initMethod, and register any custom tag labeled with @CustomTag). function createMain(urls, mainUrl) { var imports = Array(urls.length + 1); for (var i = 0; i < urls.length; ++i) { imports[i] = 'import "' + urls[i] + '" as i' + i + ';'; } imports[urls.length] = 'import "package:polymer/polymer.dart" as polymer;'; var arg = urls.length == 0 ? '[]' : ('[\n "' + urls.join('",\n "') + '"\n ]'); return (imports.join('\n') + '\n\nmain() {\n' + ' polymer.initPolymer(' + arg + ');\n' + '}\n'); } // Finds all top-level