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 /// Transfomer that inlines polymer-element definitions from html imports. | 5 /// Transfomer that inlines polymer-element definitions from html imports. |
6 library polymer.src.build.import_inliner; | 6 library polymer.src.build.import_inliner; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:convert'; | 9 import 'dart:convert'; |
10 | 10 |
11 import 'package:analyzer/analyzer.dart'; | 11 import 'package:analyzer/analyzer.dart'; |
12 import 'package:analyzer/src/generated/ast.dart'; | 12 import 'package:analyzer/src/generated/ast.dart'; |
13 import 'package:barback/barback.dart'; | 13 import 'package:barback/barback.dart'; |
14 import 'package:code_transformers/assets.dart'; | 14 import 'package:code_transformers/assets.dart'; |
15 import 'package:path/path.dart' as path; | 15 import 'package:path/path.dart' as path; |
16 import 'package:html5lib/dom.dart' show | 16 import 'package:html5lib/dom.dart' show |
17 Document, DocumentFragment, Element, Node; | 17 Document, DocumentFragment, Element, Node; |
18 import 'package:html5lib/dom_parsing.dart' show TreeVisitor; | 18 import 'package:html5lib/dom_parsing.dart' show TreeVisitor; |
19 import 'package:source_maps/refactor.dart' show TextEditTransaction; | 19 import 'package:source_maps/refactor.dart' show TextEditTransaction; |
20 import 'package:source_span/source_span.dart'; | 20 import 'package:source_span/source_span.dart'; |
21 | 21 |
22 import 'common.dart'; | 22 import 'common.dart'; |
| 23 import 'wrapped_logger.dart'; |
23 | 24 |
24 // TODO(sigmund): move to web_components package (dartbug.com/18037). | 25 // TODO(sigmund): move to web_components package (dartbug.com/18037). |
25 class _HtmlInliner extends PolymerTransformer { | 26 class _HtmlInliner extends PolymerTransformer { |
26 final TransformOptions options; | 27 final TransformOptions options; |
27 final Transform transform; | 28 final Transform transform; |
28 final TransformLogger logger; | 29 final TransformLogger logger; |
29 final AssetId docId; | 30 final AssetId docId; |
30 final seen = new Set<AssetId>(); | 31 final seen = new Set<AssetId>(); |
31 final scriptIds = <AssetId>[]; | 32 final scriptIds = <AssetId>[]; |
32 final extractedFiles = new Set<AssetId>(); | 33 final extractedFiles = new Set<AssetId>(); |
33 bool experimentalBootstrap = false; | 34 bool experimentalBootstrap = false; |
34 | 35 |
35 /// The number of extracted inline Dart scripts. Used as a counter to give | 36 /// The number of extracted inline Dart scripts. Used as a counter to give |
36 /// unique-ish filenames. | 37 /// unique-ish filenames. |
37 int inlineScriptCounter = 0; | 38 int inlineScriptCounter = 0; |
38 | 39 |
39 _HtmlInliner(this.options, Transform transform) | 40 _HtmlInliner(TransformOptions options, Transform transform) |
40 : transform = transform, | 41 : options = options, |
41 logger = transform.logger, | 42 transform = transform, |
| 43 logger = (options.releaseMode) ? transform.logger : |
| 44 new WrappedLogger(transform, convertErrorsToWarnings: true), |
42 docId = transform.primaryInput.id; | 45 docId = transform.primaryInput.id; |
43 | 46 |
44 Future apply() { | 47 Future apply() { |
45 seen.add(docId); | 48 seen.add(docId); |
46 | 49 |
47 Document document; | 50 Document document; |
48 bool changed = false; | 51 bool changed = false; |
49 | 52 |
50 return readPrimaryAsHtml(transform).then((doc) { | 53 return readPrimaryAsHtml(transform).then((doc) { |
51 document = doc; | 54 document = doc; |
52 changed = new _UrlNormalizer(transform, docId).visit(document) || changed; | 55 changed = new _UrlNormalizer(transform, docId, logger).visit(document) |
| 56 || changed; |
53 | 57 |
54 experimentalBootstrap = document.querySelectorAll('link').any((link) => | 58 experimentalBootstrap = document.querySelectorAll('link').any((link) => |
55 link.attributes['rel'] == 'import' && | 59 link.attributes['rel'] == 'import' && |
56 link.attributes['href'] == POLYMER_EXPERIMENTAL_HTML); | 60 link.attributes['href'] == POLYMER_EXPERIMENTAL_HTML); |
57 changed = _extractScripts(document) || changed; | 61 changed = _extractScripts(document) || changed; |
58 return _visitImports(document); | 62 return _visitImports(document); |
59 }).then((importsFound) { | 63 }).then((importsFound) { |
60 changed = changed || importsFound; | 64 changed = changed || importsFound; |
61 return _removeScripts(document); | 65 return _removeScripts(document); |
62 }).then((scriptsRemoved) { | 66 }).then((scriptsRemoved) { |
63 changed = changed || scriptsRemoved; | 67 changed = changed || scriptsRemoved; |
64 | 68 |
65 var output = transform.primaryInput; | 69 var output = transform.primaryInput; |
66 if (changed) output = new Asset.fromString(docId, document.outerHtml); | 70 if (changed) output = new Asset.fromString(docId, document.outerHtml); |
67 transform.addOutput(output); | 71 transform.addOutput(output); |
68 | 72 |
69 // We produce a secondary asset with extra information for later phases. | 73 // We produce a secondary asset with extra information for later phases. |
70 transform.addOutput(new Asset.fromString( | 74 transform.addOutput(new Asset.fromString( |
71 docId.addExtension('._data'), | 75 docId.addExtension('._data'), |
72 JSON.encode({ | 76 JSON.encode({ |
73 'experimental_bootstrap': experimentalBootstrap, | 77 'experimental_bootstrap': experimentalBootstrap, |
74 'script_ids': scriptIds, | 78 'script_ids': scriptIds, |
75 }, toEncodable: (id) => id.serialize()))); | 79 }, toEncodable: (id) => id.serialize()))); |
| 80 |
| 81 // Write out the logs collected by our [WrappedLogger]. |
| 82 if (logger is WrappedLogger) return logger.writeOutput(); |
76 }); | 83 }); |
77 } | 84 } |
78 | 85 |
79 /// Visits imports in [document] and add the imported documents to documents. | 86 /// Visits imports in [document] and add the imported documents to documents. |
80 /// Documents are added in the order they appear, transitive imports are added | 87 /// Documents are added in the order they appear, transitive imports are added |
81 /// first. | 88 /// first. |
82 /// | 89 /// |
83 /// Returns `true` if and only if the document was changed and should be | 90 /// Returns `true` if and only if the document was changed and should be |
84 /// written out. | 91 /// written out. |
85 Future<bool> _visitImports(Document document) { | 92 Future<bool> _visitImports(Document document) { |
86 bool changed = false; | 93 bool changed = false; |
87 | 94 |
88 _moveHeadToBody(document); | 95 _moveHeadToBody(document); |
89 | 96 |
90 // Note: we need to preserve the import order in the generated output. | 97 // Note: we need to preserve the import order in the generated output. |
91 return Future.forEach(document.querySelectorAll('link'), (Element tag) { | 98 return Future.forEach(document.querySelectorAll('link'), (Element tag) { |
92 var rel = tag.attributes['rel']; | 99 var rel = tag.attributes['rel']; |
93 if (rel != 'import' && rel != 'stylesheet') return null; | 100 if (rel != 'import' && rel != 'stylesheet') return null; |
94 | 101 |
95 // Note: URL has already been normalized so use docId. | 102 // Note: URL has already been normalized so use docId. |
96 var href = tag.attributes['href']; | 103 var href = tag.attributes['href']; |
97 var id = uriToAssetId(docId, href, transform.logger, tag.sourceSpan, | 104 var id = uriToAssetId(docId, href, logger, tag.sourceSpan, |
98 errorOnAbsolute: rel != 'stylesheet'); | 105 errorOnAbsolute: rel != 'stylesheet'); |
99 | 106 |
100 if (rel == 'import') { | 107 if (rel == 'import') { |
101 changed = true; | 108 changed = true; |
102 if (id == null || !seen.add(id)) { | 109 if (id == null || !seen.add(id)) { |
103 tag.remove(); | 110 tag.remove(); |
104 return null; | 111 return null; |
105 } | 112 } |
106 return _inlineImport(id, tag); | 113 return _inlineImport(id, tag); |
107 | 114 |
(...skipping 30 matching lines...) Expand all Loading... |
138 // Move the node into the body, where its contents will be placed. | 145 // Move the node into the body, where its contents will be placed. |
139 doc.body.insertBefore(node, insertionPoint); | 146 doc.body.insertBefore(node, insertionPoint); |
140 } | 147 } |
141 } | 148 } |
142 } | 149 } |
143 | 150 |
144 /// Loads an asset identified by [id], visits its imports and collects its | 151 /// Loads an asset identified by [id], visits its imports and collects its |
145 /// html imports. Then inlines it into the main document. | 152 /// html imports. Then inlines it into the main document. |
146 Future _inlineImport(AssetId id, Element link) { | 153 Future _inlineImport(AssetId id, Element link) { |
147 return readAsHtml(id, transform).catchError((error) { | 154 return readAsHtml(id, transform).catchError((error) { |
148 transform.logger.error( | 155 logger.error( |
149 "Failed to inline html import: $error", asset: id, | 156 "Failed to inline html import: $error", asset: id, |
150 span: link.sourceSpan); | 157 span: link.sourceSpan); |
151 }).then((doc) { | 158 }).then((doc) { |
152 if (doc == null) return false; | 159 if (doc == null) return false; |
153 new _UrlNormalizer(transform, id).visit(doc); | 160 new _UrlNormalizer(transform, id, logger).visit(doc); |
154 return _visitImports(doc).then((_) { | 161 return _visitImports(doc).then((_) { |
155 // _UrlNormalizer already ensures there is a library name. | 162 // _UrlNormalizer already ensures there is a library name. |
156 _extractScripts(doc, injectLibraryName: false); | 163 _extractScripts(doc, injectLibraryName: false); |
157 | 164 |
158 // TODO(jmesserly): figure out how this is working in vulcanizer. | 165 // TODO(jmesserly): figure out how this is working in vulcanizer. |
159 // Do they produce a <body> tag with a <head> and <body> inside? | 166 // Do they produce a <body> tag with a <head> and <body> inside? |
160 var imported = new DocumentFragment(); | 167 var imported = new DocumentFragment(); |
161 imported.nodes..addAll(doc.head.nodes)..addAll(doc.body.nodes); | 168 imported.nodes..addAll(doc.head.nodes)..addAll(doc.body.nodes); |
162 link.replaceWith(imported); | 169 link.replaceWith(imported); |
163 }); | 170 }); |
164 }); | 171 }); |
165 } | 172 } |
166 | 173 |
167 Future _inlineStylesheet(AssetId id, Element link) { | 174 Future _inlineStylesheet(AssetId id, Element link) { |
168 return transform.readInputAsString(id).catchError((error) { | 175 return transform.readInputAsString(id).catchError((error) { |
169 // TODO(jakemac): Move this warning to the linter once we can make it run | 176 // TODO(jakemac): Move this warning to the linter once we can make it run |
170 // always (see http://dartbug.com/17199). Then hide this error and replace | 177 // always (see http://dartbug.com/17199). Then hide this error and replace |
171 // with a comment pointing to the linter error (so we don't double warn). | 178 // with a comment pointing to the linter error (so we don't double warn). |
172 transform.logger.warning( | 179 logger.warning( |
173 "Failed to inline stylesheet: $error", asset: id, | 180 "Failed to inline stylesheet: $error", asset: id, |
174 span: link.sourceSpan); | 181 span: link.sourceSpan); |
175 }).then((css) { | 182 }).then((css) { |
176 if (css == null) return; | 183 if (css == null) return; |
177 css = new _UrlNormalizer(transform, id).visitCss(css); | 184 css = new _UrlNormalizer(transform, id, logger).visitCss(css); |
178 var styleElement = new Element.tag('style')..text = css; | 185 var styleElement = new Element.tag('style')..text = css; |
179 // Copy over the extra attributes from the link tag to the style tag. | 186 // Copy over the extra attributes from the link tag to the style tag. |
180 // This adds support for no-shim, shim-shadowdom, etc. | 187 // This adds support for no-shim, shim-shadowdom, etc. |
181 link.attributes.forEach((key, value) { | 188 link.attributes.forEach((key, value) { |
182 if (!IGNORED_LINKED_STYLE_ATTRS.contains(key)) { | 189 if (!IGNORED_LINKED_STYLE_ATTRS.contains(key)) { |
183 styleElement.attributes[key] = value; | 190 styleElement.attributes[key] = value; |
184 } | 191 } |
185 }); | 192 }); |
186 link.replaceWith(styleElement); | 193 link.replaceWith(styleElement); |
187 }); | 194 }); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 /// Counter used to ensure that every library name we inject is unique. | 326 /// Counter used to ensure that every library name we inject is unique. |
320 int _count = 0; | 327 int _count = 0; |
321 | 328 |
322 /// Path to the top level folder relative to the transform primaryInput. | 329 /// Path to the top level folder relative to the transform primaryInput. |
323 /// This should just be some arbitrary # of ../'s. | 330 /// This should just be some arbitrary # of ../'s. |
324 final String topLevelPath; | 331 final String topLevelPath; |
325 | 332 |
326 /// Whether or not the normalizer has changed something in the tree. | 333 /// Whether or not the normalizer has changed something in the tree. |
327 bool changed = false; | 334 bool changed = false; |
328 | 335 |
329 _UrlNormalizer(transform, this.sourceId) | 336 final TransformLogger logger; |
| 337 |
| 338 _UrlNormalizer(transform, this.sourceId, this.logger) |
330 : transform = transform, | 339 : transform = transform, |
331 topLevelPath = | 340 topLevelPath = |
332 '../' * (transform.primaryInput.id.path.split('/').length - 2); | 341 '../' * (transform.primaryInput.id.path.split('/').length - 2); |
333 | 342 |
334 visit(Node node) { | 343 visit(Node node) { |
335 super.visit(node); | 344 super.visit(node); |
336 return changed; | 345 return changed; |
337 } | 346 } |
338 | 347 |
339 visitElement(Element node) { | 348 visitElement(Element node) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 String visitInlineDart(String code) { | 409 String visitInlineDart(String code) { |
401 var unit = parseDirectives(code, suppressErrors: true); | 410 var unit = parseDirectives(code, suppressErrors: true); |
402 var file = new SourceFile(code, url: spanUrlFor(sourceId, transform)); | 411 var file = new SourceFile(code, url: spanUrlFor(sourceId, transform)); |
403 var output = new TextEditTransaction(code, file); | 412 var output = new TextEditTransaction(code, file); |
404 var foundLibraryDirective = false; | 413 var foundLibraryDirective = false; |
405 for (Directive directive in unit.directives) { | 414 for (Directive directive in unit.directives) { |
406 if (directive is UriBasedDirective) { | 415 if (directive is UriBasedDirective) { |
407 var uri = directive.uri.stringValue; | 416 var uri = directive.uri.stringValue; |
408 var span = _getSpan(file, directive.uri); | 417 var span = _getSpan(file, directive.uri); |
409 | 418 |
410 var id = uriToAssetId(sourceId, uri, transform.logger, span, | 419 var id = uriToAssetId(sourceId, uri, logger, span, |
411 errorOnAbsolute: false); | 420 errorOnAbsolute: false); |
412 if (id == null) continue; | 421 if (id == null) continue; |
413 | 422 |
414 var primaryId = transform.primaryInput.id; | 423 var primaryId = transform.primaryInput.id; |
415 var newUri = assetUrlFor(id, primaryId, transform.logger); | 424 var newUri = assetUrlFor(id, primaryId, logger); |
416 if (newUri != uri) { | 425 if (newUri != uri) { |
417 output.edit(span.start.offset, span.end.offset, "'$newUri'"); | 426 output.edit(span.start.offset, span.end.offset, "'$newUri'"); |
418 } | 427 } |
419 } else if (directive is LibraryDirective) { | 428 } else if (directive is LibraryDirective) { |
420 foundLibraryDirective = true; | 429 foundLibraryDirective = true; |
421 } | 430 } |
422 } | 431 } |
423 | 432 |
424 if (!foundLibraryDirective) { | 433 if (!foundLibraryDirective) { |
425 // Ensure all inline scripts also have a library name. | 434 // Ensure all inline scripts also have a library name. |
(...skipping 12 matching lines...) Expand all Loading... |
438 // Uri.parse blows up on invalid characters (like {{). Encoding the uri | 447 // Uri.parse blows up on invalid characters (like {{). Encoding the uri |
439 // allows it to be parsed, which does the correct thing in the general case. | 448 // allows it to be parsed, which does the correct thing in the general case. |
440 // This uri not used to build the new uri, so it never needs to be decoded. | 449 // This uri not used to build the new uri, so it never needs to be decoded. |
441 var uri = Uri.parse(Uri.encodeFull(href)); | 450 var uri = Uri.parse(Uri.encodeFull(href)); |
442 if (uri.isAbsolute) return href; | 451 if (uri.isAbsolute) return href; |
443 if (!uri.scheme.isEmpty) return href; | 452 if (!uri.scheme.isEmpty) return href; |
444 if (!uri.host.isEmpty) return href; | 453 if (!uri.host.isEmpty) return href; |
445 if (uri.path.isEmpty) return href; // Implies standalone ? or # in URI. | 454 if (uri.path.isEmpty) return href; // Implies standalone ? or # in URI. |
446 if (path.isAbsolute(href)) return href; | 455 if (path.isAbsolute(href)) return href; |
447 | 456 |
448 var id = uriToAssetId(sourceId, href, transform.logger, span); | 457 var id = uriToAssetId(sourceId, href, logger, span); |
449 if (id == null) return href; | 458 if (id == null) return href; |
450 var primaryId = transform.primaryInput.id; | 459 var primaryId = transform.primaryInput.id; |
451 | 460 |
452 if (id.path.startsWith('lib/')) { | 461 if (id.path.startsWith('lib/')) { |
453 return '${topLevelPath}packages/${id.package}/${id.path.substring(4)}'; | 462 return '${topLevelPath}packages/${id.package}/${id.path.substring(4)}'; |
454 } | 463 } |
455 | 464 |
456 if (id.path.startsWith('asset/')) { | 465 if (id.path.startsWith('asset/')) { |
457 return '${topLevelPath}assets/${id.package}/${id.path.substring(6)}'; | 466 return '${topLevelPath}assets/${id.package}/${id.path.substring(6)}'; |
458 } | 467 } |
459 | 468 |
460 if (primaryId.package != id.package) { | 469 if (primaryId.package != id.package) { |
461 // Techincally we shouldn't get there | 470 // Techincally we shouldn't get there |
462 transform.logger.error("don't know how to include $id from $primaryId", | 471 logger.error("don't know how to include $id from $primaryId", span: span); |
463 span: span); | |
464 return href; | 472 return href; |
465 } | 473 } |
466 | 474 |
467 var builder = path.url; | 475 var builder = path.url; |
468 return builder.relative(builder.join('/', id.path), | 476 return builder.relative(builder.join('/', id.path), |
469 from: builder.join('/', builder.dirname(primaryId.path))); | 477 from: builder.join('/', builder.dirname(primaryId.path))); |
470 } | 478 } |
471 } | 479 } |
472 | 480 |
473 /// HTML attributes that expect a URL value. | 481 /// HTML attributes that expect a URL value. |
(...skipping 20 matching lines...) Expand all Loading... |
494 /// When inlining <link rel="stylesheet"> tags copy over all attributes to the | 502 /// When inlining <link rel="stylesheet"> tags copy over all attributes to the |
495 /// style tag except these ones. | 503 /// style tag except these ones. |
496 const IGNORED_LINKED_STYLE_ATTRS = | 504 const IGNORED_LINKED_STYLE_ATTRS = |
497 const ['charset', 'href', 'href-lang', 'rel', 'rev']; | 505 const ['charset', 'href', 'href-lang', 'rel', 'rev']; |
498 | 506 |
499 /// Global RegExp objects for validating generated library names. | 507 /// Global RegExp objects for validating generated library names. |
500 final INVALID_LIB_CHARS_REGEX = new RegExp('[^a-z0-9_]'); | 508 final INVALID_LIB_CHARS_REGEX = new RegExp('[^a-z0-9_]'); |
501 final NUM_REGEX = new RegExp('[0-9]'); | 509 final NUM_REGEX = new RegExp('[0-9]'); |
502 | 510 |
503 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end); | 511 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end); |
OLD | NEW |