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

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

Issue 394523002: Sanitize library names generated from file paths (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fix formatting and update comments 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 | « no previous file | 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 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 /// Transform AssetId into a library name. For example: 239 /// Transform AssetId into a library name. For example:
240 /// 240 ///
241 /// myPkgName|lib/foo/bar.html -> myPkgName.foo.bar_html 241 /// myPkgName|lib/foo/bar.html -> myPkgName.foo.bar_html
242 /// myPkgName|web/foo/bar.html -> myPkgName.web.foo.bar_html 242 /// myPkgName|web/foo/bar.html -> myPkgName.web.foo.bar_html
243 /// 243 ///
244 /// This should roughly match the recommended library name conventions. 244 /// This should roughly match the recommended library name conventions.
245 String _libraryNameFor(AssetId id, int suffix) { 245 String _libraryNameFor(AssetId id, int suffix) {
246 var name = '${path.withoutExtension(id.path)}_' 246 var name = '${path.withoutExtension(id.path)}_'
247 '${path.extension(id.path).substring(1)}'; 247 '${path.extension(id.path).substring(1)}';
248 if (name.startsWith('lib/')) name = name.substring(4); 248 if (name.startsWith('lib/')) name = name.substring(4);
249 name = name.replaceAll('/', '.').replaceAll('-', '_'); 249 name = name.split('/').map((part) {
250 part = part.replaceAll(INVALID_LIB_CHARS_REGEX, '_');
251 if (part.startsWith(NUM_REGEX)) part = '_${part}';
252 return part;
253 }).join(".");
250 return '${id.package}.${name}_$suffix'; 254 return '${id.package}.${name}_$suffix';
251 } 255 }
252 256
253 /// Parse [code] and determine whether it has a library directive. 257 /// Parse [code] and determine whether it has a library directive.
254 bool _hasLibraryDirective(String code) => 258 bool _hasLibraryDirective(String code) =>
255 parseDirectives(code, suppressErrors: true) 259 parseDirectives(code, suppressErrors: true)
256 .directives.any((d) => d is LibraryDirective); 260 .directives.any((d) => d is LibraryDirective);
257 261
258 262
259 /// Recursively inlines the contents of HTML imports. Produces as output a 263 /// Recursively inlines the contents of HTML imports. Produces as output a
(...skipping 26 matching lines...) Expand all
286 290
287 /// Internally adjusts urls in the html that we are about to inline. 291 /// Internally adjusts urls in the html that we are about to inline.
288 class _UrlNormalizer extends TreeVisitor { 292 class _UrlNormalizer extends TreeVisitor {
289 final Transform transform; 293 final Transform transform;
290 294
291 /// Asset where the original content (and original url) was found. 295 /// Asset where the original content (and original url) was found.
292 final AssetId sourceId; 296 final AssetId sourceId;
293 297
294 /// Counter used to ensure that every library name we inject is unique. 298 /// Counter used to ensure that every library name we inject is unique.
295 int _count = 0; 299 int _count = 0;
296 300
297 /// Path to the top level folder relative to the transform primaryInput. 301 /// Path to the top level folder relative to the transform primaryInput.
298 /// This should just be some arbitrary # of ../'s. 302 /// This should just be some arbitrary # of ../'s.
299 final String topLevelPath; 303 final String topLevelPath;
300 304
301 _UrlNormalizer(transform, this.sourceId) 305 _UrlNormalizer(transform, this.sourceId)
302 : transform = transform, 306 : transform = transform,
303 topLevelPath = 307 topLevelPath =
304 '../' * (transform.primaryInput.id.path.split('/').length - 2); 308 '../' * (transform.primaryInput.id.path.split('/').length - 2);
305 309
306 visitElement(Element node) { 310 visitElement(Element node) {
307 // TODO(jakemac): Support custom elements that extend html elements which 311 // TODO(jakemac): Support custom elements that extend html elements which
308 // have url-like attributes. This probably means keeping a list of which 312 // have url-like attributes. This probably means keeping a list of which
309 // html elements support each url-like attribute. 313 // html elements support each url-like attribute.
310 if (!isCustomTagName(node.localName)) { 314 if (!isCustomTagName(node.localName)) {
311 node.attributes.forEach((name, value) { 315 node.attributes.forEach((name, value) {
312 if (_urlAttributes.contains(name)) { 316 if (_urlAttributes.contains(name)) {
313 if (value != '' && !value.trim().startsWith('{{')) { 317 if (value != '' && !value.trim().startsWith('{{')) {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 'href', // in a, area, link, base, command 436 'href', // in a, area, link, base, command
433 'icon', // in command 437 'icon', // in command
434 'manifest', // in html 438 'manifest', // in html
435 'poster', // in video 439 'poster', // in video
436 'src', // in audio, embed, iframe, img, input, script, source, track, 440 'src', // in audio, embed, iframe, img, input, script, source, track,
437 // video 441 // video
438 ]; 442 ];
439 443
440 /// When inlining <link rel="stylesheet"> tags copy over all attributes to the 444 /// When inlining <link rel="stylesheet"> tags copy over all attributes to the
441 /// style tag except these ones. 445 /// style tag except these ones.
442 const IGNORED_LINKED_STYLE_ATTRS = 446 const IGNORED_LINKED_STYLE_ATTRS =
443 const ['charset', 'href', 'href-lang', 'rel', 'rev']; 447 const ['charset', 'href', 'href-lang', 'rel', 'rev'];
444 448
449 /// Global RegExp objects for validating generated library names.
450 final INVALID_LIB_CHARS_REGEX = new RegExp('[^a-z0-9_]');
451 final NUM_REGEX = new RegExp('[0-9]');
452
445 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end); 453 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end);
OLDNEW
« no previous file with comments | « no previous file | pkg/polymer/test/build/import_inliner_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698