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

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

Issue 404623002: normalize url attributes on entry point elements (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 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/.gitignore ('k') | pkg/polymer/test/build/import_inliner_test.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
(...skipping 27 matching lines...) Expand all
38 38
39 _HtmlInliner(this.options, Transform transform) 39 _HtmlInliner(this.options, Transform transform)
40 : transform = transform, 40 : transform = transform,
41 logger = transform.logger, 41 logger = transform.logger,
42 docId = transform.primaryInput.id; 42 docId = transform.primaryInput.id;
43 43
44 Future apply() { 44 Future apply() {
45 seen.add(docId); 45 seen.add(docId);
46 46
47 Document document; 47 Document document;
48 bool changed; 48 bool changed = false;
49 49
50 return readPrimaryAsHtml(transform).then((doc) { 50 return readPrimaryAsHtml(transform).then((doc) {
51 document = doc; 51 document = doc;
52 return new _UrlNormalizer(transform, docId).visit(document);
53 }).then((urlsNormalized) {
Siggi Cherem (dart-lang) 2014/07/17 22:31:50 since the normalization is synchronous, we can jus
jakemac 2014/07/17 22:36:40 Done.
54 changed = changed || urlsNormalized;
52 experimentalBootstrap = document.querySelectorAll('link').any((link) => 55 experimentalBootstrap = document.querySelectorAll('link').any((link) =>
53 link.attributes['rel'] == 'import' && 56 link.attributes['rel'] == 'import' &&
54 link.attributes['href'] == POLYMER_EXPERIMENTAL_HTML); 57 link.attributes['href'] == POLYMER_EXPERIMENTAL_HTML);
55 changed = _extractScripts(document); 58 changed = _extractScripts(document) || changed;
56 return _visitImports(document); 59 return _visitImports(document);
57 }).then((importsFound) { 60 }).then((importsFound) {
58 changed = changed || importsFound; 61 changed = changed || importsFound;
59 return _removeScripts(document); 62 return _removeScripts(document);
60 }).then((scriptsRemoved) { 63 }).then((scriptsRemoved) {
61 changed = changed || scriptsRemoved; 64 changed = changed || scriptsRemoved;
62 65
63 var output = transform.primaryInput; 66 var output = transform.primaryInput;
64 if (changed) output = new Asset.fromString(docId, document.outerHtml); 67 if (changed) output = new Asset.fromString(docId, document.outerHtml);
65 transform.addOutput(output); 68 transform.addOutput(output);
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 /// Asset where the original content (and original url) was found. 298 /// Asset where the original content (and original url) was found.
296 final AssetId sourceId; 299 final AssetId sourceId;
297 300
298 /// Counter used to ensure that every library name we inject is unique. 301 /// Counter used to ensure that every library name we inject is unique.
299 int _count = 0; 302 int _count = 0;
300 303
301 /// Path to the top level folder relative to the transform primaryInput. 304 /// Path to the top level folder relative to the transform primaryInput.
302 /// This should just be some arbitrary # of ../'s. 305 /// This should just be some arbitrary # of ../'s.
303 final String topLevelPath; 306 final String topLevelPath;
304 307
308 /// Whether or not the normalizer has changed something in the tree.
309 bool changed = false;
310
305 _UrlNormalizer(transform, this.sourceId) 311 _UrlNormalizer(transform, this.sourceId)
306 : transform = transform, 312 : transform = transform,
307 topLevelPath = 313 topLevelPath =
308 '../' * (transform.primaryInput.id.path.split('/').length - 2); 314 '../' * (transform.primaryInput.id.path.split('/').length - 2);
309 315
316 visit(Element node) {
317 super.visit(node);
318 return changed;
319 }
320
310 visitElement(Element node) { 321 visitElement(Element node) {
311 // TODO(jakemac): Support custom elements that extend html elements which 322 // TODO(jakemac): Support custom elements that extend html elements which
312 // have url-like attributes. This probably means keeping a list of which 323 // have url-like attributes. This probably means keeping a list of which
313 // html elements support each url-like attribute. 324 // html elements support each url-like attribute.
314 if (!isCustomTagName(node.localName)) { 325 if (!isCustomTagName(node.localName)) {
315 node.attributes.forEach((name, value) { 326 node.attributes.forEach((name, value) {
316 if (_urlAttributes.contains(name)) { 327 if (_urlAttributes.contains(name)) {
317 if (value != '' && !value.trim().startsWith('{{')) { 328 if (value != '' && !value.trim().startsWith('{{')) {
318 node.attributes[name] = _newUrl(value, node.sourceSpan); 329 node.attributes[name] = _newUrl(value, node.sourceSpan);
330 changed = changed || value != node.attributes[name];
319 } 331 }
320 } 332 }
321 }); 333 });
322 } 334 }
323 if (node.localName == 'style') { 335 if (node.localName == 'style') {
324 node.text = visitCss(node.text); 336 node.text = visitCss(node.text);
337 changed = true;
325 } else if (node.localName == 'script' && 338 } else if (node.localName == 'script' &&
326 node.attributes['type'] == TYPE_DART && 339 node.attributes['type'] == TYPE_DART &&
327 !node.attributes.containsKey('src')) { 340 !node.attributes.containsKey('src')) {
328 // TODO(jmesserly): we might need to visit JS too to handle ES Harmony 341 // TODO(jmesserly): we might need to visit JS too to handle ES Harmony
329 // modules. 342 // modules.
330 node.text = visitInlineDart(node.text); 343 node.text = visitInlineDart(node.text);
344 changed = true;
331 } 345 }
332 super.visitElement(node); 346 return super.visitElement(node);
333 } 347 }
334 348
335 static final _URL = new RegExp(r'url\(([^)]*)\)', multiLine: true); 349 static final _URL = new RegExp(r'url\(([^)]*)\)', multiLine: true);
336 static final _QUOTE = new RegExp('["\']', multiLine: true); 350 static final _QUOTE = new RegExp('["\']', multiLine: true);
337 351
338 /// Visit the CSS text and replace any relative URLs so we can inline it. 352 /// Visit the CSS text and replace any relative URLs so we can inline it.
339 // Ported from: 353 // Ported from:
340 // https://github.com/Polymer/vulcanize/blob/c14f63696797cda18dc3d372b78aa3378 acc691f/lib/vulcan.js#L149 354 // https://github.com/Polymer/vulcanize/blob/c14f63696797cda18dc3d372b78aa3378 acc691f/lib/vulcan.js#L149
341 // TODO(jmesserly): use csslib here instead? Parsing with RegEx is sadness. 355 // TODO(jmesserly): use csslib here instead? Parsing with RegEx is sadness.
342 // Maybe it's reliable enough for finding URLs in CSS? I'm not sure. 356 // Maybe it's reliable enough for finding URLs in CSS? I'm not sure.
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 /// When inlining <link rel="stylesheet"> tags copy over all attributes to the 458 /// When inlining <link rel="stylesheet"> tags copy over all attributes to the
445 /// style tag except these ones. 459 /// style tag except these ones.
446 const IGNORED_LINKED_STYLE_ATTRS = 460 const IGNORED_LINKED_STYLE_ATTRS =
447 const ['charset', 'href', 'href-lang', 'rel', 'rev']; 461 const ['charset', 'href', 'href-lang', 'rel', 'rev'];
448 462
449 /// Global RegExp objects for validating generated library names. 463 /// Global RegExp objects for validating generated library names.
450 final INVALID_LIB_CHARS_REGEX = new RegExp('[^a-z0-9_]'); 464 final INVALID_LIB_CHARS_REGEX = new RegExp('[^a-z0-9_]');
451 final NUM_REGEX = new RegExp('[0-9]'); 465 final NUM_REGEX = new RegExp('[0-9]');
452 466
453 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end); 467 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end);
OLDNEW
« no previous file with comments | « pkg/.gitignore ('k') | pkg/polymer/test/build/import_inliner_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698