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

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

Issue 23452010: Add a polymer validator: a linter/analysis that will replace the old (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
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 entrypoint .html files under web or test. */ 25 /** Only run on entrypoint .html files under web or test. */
26 Future<bool> isPrimary(Asset input) => isHtmlInWebOrTest(input.id); 26 Future<bool> isPrimary(Asset input) =>
27 new Future.value(isHtmlInWebOrTest(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 bool shadowDomFound = false; 31 bool shadowDomFound = false;
33 bool jsInteropFound = false; 32 bool jsInteropFound = false;
34 bool dartScriptTags = false; 33 bool dartScriptTags = false;
35 34
36 for (var tag in document.queryAll('script')) { 35 for (var tag in document.queryAll('script')) {
37 var src = tag.attributes['src']; 36 var src = tag.attributes['src'];
38 if (src != null) { 37 if (src != null) {
39 var last = src.split('/').last; 38 var last = src.split('/').last;
40 if (last == 'interop.js') { 39 if (last == 'interop.js') {
41 jsInteropFound = true; 40 jsInteropFound = true;
42 } else if (_shadowDomJS.hasMatch(last)) { 41 } else if (_shadowDomJS.hasMatch(last)) {
43 shadowDomFound = true; 42 shadowDomFound = true;
44 } 43 }
45 } 44 }
46 45
47 if (tag.attributes['type'] == 'application/dart') { 46 if (tag.attributes['type'] == 'application/dart') {
48 dartScriptTags = true; 47 dartScriptTags = true;
49 } 48 }
50 } 49 }
51 50
52 if (!dartScriptTags) { 51 if (!dartScriptTags) {
53 // 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.
54 transform.addOutput(new Asset.fromString(id, content)); 53 transform.addOutput(transform.primaryInput);
55 return; 54 return;
56 } 55 }
57 56
58 if (!jsInteropFound) { 57 if (!jsInteropFound) {
59 // JS interop code is required for Polymer CSS shimming. 58 // JS interop code is required for Polymer CSS shimming.
60 document.body.nodes.insert(0, parseFragment( 59 document.body.nodes.insert(0, parseFragment(
61 '<script src="packages/browser/interop.js"></script>\n')); 60 '<script src="packages/browser/interop.js"></script>\n'));
62 } 61 }
63 62
64 if (!shadowDomFound) { 63 if (!shadowDomFound) {
65 // 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
66 // possible). 65 // possible).
67 document.body.nodes.insert(0, parseFragment( 66 document.body.nodes.insert(0, parseFragment(
68 '<script src="packages/shadow_dom/shadow_dom.min.js"></script>\n')); 67 '<script src="packages/shadow_dom/shadow_dom.min.js"></script>\n'));
69 } 68 }
70 69
71 transform.addOutput(new Asset.fromString(id, document.outerHtml)); 70 transform.addOutput(
71 new Asset.fromString(transform.primaryInput.id, document.outerHtml));
72 }); 72 });
73 } 73 }
74 } 74 }
75 75
76 final _shadowDomJS = new RegExp(r'shadow_dom\..*\.js', caseSensitive: false); 76 final _shadowDomJS = new RegExp(r'shadow_dom\..*\.js', caseSensitive: false);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698