Skip to content

Commit

Permalink
fixing .relativeTo() path comparison - closes #75
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneyrehm committed Apr 15, 2013
1 parent c33dad0 commit 3158187
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m

## Changelog ##

* fixing [`relativeTo()`](http://medialize.github.com/URI.js/docs.htmlrelativeto) - ([Issue #75](https://github.com/medialize/URI.js/issues/75))

### 1.10.1 (April 2nd 2013) ###

* adding [`absoluteTo()`](http://medialize.github.com/URI.js/docs.html#absoluteto) to properly resolve relative scheme - ([Issue #71](https://github.com/medialize/URI.js/issues/73))
Expand Down
23 changes: 13 additions & 10 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,9 @@ URI.commonPath = function(one, two) {
if (pos < 1) {
return one.charAt(0) === two.charAt(0) && one.charAt(0) === '/' ? '/' : '';
}

// revert to last /
if (one.charAt(pos) !== '/') {
if (one.charAt(pos) !== '/' || two.charAt(pos) !== '/') {
pos = one.substring(0, pos).lastIndexOf('/');
}

Expand Down Expand Up @@ -1672,33 +1672,36 @@ p.relativeTo = function(base) {
var properties = ['protocol', 'username', 'password', 'hostname', 'port'];
var common, _base, _this, _base_diff, _this_diff;

if (this._parts.urn) {
if (relative._parts.urn) {
throw new Error('URNs do not have any generally defined hierachical components');
}

if (!(base instanceof URI)) {
base = new URI(base);
}

if (this.path().charAt(0) !== '/' || base.path().charAt(0) !== '/') {
if (relative.path().charAt(0) !== '/' || base.path().charAt(0) !== '/') {
throw new Error('Cannot calculate common path from non-relative URLs');
}

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

// no relation if there's nothing in common
if (!common || common === '/') {
return relative;
}

// relative paths don't have authority
for (var i = 0, p; p = properties[i]; i++) {
relative._parts[p] = null;
}

// no relation if there's nothing in common
if (common === '/') {
return relative;
} else if (!common) {
// there's absolutely nothing in common here
return this.clone();
}

_base = base.directory();
_this = this.directory();
_this = relative.directory();

// base and this are on the same level
if (_base === _this) {
Expand Down
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,21 @@ test("relativeTo", function() {
url: '/base/path/with/subdir/inner.html',
base: '/base/path/top.html',
result: './with/subdir/inner.html'
}, {
name: 'absolute /',
url: 'http://example.org/foo/bar/bat',
base: 'http://example.org/',
result: '/foo/bar/bat'
}, {
name: 'absolute /foo',
url: 'http://example.org/foo/bar/bat',
base: 'http://example.org/foo',
result: '/foo/bar/bat'
}, {
name: 'absolute /foo/',
url: 'http://example.org/foo/bar/bat',
base: 'http://example.org/foo/',
result: './bar/bat'
}
];

Expand Down

0 comments on commit 3158187

Please sign in to comment.