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_maps/span.dart'; | 20 import 'package:source_span/source_span.dart'; |
21 | 21 |
22 import 'common.dart'; | 22 import 'common.dart'; |
23 | 23 |
24 // TODO(sigmund): move to web_components package (dartbug.com/18037). | 24 // TODO(sigmund): move to web_components package (dartbug.com/18037). |
25 class _HtmlInliner extends PolymerTransformer { | 25 class _HtmlInliner extends PolymerTransformer { |
26 final TransformOptions options; | 26 final TransformOptions options; |
27 final Transform transform; | 27 final Transform transform; |
28 final TransformLogger logger; | 28 final TransformLogger logger; |
29 final AssetId docId; | 29 final AssetId docId; |
30 final seen = new Set<AssetId>(); | 30 final seen = new Set<AssetId>(); |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 static final _QUOTE = new RegExp('["\']', multiLine: true); | 368 static final _QUOTE = new RegExp('["\']', multiLine: true); |
369 static final _BINDINGS = new RegExp(r'({{)|(\[\[)'); | 369 static final _BINDINGS = new RegExp(r'({{)|(\[\[)'); |
370 | 370 |
371 /// Visit the CSS text and replace any relative URLs so we can inline it. | 371 /// Visit the CSS text and replace any relative URLs so we can inline it. |
372 // Ported from: | 372 // Ported from: |
373 // https://github.com/Polymer/vulcanize/blob/c14f63696797cda18dc3d372b78aa3378
acc691f/lib/vulcan.js#L149 | 373 // https://github.com/Polymer/vulcanize/blob/c14f63696797cda18dc3d372b78aa3378
acc691f/lib/vulcan.js#L149 |
374 // TODO(jmesserly): use csslib here instead? Parsing with RegEx is sadness. | 374 // TODO(jmesserly): use csslib here instead? Parsing with RegEx is sadness. |
375 // Maybe it's reliable enough for finding URLs in CSS? I'm not sure. | 375 // Maybe it's reliable enough for finding URLs in CSS? I'm not sure. |
376 String visitCss(String cssText) { | 376 String visitCss(String cssText) { |
377 var url = spanUrlFor(sourceId, transform); | 377 var url = spanUrlFor(sourceId, transform); |
378 var src = new SourceFile.text(url, cssText); | 378 var src = new SourceFile(cssText, url: url); |
379 return cssText.replaceAllMapped(_URL, (match) { | 379 return cssText.replaceAllMapped(_URL, (match) { |
380 // Extract the URL, without any surrounding quotes. | 380 // Extract the URL, without any surrounding quotes. |
381 var span = src.span(match.start, match.end); | 381 var span = src.span(match.start, match.end); |
382 var href = match[1].replaceAll(_QUOTE, ''); | 382 var href = match[1].replaceAll(_QUOTE, ''); |
383 href = _newUrl(href, span); | 383 href = _newUrl(href, span); |
384 return 'url($href)'; | 384 return 'url($href)'; |
385 }); | 385 }); |
386 } | 386 } |
387 | 387 |
388 String visitInlineDart(String code) { | 388 String visitInlineDart(String code) { |
389 var unit = parseDirectives(code, suppressErrors: true); | 389 var unit = parseDirectives(code, suppressErrors: true); |
390 var file = new SourceFile.text(spanUrlFor(sourceId, transform), code); | 390 var file = new SourceFile(code, url: spanUrlFor(sourceId, transform)); |
391 var output = new TextEditTransaction(code, file); | 391 var output = new TextEditTransaction(code, file); |
392 var foundLibraryDirective = false; | 392 var foundLibraryDirective = false; |
393 for (Directive directive in unit.directives) { | 393 for (Directive directive in unit.directives) { |
394 if (directive is UriBasedDirective) { | 394 if (directive is UriBasedDirective) { |
395 var uri = directive.uri.stringValue; | 395 var uri = directive.uri.stringValue; |
396 var span = _getSpan(file, directive.uri); | 396 var span = _getSpan(file, directive.uri); |
397 | 397 |
398 var id = uriToAssetId(sourceId, uri, transform.logger, span, | 398 var id = uriToAssetId(sourceId, uri, transform.logger, span, |
399 errorOnAbsolute: false); | 399 errorOnAbsolute: false); |
400 if (id == null) continue; | 400 if (id == null) continue; |
(...skipping 11 matching lines...) Expand all Loading... |
412 if (!foundLibraryDirective) { | 412 if (!foundLibraryDirective) { |
413 // Ensure all inline scripts also have a library name. | 413 // Ensure all inline scripts also have a library name. |
414 var libName = _libraryNameFor(sourceId, _count++); | 414 var libName = _libraryNameFor(sourceId, _count++); |
415 output.edit(0, 0, "library $libName;\n"); | 415 output.edit(0, 0, "library $libName;\n"); |
416 } | 416 } |
417 | 417 |
418 if (!output.hasEdits) return code; | 418 if (!output.hasEdits) return code; |
419 | 419 |
420 // TODO(sigmund): emit source maps when barback supports it (see | 420 // TODO(sigmund): emit source maps when barback supports it (see |
421 // dartbug.com/12340) | 421 // dartbug.com/12340) |
422 return (output.commit()..build(file.url)).text; | 422 return (output.commit()..build(file.url.toString())).text; |
423 } | 423 } |
424 | 424 |
425 String _newUrl(String href, Span span) { | 425 String _newUrl(String href, SourceSpan span) { |
426 // Uri.parse blows up on invalid characters (like {{). Encoding the uri | 426 // Uri.parse blows up on invalid characters (like {{). Encoding the uri |
427 // allows it to be parsed, which does the correct thing in the general case. | 427 // allows it to be parsed, which does the correct thing in the general case. |
428 // This uri not used to build the new uri, so it never needs to be decoded. | 428 // This uri not used to build the new uri, so it never needs to be decoded. |
429 var uri = Uri.parse(Uri.encodeFull(href)); | 429 var uri = Uri.parse(Uri.encodeFull(href)); |
430 if (uri.isAbsolute) return href; | 430 if (uri.isAbsolute) return href; |
431 if (!uri.scheme.isEmpty) return href; | 431 if (!uri.scheme.isEmpty) return href; |
432 if (!uri.host.isEmpty) return href; | 432 if (!uri.host.isEmpty) return href; |
433 if (uri.path.isEmpty) return href; // Implies standalone ? or # in URI. | 433 if (uri.path.isEmpty) return href; // Implies standalone ? or # in URI. |
434 if (path.isAbsolute(href)) return href; | 434 if (path.isAbsolute(href)) return href; |
435 | 435 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 /// When inlining <link rel="stylesheet"> tags copy over all attributes to the | 480 /// When inlining <link rel="stylesheet"> tags copy over all attributes to the |
481 /// style tag except these ones. | 481 /// style tag except these ones. |
482 const IGNORED_LINKED_STYLE_ATTRS = | 482 const IGNORED_LINKED_STYLE_ATTRS = |
483 const ['charset', 'href', 'href-lang', 'rel', 'rev']; | 483 const ['charset', 'href', 'href-lang', 'rel', 'rev']; |
484 | 484 |
485 /// Global RegExp objects for validating generated library names. | 485 /// Global RegExp objects for validating generated library names. |
486 final INVALID_LIB_CHARS_REGEX = new RegExp('[^a-z0-9_]'); | 486 final INVALID_LIB_CHARS_REGEX = new RegExp('[^a-z0-9_]'); |
487 final NUM_REGEX = new RegExp('[0-9]'); | 487 final NUM_REGEX = new RegExp('[0-9]'); |
488 | 488 |
489 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end); | 489 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end); |
OLD | NEW |