Skip to content

Commit

Permalink
fixing Issue #29 - absoluteTo() for relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneyrehm committed Jun 23, 2012
1 parent 484aa00 commit 03e292a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m

## Changelog ##

### 1.6.2 (???) ###

* `.directory()` now returns empty string if there is no directory
* fixed `.absoluteTo()` to join two relative paths properly ([Issue #29](https://github.com/medialize/URI.js/issues/29))

### 1.6.1 (May 19th 2012) ###

* fixing TypeError on domain() with dot-less hostnames ([Issue #27](https://github.com/medialize/URI.js/issues/27))
Expand Down
19 changes: 11 additions & 8 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,12 +941,16 @@ p.directory = function(v, build) {
}

if (v === undefined || v === true) {
if (!this._parts.path || this._parts.path === '/') {
if (!this._parts.path && !this._parts.hostname) {
return '';
}

if (this._parts.path === '/') {
return '/';
}

var end = this._parts.path.length - this.filename().length - 1,
res = this._parts.path.substring(0, end) || "/";
res = this._parts.path.substring(0, end) || (this._parts.hostname ? "/" : "");

return v ? URI.decodePath(res) : res;

Expand Down Expand Up @@ -1310,24 +1314,23 @@ p.absoluteTo = function(base) {
if (this._parts.urn) {
throw new Error('URNs do not have any generally defined hierachical components');
}

if (!this.is('relative')) {
throw new Error('Cannot resolve non-relative URL');
}

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

var resolved = new URI(this),
properties = ['protocol', 'username', 'password', 'hostname', 'port'];
properties = ['protocol', 'username', 'password', 'hostname', 'port'],
basedir;

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

if (resolved.path()[0] !== '/') {
resolved._parts.path = base.directory() + '/' + resolved._parts.path;
basedir = base.directory();
resolved._parts.path = (basedir ? (basedir + '/') : '') + resolved._parts.path;
console.log(resolved._parts.path);
resolved.normalizePath();
}

Expand Down
42 changes: 42 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,48 @@ test("absoluteTo", function() {
url: '../relative/path?blubber=1#hash3',
base: '/path/to/file?some=query#hash',
result: '/path/relative/path?blubber=1#hash3'
}, {
name: 'relative path - urljoin',
url: 'the_relative_url',
base: 'rel/path/',
result: 'rel/path/the_relative_url'
}, {
name: 'relative path file - urljoin',
url: 'the_relative_url',
base: 'rel/path/something',
result: 'rel/path/the_relative_url'
}, {
name: 'relative path file - urljoin',
url: '../the_relative_url',
base: 'rel/path/',
result: 'rel/the_relative_url'
}, {
name: 'relative path file - urljoin',
url: '/the_relative_url',
base: 'rel/path/',
result: '/the_relative_url'
}, {
name: 'relative path file - urljoin',
url: '/the_relative_url',
base: 'http://example.com/rel/path/',
result: 'http://example.com/the_relative_url'
}, {
name: 'relative path file - urljoin',
url: '/the_relative_url',
base: 'http://github.com//the_relative_url',
// result is sanitized, so a double slashes are eradicated!
//result: 'http://github.com//the_relative_url'
result: 'http://github.com/the_relative_url'
}, {
name: 'file paths - urljoin',
url: 'anotherFile',
base: 'aFile',
result: 'anotherFile'
}, {
name: 'absolute URL to absolute URL',
url: 'http://example.com/some/other/path/file',
base: 'http://example.org/rel/path/foo',
result: 'http://example.org/some/other/path/file'
}
];

Expand Down

0 comments on commit 03e292a

Please sign in to comment.