Skip to content

Commit

Permalink
fixing normalizePath() to maintain parents - closing #133
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneyrehm committed Jan 22, 2014
1 parent 253565d commit 12d82e7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m
### [dev-version master] ###

* fixing [`.absoluteTo()`](http://medialize.github.com/URI.js/docs.html#absoluteto) to comply with [RFC3986 Reference Resolution Examples](http://tools.ietf.org/html/rfc3986#section-5.4) - ([Issue #113](https://github.com/medialize/URI.js/issues/113))
* fixing [`.normalizePath()`](http://medialize.github.com/URI.js/docs.html#normalize-path) to maintain leading parent references (`../`) for relative paths, while removing them for absolute paths - ([Issue #133](https://github.com/medialize/URI.js/issues/133))

### 1.11.2 (August 14th 2013) ###

Expand Down
11 changes: 10 additions & 1 deletion src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,7 @@ p.normalizePath = function(build) {

var _was_relative;
var _path = this._parts.path;
var _leadingParents = '';
var _parent, _pos;

// handle relative paths
Expand All @@ -1633,6 +1634,14 @@ p.normalizePath = function(build) {
.replace(/(\/(\.\/)+)|(\/\.$)/g, '/')
.replace(/\/{2,}/g, '/');

// remember leading parents
if (_was_relative) {
_leadingParents = _path.substring(1).match(/^(\.\.\/)+/) || '';
if (_leadingParents) {
_leadingParents = _leadingParents[0];
}
}

// resolve parents
while (true) {
_parent = _path.indexOf('/..');
Expand All @@ -1654,7 +1663,7 @@ p.normalizePath = function(build) {

// revert to relative
if (_was_relative && this.is('relative')) {
_path = _path.substring(1);
_path = _leadingParents + _path.substring(1);
}

_path = URI.recodePath(_path);
Expand Down
9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,15 @@ test("normalizePath", function() {
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('../../../../../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");

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 absolute");
});
test("normalizeQuery", function() {
var u = new URI("http://example.org/foobar.html?");
Expand Down

0 comments on commit 12d82e7

Please sign in to comment.