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

Unified Diff: pkg/barback/lib/src/asset_id.dart

Issue 22887039: Normalize incoming AssetID paths. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise. Created 7 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/barback/test/asset_id_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/barback/lib/src/asset_id.dart
diff --git a/pkg/barback/lib/src/asset_id.dart b/pkg/barback/lib/src/asset_id.dart
index c4ed1d86cd1986e4d435ca4cc1faafa7e6506c8c..e09126499cbcc0523e5908bc7117db8d2d97e1c6 100644
--- a/pkg/barback/lib/src/asset_id.dart
+++ b/pkg/barback/lib/src/asset_id.dart
@@ -6,9 +6,6 @@ library barback.asset_id;
import 'package:path/path.dart' as pathos;
-/// AssetIDs always use POSIX style paths regardless of the host platform.
-final _posix = new pathos.Builder(style: pathos.Style.posix);
-
/// Identifies an asset within a package.
class AssetId implements Comparable<AssetId> {
/// The name of the package containing this asset.
@@ -29,10 +26,18 @@ class AssetId implements Comparable<AssetId> {
String get extension => pathos.extension(path);
/// Creates a new AssetId at [path] within [package].
+ ///
+ /// The [path] will be normalized: any backslashes will be replaced with
+ /// forward slashes (regardless of host OS) and "." and ".." will be removed
+ /// where possible.
AssetId(this.package, String path)
- : path = _posix.normalize(path);
+ : path = _normalizePath(path);
/// Parses an [AssetId] string of the form "package|path/to/asset.txt".
+ ///
+ /// The [path] will be normalized: any backslashes will be replaced with
+ /// forward slashes (regardless of host OS) and "." and ".." will be removed
+ /// where possible.
factory AssetId.parse(String description) {
var parts = description.split("|");
if (parts.length != 2) {
@@ -92,3 +97,15 @@ class AssetId implements Comparable<AssetId> {
/// and passed to [deserialize].
serialize() => [package, path];
}
+
+String _normalizePath(String path) {
+ if (pathos.isAbsolute(path)) {
+ throw new ArgumentError('Asset paths must be relative, but got "$path".');
+ }
+
+ // Normalize path separators so that they are always "/" in the AssetID.
+ path = path.replaceAll(r"\", "/");
+
+ // Collapse "." and "..".
+ return pathos.posix.normalize(path);
+}
« no previous file with comments | « no previous file | pkg/barback/test/asset_id_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698