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

Side by Side Diff: pkg/polymer/lib/src/build/import_inliner.dart

Issue 196943024: Adding some asset-related utilities to code_transformers (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 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
« no previous file with comments | « pkg/polymer/lib/src/build/common.dart ('k') | pkg/polymer/lib/src/build/linter.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 /// 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/src/generated/ast.dart'; 11 import 'package:analyzer/src/generated/ast.dart';
12 import 'package:barback/barback.dart'; 12 import 'package:barback/barback.dart';
13 import 'package:code_transformers/assets.dart';
13 import 'package:path/path.dart' as path; 14 import 'package:path/path.dart' as path;
14 import 'package:html5lib/dom.dart' show 15 import 'package:html5lib/dom.dart' show
15 Document, DocumentFragment, Element, Node; 16 Document, DocumentFragment, Element, Node;
16 import 'package:html5lib/dom_parsing.dart' show TreeVisitor; 17 import 'package:html5lib/dom_parsing.dart' show TreeVisitor;
17 import 'package:source_maps/refactor.dart' show TextEditTransaction; 18 import 'package:source_maps/refactor.dart' show TextEditTransaction;
18 import 'package:source_maps/span.dart'; 19 import 'package:source_maps/span.dart';
19 20
20 import 'common.dart'; 21 import 'common.dart';
21 22
22 class _HtmlInliner extends PolymerTransformer { 23 class _HtmlInliner extends PolymerTransformer {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 74
74 _moveHeadToBody(document); 75 _moveHeadToBody(document);
75 76
76 // Note: we need to preserve the import order in the generated output. 77 // Note: we need to preserve the import order in the generated output.
77 return Future.forEach(document.querySelectorAll('link'), (Element tag) { 78 return Future.forEach(document.querySelectorAll('link'), (Element tag) {
78 var rel = tag.attributes['rel']; 79 var rel = tag.attributes['rel'];
79 if (rel != 'import' && rel != 'stylesheet') return null; 80 if (rel != 'import' && rel != 'stylesheet') return null;
80 81
81 // Note: URL has already been normalized so use docId. 82 // Note: URL has already been normalized so use docId.
82 var href = tag.attributes['href']; 83 var href = tag.attributes['href'];
83 var id = resolve(docId, href, transform.logger, tag.sourceSpan, 84 var id = uriToAssetId(docId, href, transform.logger, tag.sourceSpan,
84 allowAbsolute: rel == 'stylesheet'); 85 errorOnAbsolute: rel != 'stylesheet');
85 86
86 if (rel == 'import') { 87 if (rel == 'import') {
87 changed = true; 88 changed = true;
88 if (id == null || !seen.add(id)) { 89 if (id == null || !seen.add(id)) {
89 tag.remove(); 90 tag.remove();
90 return null; 91 return null;
91 } 92 }
92 return _inlineImport(id, tag); 93 return _inlineImport(id, tag);
93 94
94 } else if (rel == 'stylesheet') { 95 } else if (rel == 'stylesheet') {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 /// Remove scripts from HTML imports, and remember their [AssetId]s for later 156 /// Remove scripts from HTML imports, and remember their [AssetId]s for later
156 /// use. 157 /// use.
157 /// 158 ///
158 /// Dartium only allows a single script tag per page, so we can't inline 159 /// Dartium only allows a single script tag per page, so we can't inline
159 /// the script tags. Instead we remove them entirely. 160 /// the script tags. Instead we remove them entirely.
160 void _removeScripts(Document doc) { 161 void _removeScripts(Document doc) {
161 for (var script in doc.querySelectorAll('script')) { 162 for (var script in doc.querySelectorAll('script')) {
162 if (script.attributes['type'] == TYPE_DART) { 163 if (script.attributes['type'] == TYPE_DART) {
163 script.remove(); 164 script.remove();
164 var src = script.attributes['src']; 165 var src = script.attributes['src'];
165 scriptIds.add(resolve(docId, src, logger, script.sourceSpan)); 166 scriptIds.add(uriToAssetId(docId, src, logger, script.sourceSpan));
166 167
167 // only the first script needs to be added. 168 // only the first script needs to be added.
168 // others are already removed by _extractScripts 169 // others are already removed by _extractScripts
169 return; 170 return;
170 } 171 }
171 } 172 }
172 } 173 }
173 174
174 /// Split inline scripts into their own files. We need to do this for dart2js 175 /// Split inline scripts into their own files. We need to do this for dart2js
175 /// to be able to compile them. 176 /// to be able to compile them.
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 String visitInlineDart(String code) { 312 String visitInlineDart(String code) {
312 var unit = parseCompilationUnit(code); 313 var unit = parseCompilationUnit(code);
313 var file = new SourceFile.text(spanUrlFor(sourceId, transform), code); 314 var file = new SourceFile.text(spanUrlFor(sourceId, transform), code);
314 var output = new TextEditTransaction(code, file); 315 var output = new TextEditTransaction(code, file);
315 316
316 for (Directive directive in unit.directives) { 317 for (Directive directive in unit.directives) {
317 if (directive is UriBasedDirective) { 318 if (directive is UriBasedDirective) {
318 var uri = directive.uri.stringValue; 319 var uri = directive.uri.stringValue;
319 var span = _getSpan(file, directive.uri); 320 var span = _getSpan(file, directive.uri);
320 321
321 var id = resolve(sourceId, uri, transform.logger, span, 322 var id = uriToAssetId(sourceId, uri, transform.logger, span,
322 allowAbsolute: true); 323 errorOnAbsolute: false);
323 if (id == null) continue; 324 if (id == null) continue;
324 325
325 var primaryId = transform.primaryInput.id; 326 var primaryId = transform.primaryInput.id;
326 var newUri = assetUrlFor(id, primaryId, transform.logger); 327 var newUri = assetUrlFor(id, primaryId, transform.logger);
327 if (newUri != uri) { 328 if (newUri != uri) {
328 output.edit(span.start.offset, span.end.offset, "'$newUri'"); 329 output.edit(span.start.offset, span.end.offset, "'$newUri'");
329 } 330 }
330 } 331 }
331 } 332 }
332 333
333 if (!output.hasEdits) return code; 334 if (!output.hasEdits) return code;
334 335
335 // TODO(sigmund): emit source maps when barback supports it (see 336 // TODO(sigmund): emit source maps when barback supports it (see
336 // dartbug.com/12340) 337 // dartbug.com/12340)
337 return (output.commit()..build(file.url)).text; 338 return (output.commit()..build(file.url)).text;
338 } 339 }
339 340
340 String _newUrl(String href, Span span) { 341 String _newUrl(String href, Span span) {
341 var uri = Uri.parse(href); 342 var uri = Uri.parse(href);
342 if (uri.isAbsolute) return href; 343 if (uri.isAbsolute) return href;
343 if (!uri.scheme.isEmpty) return href; 344 if (!uri.scheme.isEmpty) return href;
344 if (!uri.host.isEmpty) return href; 345 if (!uri.host.isEmpty) return href;
345 if (uri.path.isEmpty) return href; // Implies standalone ? or # in URI. 346 if (uri.path.isEmpty) return href; // Implies standalone ? or # in URI.
346 if (path.isAbsolute(href)) return href; 347 if (path.isAbsolute(href)) return href;
347 348
348 var id = resolve(sourceId, href, transform.logger, span); 349 var id = uriToAssetId(sourceId, href, transform.logger, span);
349 if (id == null) return href; 350 if (id == null) return href;
350 var primaryId = transform.primaryInput.id; 351 var primaryId = transform.primaryInput.id;
351 352
352 if (id.path.startsWith('lib/')) { 353 if (id.path.startsWith('lib/')) {
353 return 'packages/${id.package}/${id.path.substring(4)}'; 354 return 'packages/${id.package}/${id.path.substring(4)}';
354 } 355 }
355 356
356 if (id.path.startsWith('asset/')) { 357 if (id.path.startsWith('asset/')) {
357 return 'assets/${id.package}/${id.path.substring(6)}'; 358 return 'assets/${id.package}/${id.path.substring(6)}';
358 } 359 }
(...skipping 24 matching lines...) Expand all
383 'formaction', // in button, input 384 'formaction', // in button, input
384 'href', // in a, area, link, base, command 385 'href', // in a, area, link, base, command
385 'icon', // in command 386 'icon', // in command
386 'manifest', // in html 387 'manifest', // in html
387 'poster', // in video 388 'poster', // in video
388 'src', // in audio, embed, iframe, img, input, script, source, track, 389 'src', // in audio, embed, iframe, img, input, script, source, track,
389 // video 390 // video
390 ]; 391 ];
391 392
392 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end); 393 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end);
OLDNEW
« no previous file with comments | « pkg/polymer/lib/src/build/common.dart ('k') | pkg/polymer/lib/src/build/linter.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698