Index: pkg/path/lib/path.dart |
diff --git a/pkg/path/lib/path.dart b/pkg/path/lib/path.dart |
index 25ce0e6fb850199240b4d00d90f72bd92a12449c..e56eb596b03e01c697ce140fa3cc2f2b6f5943f2 100644 |
--- a/pkg/path/lib/path.dart |
+++ b/pkg/path/lib/path.dart |
@@ -114,6 +114,17 @@ String basenameWithoutExtension(String path) => |
/// Trailing separators are ignored. |
/// |
/// builder.dirname('path/to/'); // -> 'path' |
+/// |
+/// If an absolute path contains no directories, only a root, then the root |
+/// is returned. |
+/// |
+/// path.dirname('/'); // -> '/' (posix) |
+/// path.dirname('c:\'); // -> 'c:\' (windows) |
+/// |
+/// If a relative path has no directories, then '.' is returned. |
+/// |
+/// path.dirname('foo'); // -> '.' |
+/// path.dirname(''); // -> '.' |
String dirname(String path) => _builder.dirname(path); |
/// Gets the file extension of [path]: the portion of [basename] from the last |
@@ -588,8 +599,6 @@ class Builder { |
/// |
/// builder.normalize('path/./to/..//file.text'); // -> 'path/file.txt' |
String normalize(String path) { |
- if (path == '') return path; |
- |
var parsed = _parse(path); |
parsed.normalize(); |
return parsed.toString(); |
@@ -630,8 +639,6 @@ class Builder { |
/// var builder = new Builder(r'some/relative/path'); |
/// builder.relative(r'/absolute/path'); // -> '/absolute/path' |
String relative(String path, {String from}) { |
- if (path == '') return '.'; |
- |
from = from == null ? root : this.join(root, from); |
// We can't determine the path from a relative path to an absolute path. |
@@ -678,8 +685,12 @@ class Builder { |
pathParsed.separators.removeAt(1); |
} |
- // If there are any directories left in the root path, we need to walk up |
- // out of them. |
+ // If there are any directories left in the from path, we need to walk up |
+ // out of them. If a directory left in the from path is '..', it cannot |
+ // be cancelled by adding a '..'. |
+ if (fromParsed.parts.length > 0 && fromParsed.parts[0] == '..') { |
+ throw new ArgumentError('Unable to find a path to "$path" from "$from".'); |
+ } |
_growListFront(pathParsed.parts, fromParsed.parts.length, '..'); |
pathParsed.separators[0] = ''; |
pathParsed.separators.insertAll(1, |
@@ -688,6 +699,13 @@ class Builder { |
// Corner case: the paths completely collapsed. |
if (pathParsed.parts.length == 0) return '.'; |
+ // Corner case: path was '.' and some '..' directories were added in front. |
+ // Don't add a final '/.' in that case. |
+ if (pathParsed.parts.length > 1 && pathParsed.parts.last == '.') { |
+ pathParsed.parts.removeLast(); |
+ pathParsed.separators..removeLast()..removeLast()..add(''); |
+ } |
+ |
// Make it relative. |
pathParsed.root = ''; |
pathParsed.removeTrailingSeparators(); |
@@ -1046,7 +1064,8 @@ class _ParsedPath { |
return copy._splitExtension()[0]; |
} |
- bool get hasTrailingSeparator => !parts.isEmpty && (parts.last == '' || separators.last != ''); |
+ bool get hasTrailingSeparator => |
+ !parts.isEmpty && (parts.last == '' || separators.last != ''); |
void removeTrailingSeparators() { |
while (!parts.isEmpty && parts.last == '') { |