Skip to content

Commit

Permalink
optimizing relativeTo() results - closes #95
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneyrehm committed Aug 4, 2013
1 parent 38bda29 commit c071df2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m

### `[dev-version]` (master branch) ###

* optimize [`relativeTo()`](http://medialize.github.com/URI.js/docs.html#relativeto) results - ([Issue #78](https://github.com/medialize/URI.js/issues/78))
* optimize [`relativeTo()`](http://medialize.github.com/URI.js/docs.html#relativeto) results - ([Issue #78](https://github.com/medialize/URI.js/issues/78), [Issue #95](https://github.com/medialize/URI.js/issues/95))
* removing obsolete code fragments from `URI.parse()` and `relativeTo()` - ([Issue #100](https://github.com/medialize/URI.js/issues/100))
* adding setting `URI.escapeQuerySpace` to control if query string should escape spaces using `+` or `%20` - ([Issue #74](https://github.com/medialize/URI.js/issues/74))
* updating [Punycode.js](https://github.com/bestiejs/punycode.js/) to version 1.2.3
Expand Down
20 changes: 17 additions & 3 deletions docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -866,12 +866,26 @@ <h3 id="relativeto">relativeTo()</h3>
// -&gt; "foo/world.html"

// absolute URLs are passed through unchanged
URI("http://example.org/world.html").absoluteTo("http://google.com/baz");
URI("http://example.org/world.html")
.absoluteTo("http://google.com/baz");
// -&gt; "http://example.org/world.html"

// absolute URLs relative to absolute URLs
URI("http://example.org/world.html").clone().authority("").absoluteTo("http://google.com/baz");
// -&gt; "http://google.com/world.html"</pre>
URI("http://example.org/world.html").clone().authority("")
.absoluteTo("http://google.com/baz");
// -&gt; "http://google.com/world.html"

// equal URLs are relative by empty string
URI("http://www.example.com:8080/dir/file")
.relativeTo('http://www.example.com:8080/dir/file')
.toString();
// -&gt; ""

// relative on fragment and query string as well
URI("http://www.example.com:8080/dir/file?foo=bar#abcd")
.relativeTo('http://www.example.com:8080/dir/file')
.toString();
// -&gt; "?foo=bar#abcd"</pre>
<p>.relativeTo() and .absoluteTo() reverse each other.</p>

<h3 id="absoluteto">absoluteTo()</h3>
Expand Down
15 changes: 11 additions & 4 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,7 @@ p.absoluteTo = function(base) {
};
p.relativeTo = function(base) {
var relative = this.clone().normalize();
var relativeParts, baseParts, common;
var relativeParts, baseParts, common, relativePath, basePath;

if (relative._parts.urn) {
throw new Error('URNs do not have any generally defined hierarchical components');
Expand All @@ -1766,12 +1766,14 @@ p.relativeTo = function(base) {
base = new URI(base).normalize();
relativeParts = relative._parts;
baseParts = base._parts;
relativePath = relative.path();
basePath = base.path();

if (relative.path().charAt(0) !== '/') {
if (relativePath.charAt(0) !== '/') {
throw new Error('URI is already relative');
}

if (base.path().charAt(0) !== '/') {
if (basePath.charAt(0) !== '/') {
throw new Error('Cannot calculate a URI relative to another relative URI');
}

Expand All @@ -1794,6 +1796,11 @@ p.relativeTo = function(base) {
return relative.build();
}

if (relativePath === basePath) {
relativeParts.path = '';
return relative.build();
}

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

Expand All @@ -1806,7 +1813,7 @@ p.relativeTo = function(base) {
.substring(common.length)
.replace(/[^\/]*$/, '')
.replace(/.*?\//g, '../');

relativeParts.path = parents + relativeParts.path.substring(common.length);

return relative.build();
Expand Down
21 changes: 18 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1179,16 +1179,31 @@ test("relativeTo", function() {
url: 'http://example.org:8081/foo/bar',
base: 'http://example.org/foo/bar',
result: '//example.org:8081/foo/bar'
}, {
name: 'same path - fragment',
url: 'http://www.example.com:8080/dir/file#abcd',
base: 'http://www.example.com:8080/dir/file',
result: '#abcd'
}, {
name: 'same path - query',
url: 'http://www.example.com:8080/dir/file?abcd=123',
base: 'http://www.example.com:8080/dir/file',
result: '?abcd=123'
}, {
name: 'same path - query and fragment',
url: 'http://www.example.com:8080/dir/file?abcd=123#alpha',
base: 'http://www.example.com:8080/dir/file',
result: '?abcd=123#alpha'
}, {
name: 'already relative',
url: 'foo/bar',
base: '/foo/',
throws: true
'throws': true
}, {
name: 'relative base',
url: '/foo/bar',
base: 'foo/',
throws: true

This comment has been minimized.

Copy link
@djcsdy

djcsdy Aug 5, 2013

Contributor

Well spotted, I always forget that throws is a reserved word in EcmaScript 3.

(I’m not sure if there’s any browser that actually prevents you using it though...)

This comment has been minimized.

Copy link
@rodneyrehm

rodneyrehm Aug 5, 2013

Author Member

I'm not aware of any browser issues - it's just bugging jshint…

I think I would've called the property "error" or something… but in the end - who cares?

'throws': true
}
];

Expand All @@ -1204,7 +1219,7 @@ test("relativeTo", function() {
caught = true;
}

if (t.throws) {
if (t['throws']) {
ok(caught, t.name + " should throw exception");
} else {
ok(!caught, t.name + " should not throw exception");
Expand Down

0 comments on commit c071df2

Please sign in to comment.