Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: pkg/polymer/lib/src/transform/polyfill_injector.dart

Issue 23757034: Rearranges the polymer package now that the old compiler is gone. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698