| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, 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 /** | |
| 6 * Final phase of the polymer transformation: includes any additional polyfills | |
| 7 * that may needed by the deployed app. | |
| 8 */ | |
| 9 library polymer.src.transform.polyfill_injector; | |
| 10 | |
| 11 import 'dart:async'; | |
| 12 | |
| 13 import 'package:barback/barback.dart'; | |
| 14 import 'package:html5lib/dom.dart' show Document, Node, DocumentFragment; | |
| 15 import 'package:html5lib/parser.dart' show parseFragment; | |
| 16 import 'common.dart'; | |
| 17 | |
| 18 /** | |
| 19 * Ensures that any scripts and polyfills needed to run a polymer application | |
| 20 * are included. For example, this transformer will ensure that there is a | |
| 21 * script tag that loads the shadow_dom polyfill and interop.js (used for the | |
| 22 * css shimming). | |
| 23 */ | |
| 24 class PolyfillInjector extends Transformer with PolymerTransformer { | |
| 25 final TransformOptions options; | |
| 26 | |
| 27 PolyfillInjector(this.options); | |
| 28 | |
| 29 /** Only run on entry point .html files. */ | |
| 30 Future<bool> isPrimary(Asset input) => | |
| 31 new Future.value(options.isHtmlEntryPoint(input.id)); | |
| 32 | |
| 33 Future apply(Transform transform) { | |
| 34 return readPrimaryAsHtml(transform).then((document) { | |
| 35 bool shadowDomFound = false; | |
| 36 bool jsInteropFound = false; | |
| 37 bool pkgJsInteropFound = false; | |
| 38 bool dartScriptTags = false; | |
| 39 | |
| 40 for (var tag in document.queryAll('script')) { | |
| 41 var src = tag.attributes['src']; | |
| 42 if (src != null) { | |
| 43 var last = src.split('/').last; | |
| 44 if (last == 'interop.js') { | |
| 45 jsInteropFound = true; | |
| 46 } else if (last == 'dart_interop.js') { | |
| 47 pkgJsInteropFound = true; | |
| 48 } else if (_shadowDomJS.hasMatch(last)) { | |
| 49 shadowDomFound = true; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 if (tag.attributes['type'] == 'application/dart') { | |
| 54 dartScriptTags = true; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 if (!dartScriptTags) { | |
| 59 // This HTML has no Dart code, there is nothing to do here. | |
| 60 transform.addOutput(transform.primaryInput); | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 if (!pkgJsInteropFound) { | |
| 65 // JS interop code is required for Polymer CSS shimming. | |
| 66 document.body.nodes.insert(0, parseFragment( | |
| 67 '<script src="packages/js/dart_interop.js"></script>\n')); | |
| 68 } | |
| 69 | |
| 70 if (!jsInteropFound) { | |
| 71 // JS interop code is required for Polymer CSS shimming. | |
| 72 document.body.nodes.insert(0, parseFragment( | |
| 73 '<script src="packages/browser/interop.js"></script>\n')); | |
| 74 } | |
| 75 | |
| 76 if (!shadowDomFound) { | |
| 77 // Insert at the beginning (this polyfill needs to run as early as | |
| 78 // possible). | |
| 79 // TODO(jmesserly): this is .debug to workaround issue 13046. | |
| 80 document.body.nodes.insert(0, parseFragment( | |
| 81 '<script src="packages/shadow_dom/shadow_dom.debug.js"></script>\n')
); | |
| 82 } | |
| 83 | |
| 84 transform.addOutput( | |
| 85 new Asset.fromString(transform.primaryInput.id, document.outerHtml)); | |
| 86 }); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 final _shadowDomJS = new RegExp(r'shadow_dom\..*\.js', caseSensitive: false); | |
| OLD | NEW |