Allow [path.fromUri] to take a string as well as a URI.
R=rnystrom@google.com
Review URL: https://codereview.chromium.org//203673003
git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart/pkg/path@34067 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..0d48c07
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,3 @@
+# 1.1.0
+
+* `path.fromUri` now accepts strings as well as `Uri` objects.
diff --git a/lib/path.dart b/lib/path.dart
index 7ff858c..524f16d 100644
--- a/lib/path.dart
+++ b/lib/path.dart
@@ -322,23 +322,27 @@
/// withoutExtension('path/to/foo.dart'); // -> 'path/to/foo'
String withoutExtension(String path) => _context.withoutExtension(path);
-/// Returns the path represented by [uri].
+/// Returns the path represented by [uri], which may be a [String] or a [Uri].
///
/// For POSIX and Windows styles, [uri] must be a `file:` URI. For the URL
/// style, this will just convert [uri] to a string.
///
/// // POSIX
-/// path.fromUri(Uri.parse('file:///path/to/foo'))
+/// context.fromUri('file:///path/to/foo')
/// // -> '/path/to/foo'
///
/// // Windows
-/// path.fromUri(Uri.parse('file:///C:/path/to/foo'))
+/// context.fromUri('file:///C:/path/to/foo')
/// // -> r'C:\path\to\foo'
///
/// // URL
-/// path.fromUri(Uri.parse('http://dartlang.org/path/to/foo'))
+/// context.fromUri('http://dartlang.org/path/to/foo')
/// // -> 'http://dartlang.org/path/to/foo'
-String fromUri(Uri uri) => _context.fromUri(uri);
+///
+/// If [uri] is relative, a relative path will be returned.
+///
+/// path.fromUri('path/to/foo'); // -> 'path/to/foo'
+String fromUri(uri) => _context.fromUri(uri);
/// Returns the URI that represents [path].
///
diff --git a/lib/src/context.dart b/lib/src/context.dart
index b897c8b..9c8e1e4 100644
--- a/lib/src/context.dart
+++ b/lib/src/context.dart
@@ -423,23 +423,30 @@
return parsed.toString();
}
- /// Returns the path represented by [uri].
+ /// Returns the path represented by [uri], which may be a [String] or a [Uri].
///
/// For POSIX and Windows styles, [uri] must be a `file:` URI. For the URL
/// style, this will just convert [uri] to a string.
///
/// // POSIX
- /// context.fromUri(Uri.parse('file:///path/to/foo'))
+ /// context.fromUri('file:///path/to/foo')
/// // -> '/path/to/foo'
///
/// // Windows
- /// context.fromUri(Uri.parse('file:///C:/path/to/foo'))
+ /// context.fromUri('file:///C:/path/to/foo')
/// // -> r'C:\path\to\foo'
///
/// // URL
- /// context.fromUri(Uri.parse('http://dartlang.org/path/to/foo'))
+ /// context.fromUri('http://dartlang.org/path/to/foo')
/// // -> 'http://dartlang.org/path/to/foo'
- String fromUri(Uri uri) => style.pathFromUri(uri);
+ ///
+ /// If [uri] is relative, a relative path will be returned.
+ ///
+ /// path.fromUri('path/to/foo'); // -> 'path/to/foo'
+ String fromUri(uri) {
+ if (uri is String) uri = Uri.parse(uri);
+ return style.pathFromUri(uri);
+ }
/// Returns the URI that represents [path].
///
diff --git a/pubspec.yaml b/pubspec.yaml
index 3b555a1..8095c88 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: path
-version: 1.0.0
+version: 1.1.0
author: Dart Team <misc@dartlang.org>
description: >
A string-based path manipulation library. All of the path operations you know
diff --git a/test/posix_test.dart b/test/posix_test.dart
index 83a473e..9da97c9 100644
--- a/test/posix_test.dart
+++ b/test/posix_test.dart
@@ -474,19 +474,26 @@
expect(context.withoutExtension('a/b.c//'), 'a/b//');
});
- test('fromUri', () {
- expect(context.fromUri(Uri.parse('file:///path/to/foo')), '/path/to/foo');
- expect(context.fromUri(Uri.parse('file:///path/to/foo/')), '/path/to/foo/');
- expect(context.fromUri(Uri.parse('file:///')), '/');
- expect(context.fromUri(Uri.parse('foo/bar')), 'foo/bar');
- expect(context.fromUri(Uri.parse('/path/to/foo')), '/path/to/foo');
- expect(context.fromUri(Uri.parse('///path/to/foo')), '/path/to/foo');
- expect(context.fromUri(Uri.parse('file:///path/to/foo%23bar')),
- '/path/to/foo#bar');
- expect(context.fromUri(Uri.parse('_%7B_%7D_%60_%5E_%20_%22_%25_')),
- r'_{_}_`_^_ _"_%_');
- expect(() => context.fromUri(Uri.parse('http://dartlang.org')),
- throwsArgumentError);
+ group('fromUri', () {
+ test('with a URI', () {
+ expect(context.fromUri(Uri.parse('file:///path/to/foo')), '/path/to/foo');
+ expect(context.fromUri(Uri.parse('file:///path/to/foo/')),
+ '/path/to/foo/');
+ expect(context.fromUri(Uri.parse('file:///')), '/');
+ expect(context.fromUri(Uri.parse('foo/bar')), 'foo/bar');
+ expect(context.fromUri(Uri.parse('/path/to/foo')), '/path/to/foo');
+ expect(context.fromUri(Uri.parse('///path/to/foo')), '/path/to/foo');
+ expect(context.fromUri(Uri.parse('file:///path/to/foo%23bar')),
+ '/path/to/foo#bar');
+ expect(context.fromUri(Uri.parse('_%7B_%7D_%60_%5E_%20_%22_%25_')),
+ r'_{_}_`_^_ _"_%_');
+ expect(() => context.fromUri(Uri.parse('http://dartlang.org')),
+ throwsArgumentError);
+ });
+
+ test('with a string', () {
+ expect(context.fromUri('file:///path/to/foo'), '/path/to/foo');
+ });
});
test('toUri', () {
diff --git a/test/url_test.dart b/test/url_test.dart
index e149b4f..58c7b92 100644
--- a/test/url_test.dart
+++ b/test/url_test.dart
@@ -700,20 +700,28 @@
expect(context.withoutExtension('a/b.c//'), 'a/b//');
});
- test('fromUri', () {
- expect(context.fromUri(Uri.parse('http://dartlang.org/path/to/foo')),
- 'http://dartlang.org/path/to/foo');
- expect(context.fromUri(Uri.parse('http://dartlang.org/path/to/foo/')),
- 'http://dartlang.org/path/to/foo/');
- expect(context.fromUri(Uri.parse('file:///path/to/foo')),
- 'file:///path/to/foo');
- expect(context.fromUri(Uri.parse('foo/bar')), 'foo/bar');
- expect(context.fromUri(Uri.parse('http://dartlang.org/path/to/foo%23bar')),
- 'http://dartlang.org/path/to/foo%23bar');
- // Since the resulting "path" is also a URL, special characters should
- // remain percent-encoded in the result.
- expect(context.fromUri(Uri.parse('_%7B_%7D_%60_%5E_%20_%22_%25_')),
- r'_%7B_%7D_%60_%5E_%20_%22_%25_');
+ group('fromUri', () {
+ test('with a URI', () {
+ expect(context.fromUri(Uri.parse('http://dartlang.org/path/to/foo')),
+ 'http://dartlang.org/path/to/foo');
+ expect(context.fromUri(Uri.parse('http://dartlang.org/path/to/foo/')),
+ 'http://dartlang.org/path/to/foo/');
+ expect(context.fromUri(Uri.parse('file:///path/to/foo')),
+ 'file:///path/to/foo');
+ expect(context.fromUri(Uri.parse('foo/bar')), 'foo/bar');
+ expect(context.fromUri(
+ Uri.parse('http://dartlang.org/path/to/foo%23bar')),
+ 'http://dartlang.org/path/to/foo%23bar');
+ // Since the resulting "path" is also a URL, special characters should
+ // remain percent-encoded in the result.
+ expect(context.fromUri(Uri.parse('_%7B_%7D_%60_%5E_%20_%22_%25_')),
+ r'_%7B_%7D_%60_%5E_%20_%22_%25_');
+ });
+
+ test('with a string', () {
+ expect(context.fromUri('http://dartlang.org/path/to/foo'),
+ 'http://dartlang.org/path/to/foo');
+ });
});
test('toUri', () {
diff --git a/test/windows_test.dart b/test/windows_test.dart
index e5dcac4..d194663 100644
--- a/test/windows_test.dart
+++ b/test/windows_test.dart
@@ -590,27 +590,35 @@
expect(context.withoutExtension(r'a\b.c\'), r'a\b\');
});
- test('fromUri', () {
- expect(context.fromUri(Uri.parse('file:///C:/path/to/foo')),
- r'C:\path\to\foo');
- expect(context.fromUri(Uri.parse('file://server/share/path/to/foo')),
- r'\\server\share\path\to\foo');
- expect(context.fromUri(Uri.parse('file:///C:/')), r'C:\');
- expect(context.fromUri(Uri.parse('file://server/share')),
- r'\\server\share');
- expect(context.fromUri(Uri.parse('foo/bar')), r'foo\bar');
- expect(context.fromUri(Uri.parse('/C:/path/to/foo')), r'C:\path\to\foo');
- expect(context.fromUri(Uri.parse('///C:/path/to/foo')), r'C:\path\to\foo');
- expect(context.fromUri(Uri.parse('//server/share/path/to/foo')),
- r'\\server\share\path\to\foo');
- expect(context.fromUri(Uri.parse('file:///C:/path/to/foo%23bar')),
- r'C:\path\to\foo#bar');
- expect(context.fromUri(Uri.parse('file://server/share/path/to/foo%23bar')),
- r'\\server\share\path\to\foo#bar');
- expect(context.fromUri(Uri.parse('_%7B_%7D_%60_%5E_%20_%22_%25_')),
- r'_{_}_`_^_ _"_%_');
- expect(() => context.fromUri(Uri.parse('http://dartlang.org')),
- throwsArgumentError);
+ group('fromUri', () {
+ test('with a URI', () {
+ expect(context.fromUri(Uri.parse('file:///C:/path/to/foo')),
+ r'C:\path\to\foo');
+ expect(context.fromUri(Uri.parse('file://server/share/path/to/foo')),
+ r'\\server\share\path\to\foo');
+ expect(context.fromUri(Uri.parse('file:///C:/')), r'C:\');
+ expect(context.fromUri(Uri.parse('file://server/share')),
+ r'\\server\share');
+ expect(context.fromUri(Uri.parse('foo/bar')), r'foo\bar');
+ expect(context.fromUri(Uri.parse('/C:/path/to/foo')), r'C:\path\to\foo');
+ expect(context.fromUri(Uri.parse('///C:/path/to/foo')),
+ r'C:\path\to\foo');
+ expect(context.fromUri(Uri.parse('//server/share/path/to/foo')),
+ r'\\server\share\path\to\foo');
+ expect(context.fromUri(Uri.parse('file:///C:/path/to/foo%23bar')),
+ r'C:\path\to\foo#bar');
+ expect(context.fromUri(
+ Uri.parse('file://server/share/path/to/foo%23bar')),
+ r'\\server\share\path\to\foo#bar');
+ expect(context.fromUri(Uri.parse('_%7B_%7D_%60_%5E_%20_%22_%25_')),
+ r'_{_}_`_^_ _"_%_');
+ expect(() => context.fromUri(Uri.parse('http://dartlang.org')),
+ throwsArgumentError);
+ });
+
+ test('with a string', () {
+ expect(context.fromUri('file:///C:/path/to/foo'), r'C:\path\to\foo');
+ });
});
test('toUri', () {