Skip to content

Commit

Permalink
feature(normalizePath): fixing "/foo/.." to result in "/" instead of …
Browse files Browse the repository at this point in the history
…"" - closing #224
  • Loading branch information
rodneyrehm committed Jun 29, 2015
1 parent 4ad3f29 commit 5331344
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m

* fixing [`URI.parseQuery()`](http://medialize.github.io/URI.js/docs.html#static-parseQuery) to accept `?foo&foo=bar` - [Issue #220](https://github.com/medialize/URI.js/issues/220)
* fixing [`URI.segmentCoded()`](http://medialize.github.io/URI.js/docs.html#accessors-segmentCoded) to encode (instead of decode) array input - [Issue #223](https://github.com/medialize/URI.js/issues/223)
* fixing [`URI.normalizePath()`](http://medialize.github.io/URI.js/docs.html#normalize-path) to properly resolve `/foo/..` to `/` - [Issue #224](https://github.com/medialize/URI.js/issues/224)

### 1.15.1 (April 5th 2015) ###

Expand Down
7 changes: 6 additions & 1 deletion src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,11 @@
_path = '/' + _path;
}

// handle relative files (as opposed to directories)
if (_path.slice(-3) === '/..' || _path.slice(-2) === '/.') {
_path += '/';
}

// resolve simples
_path = _path
.replace(/(\/(\.\/)+)|(\/\.$)/g, '/')
Expand Down Expand Up @@ -2016,7 +2021,7 @@
}

// determine common sub path
common = URI.commonPath(relative.path(), base.path());
common = URI.commonPath(relativePath, basePath);

// If the paths have nothing in common, return a relative URL with the absolute path.
if (!common) {
Expand Down
15 changes: 12 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1096,15 +1096,24 @@
u.path('/.//').normalizePath();
equal(u.path(), '/', 'root /.//');

u.path('/.').normalizePath();
equal(u.path(), '/', 'root /.');

// encoding
u._parts.path = '/~userhome/@mine;is %2F and/';
u.normalize();
equal(u.pathname(), '/~userhome/@mine;is%20%2F%20and/', 'path encoding');

// relative URL
u = URI('/.').normalizePath();
equal(u.path(), '/', 'root /.');

u = URI('/..').normalizePath();
equal(u.path(), '/', 'root /..');

u = URI('/foo/.').normalizePath();
equal(u.path(), '/foo/', 'root /foo/.');

u = URI('/foo/..').normalizePath();
equal(u.path(), '/', 'root /foo/..');

u = URI('../../../../../www/common/js/app/../../../../www_test/common/js/app/views/view-test.html');
u.normalize();
equal(u.path(), '../../../../../www_test/common/js/app/views/view-test.html', 'parent relative');
Expand Down

0 comments on commit 5331344

Please sign in to comment.