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

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: 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
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.replaceAll('-', '_');
Siggi Cherem (dart-lang) 2014/07/14 19:34:11 alternatively, we could remove this line and make
jakemac 2014/07/14 20:30:24 Actually I kind of like that idea, less likelihood
250 name = name.split('/').map((part) {
251 part = part.replaceAll(INVALID_LIB_CHARS_REGEX, '');
252 if (part.startsWith(NUM_REGEX)) part = '_${part}';
253 return part;
254 }).join(".");
250 return '${id.package}.${name}_$suffix'; 255 return '${id.package}.${name}_$suffix';
251 } 256 }
252 257
253 /// Parse [code] and determine whether it has a library directive. 258 /// Parse [code] and determine whether it has a library directive.
254 bool _hasLibraryDirective(String code) => 259 bool _hasLibraryDirective(String code) =>
255 parseDirectives(code, suppressErrors: true) 260 parseDirectives(code, suppressErrors: true)
256 .directives.any((d) => d is LibraryDirective); 261 .directives.any((d) => d is LibraryDirective);
257 262
258 263
259 /// Recursively inlines the contents of HTML imports. Produces as output a 264 /// Recursively inlines the contents of HTML imports. Produces as output a
(...skipping 26 matching lines...) Expand all
286 291
287 /// Internally adjusts urls in the html that we are about to inline. 292 /// Internally adjusts urls in the html that we are about to inline.
288 class _UrlNormalizer extends TreeVisitor { 293 class _UrlNormalizer extends TreeVisitor {
289 final Transform transform; 294 final Transform transform;
290 295
291 /// Asset where the original content (and original url) was found. 296 /// Asset where the original content (and original url) was found.
292 final AssetId sourceId; 297 final AssetId sourceId;
293 298
294 /// Counter used to ensure that every library name we inject is unique. 299 /// Counter used to ensure that every library name we inject is unique.
295 int _count = 0; 300 int _count = 0;
296 301
297 /// Path to the top level folder relative to the transform primaryInput. 302 /// Path to the top level folder relative to the transform primaryInput.
298 /// This should just be some arbitrary # of ../'s. 303 /// This should just be some arbitrary # of ../'s.
299 final String topLevelPath; 304 final String topLevelPath;
300 305
301 _UrlNormalizer(transform, this.sourceId) 306 _UrlNormalizer(transform, this.sourceId)
302 : transform = transform, 307 : transform = transform,
303 topLevelPath = 308 topLevelPath =
304 '../' * (transform.primaryInput.id.path.split('/').length - 2); 309 '../' * (transform.primaryInput.id.path.split('/').length - 2);
305 310
306 visitElement(Element node) { 311 visitElement(Element node) {
307 // TODO(jakemac): Support custom elements that extend html elements which 312 // TODO(jakemac): Support custom elements that extend html elements which
308 // have url-like attributes. This probably means keeping a list of which 313 // have url-like attributes. This probably means keeping a list of which
309 // html elements support each url-like attribute. 314 // html elements support each url-like attribute.
310 if (!isCustomTagName(node.localName)) { 315 if (!isCustomTagName(node.localName)) {
311 node.attributes.forEach((name, value) { 316 node.attributes.forEach((name, value) {
312 if (_urlAttributes.contains(name)) { 317 if (_urlAttributes.contains(name)) {
313 if (value != '' && !value.trim().startsWith('{{')) { 318 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 437 'href', // in a, area, link, base, command
433 'icon', // in command 438 'icon', // in command
434 'manifest', // in html 439 'manifest', // in html
435 'poster', // in video 440 'poster', // in video
436 'src', // in audio, embed, iframe, img, input, script, source, track, 441 'src', // in audio, embed, iframe, img, input, script, source, track,
437 // video 442 // video
438 ]; 443 ];
439 444
440 /// When inlining <link rel="stylesheet"> tags copy over all attributes to the 445 /// When inlining <link rel="stylesheet"> tags copy over all attributes to the
441 /// style tag except these ones. 446 /// style tag except these ones.
442 const IGNORED_LINKED_STYLE_ATTRS = 447 const IGNORED_LINKED_STYLE_ATTRS =
443 const ['charset', 'href', 'href-lang', 'rel', 'rev']; 448 const ['charset', 'href', 'href-lang', 'rel', 'rev'];
444 449
450 /// Global RegExp objects for validating generated library names.
451 final INVALID_LIB_CHARS_REGEX = new RegExp('[^a-z0-9_]');
452 final NUM_REGEX = new RegExp('[0-9]');
453
445 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end); 454 _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') | pkg/polymer/test/build/import_inliner_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698