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

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

Issue 23445009: Prune the old deploy code. This CL does a few changes: (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 {
25 /** Only run this transformer on .html files. */
26 final String allowedExtensions = ".html";
27
28 Future apply(Transform transform) {
29 var id = transform.primaryId;
30 return getPrimaryContent(transform).then((content) {
31 var document = parseHtml(content, id.path, transform.logger);
32 bool shadowDomFound = false;
33 bool jsInteropFound = false;
34 bool dartScriptTags = false;
35
36 for (var tag in document.queryAll('script')) {
37 var src = tag.attributes['src'];
38 if (src != null) {
39 var last = src.split('/').last;
40 if (last == 'interop.js') {
41 jsInteropFound = true;
42 } else if (_shadowDomJS.hasMatch(last)) {
43 shadowDomFound = true;
44 }
45 }
46
47 if (tag.attributes['type'] == 'application/dart') {
48 dartScriptTags = true;
49 }
50 }
51
52 if (!dartScriptTags) {
53 // This HTML has no Dart code, there is nothing to do here.
54 transform.addOutput(new Asset.fromString(id, content));
55 return;
56 }
57
58 if (!jsInteropFound) {
59 // JS interop code is required for Polymer CSS shimming.
60 document.body.nodes.insert(0, parseFragment(
61 '<script type="text/javascript" '
Jennifer Messerly 2013/08/28 22:25:45 could we leave off the type here?
Siggi Cherem (dart-lang) 2013/08/28 23:17:06 Done.
62 'src="packages/browser/interop.js"></script>\n'));
63 }
64
65 if (!shadowDomFound) {
66 // Insert at the beginning (this polyfill needs to run as early as
67 // possible).
68 document.body.nodes.insert(0, parseFragment(
69 '<script type="text/javascript" '
70 'src="packages/shadow_dom/shadow_dom.min.js"></script>\n'));
71 }
72
73 transform.addOutput(new Asset.fromString(id, document.outerHtml));
74 });
75 }
76 }
77
78 final _shadowDomJS = new RegExp(r'shadow_dom\..*\.js', caseSensitive: false);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698