Skip to content

Commit

Permalink
Fixing .absoluteTo() to comply with RFC 3986 - Issue #41
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneyrehm committed Aug 26, 2012
1 parent bff0696 commit 5632bbe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ p.readable = function() {
p.absoluteTo = function(base) {
var resolved = this.clone(),
properties = ['protocol', 'username', 'password', 'hostname', 'port'],
basedir;
basedir, i, p;

if (this._parts.urn) {
throw new Error('URNs do not have any generally defined hierachical components');
Expand All @@ -1421,9 +1421,16 @@ p.absoluteTo = function(base) {
base = new URI(base);
}

for (var i = 0, p; p = properties[i]; i++) {
for (i = 0, p; p = properties[i]; i++) {
resolved._parts[p] = base._parts[p];
}

properties = ['query', 'path'];
for (i = 0, p; p = properties[i]; i++) {
if (!resolved._parts[p] && base._parts[p]) {
resolved._parts[p] = base._parts[p];
}
}

if (resolved.path()[0] !== '/') {
basedir = base.directory();
Expand Down
5 changes: 5 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,11 @@ test("absoluteTo", function() {
url: '../relative/path?blubber=1#hash3',
base: '/path/to/file?some=query#hash',
result: '/path/relative/path?blubber=1#hash3'
}, {
name: 'base query string',
url: '#hash3',
base: '/path/to/file?some=query#hash',
result: '/path/to/file?some=query#hash3'
}, {
name: 'relative path - urljoin',
url: 'the_relative_url',
Expand Down

0 comments on commit 5632bbe

Please sign in to comment.