| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Final phase of the polymer transformation: includes any additional polyfills | 6 * Final phase of the polymer transformation: includes any additional polyfills |
| 7 * that may needed by the deployed app. | 7 * that may needed by the deployed app. |
| 8 */ | 8 */ |
| 9 library polymer.src.transform.polyfill_injector; | 9 library polymer.src.transform.polyfill_injector; |
| 10 | 10 |
| 11 import 'dart:async'; | 11 import 'dart:async'; |
| 12 | 12 |
| 13 import 'package:barback/barback.dart'; | 13 import 'package:barback/barback.dart'; |
| 14 import 'package:html5lib/dom.dart' show Document, Node, DocumentFragment; | 14 import 'package:html5lib/dom.dart' show Document, Node, DocumentFragment; |
| 15 import 'package:html5lib/parser.dart' show parseFragment; | 15 import 'package:html5lib/parser.dart' show parseFragment; |
| 16 import 'common.dart'; | 16 import 'common.dart'; |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * Ensures that any scripts and polyfills needed to run a polymer application | 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 | 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 | 21 * script tag that loads the shadow_dom polyfill and interop.js (used for the |
| 22 * css shimming). | 22 * css shimming). |
| 23 */ | 23 */ |
| 24 class PolyfillInjector extends Transformer { | 24 class PolyfillInjector extends Transformer { |
| 25 /** Only run on entry point .html files. */ | 25 /** Only run on entry point .html files. */ |
| 26 Future<bool> isPrimary(Asset input) => isPrimaryHtml(input.id); | 26 Future<bool> isPrimary(Asset input) => |
| 27 new Future.value(isPrimaryHtml(input.id)); |
| 27 | 28 |
| 28 Future apply(Transform transform) { | 29 Future apply(Transform transform) { |
| 29 var id = transform.primaryInput.id; | 30 return readPrimaryAsHtml(transform).then((document) { |
| 30 return transform.primaryInput.readAsString().then((content) { | |
| 31 var document = parseHtml(content, id.path, transform.logger, | |
| 32 checkDocType: false); | |
| 33 bool shadowDomFound = false; | 31 bool shadowDomFound = false; |
| 34 bool jsInteropFound = false; | 32 bool jsInteropFound = false; |
| 35 bool dartScriptTags = false; | 33 bool dartScriptTags = false; |
| 36 | 34 |
| 37 for (var tag in document.queryAll('script')) { | 35 for (var tag in document.queryAll('script')) { |
| 38 var src = tag.attributes['src']; | 36 var src = tag.attributes['src']; |
| 39 if (src != null) { | 37 if (src != null) { |
| 40 var last = src.split('/').last; | 38 var last = src.split('/').last; |
| 41 if (last == 'interop.js') { | 39 if (last == 'interop.js') { |
| 42 jsInteropFound = true; | 40 jsInteropFound = true; |
| 43 } else if (_shadowDomJS.hasMatch(last)) { | 41 } else if (_shadowDomJS.hasMatch(last)) { |
| 44 shadowDomFound = true; | 42 shadowDomFound = true; |
| 45 } | 43 } |
| 46 } | 44 } |
| 47 | 45 |
| 48 if (tag.attributes['type'] == 'application/dart') { | 46 if (tag.attributes['type'] == 'application/dart') { |
| 49 dartScriptTags = true; | 47 dartScriptTags = true; |
| 50 } | 48 } |
| 51 } | 49 } |
| 52 | 50 |
| 53 if (!dartScriptTags) { | 51 if (!dartScriptTags) { |
| 54 // This HTML has no Dart code, there is nothing to do here. | 52 // This HTML has no Dart code, there is nothing to do here. |
| 55 transform.addOutput(new Asset.fromString(id, content)); | 53 transform.addOutput(transform.primaryInput); |
| 56 return; | 54 return; |
| 57 } | 55 } |
| 58 | 56 |
| 59 if (!jsInteropFound) { | 57 if (!jsInteropFound) { |
| 60 // JS interop code is required for Polymer CSS shimming. | 58 // JS interop code is required for Polymer CSS shimming. |
| 61 document.body.nodes.insert(0, parseFragment( | 59 document.body.nodes.insert(0, parseFragment( |
| 62 '<script src="packages/browser/interop.js"></script>\n')); | 60 '<script src="packages/browser/interop.js"></script>\n')); |
| 63 } | 61 } |
| 64 | 62 |
| 65 if (!shadowDomFound) { | 63 if (!shadowDomFound) { |
| 66 // Insert at the beginning (this polyfill needs to run as early as | 64 // Insert at the beginning (this polyfill needs to run as early as |
| 67 // possible). | 65 // possible). |
| 68 document.body.nodes.insert(0, parseFragment( | 66 document.body.nodes.insert(0, parseFragment( |
| 69 '<script src="packages/shadow_dom/shadow_dom.min.js"></script>\n')); | 67 '<script src="packages/shadow_dom/shadow_dom.min.js"></script>\n')); |
| 70 } | 68 } |
| 71 | 69 |
| 72 transform.addOutput(new Asset.fromString(id, document.outerHtml)); | 70 transform.addOutput( |
| 71 new Asset.fromString(transform.primaryInput.id, document.outerHtml)); |
| 73 }); | 72 }); |
| 74 } | 73 } |
| 75 } | 74 } |
| 76 | 75 |
| 77 final _shadowDomJS = new RegExp(r'shadow_dom\..*\.js', caseSensitive: false); | 76 final _shadowDomJS = new RegExp(r'shadow_dom\..*\.js', caseSensitive: false); |
| OLD | NEW |