Created
March 21, 2009 22:36
-
-
Save youpy/83004 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* appjet:version 0.1 */ | |
import("lib-jsunit-2-2"); | |
import(appjet.appName); | |
print(H1(appjet.appName)); | |
print(H2('Usage')); | |
print(P(CODE("""import("lib-uri"); | |
var uri = new URI('http://example.com/a/b'); | |
uri.resolveLink('/c'); //=> http://example.com/c | |
"""))); | |
print(H2('Tests')); | |
test(function testNew() { | |
var uri = new URI('http://www.example.com:1234/a/b/c?d=e#f'); | |
assertEquals('http://www.example.com:1234/a/b/c?d=e#f', uri.href); | |
assertEquals('http:', uri.protocol); | |
assertEquals('www.example.com:1234', uri.host); | |
assertEquals('www.example.com', uri.hostname); | |
assertEquals('1234', uri.port); | |
assertEquals('/a/b/c', uri.pathname); | |
assertEquals('?d=e', uri.search); | |
assertEquals('#f', uri.hash); | |
}); | |
test(function testResolveLink() { | |
var uri = new URI('http://example.com/a/b?c=4'); | |
assertSameURI('http://example.com/b/c', uri.resolveLink('http://example.com/b/c')); | |
assertSameURI('http://example.com/', uri.resolveLink('/')); | |
assertSameURI('http://example.com/a', uri.resolveLink('/a')); | |
assertSameURI('http://example.com/a/c', uri.resolveLink('/a/c')); | |
assertSameURI('http://example.com/a/c', uri.resolveLink('c')); | |
assertSameURI('http://example.com/a/../c', uri.resolveLink('../c')); | |
assertSameURI('http://example.com/a/b?c=4#d', uri.resolveLink('#d')); | |
}); | |
test(function testResolveLinkBaseUrlEndsWithDirectory() { | |
var uri = new URI('http://example.com/a/'); | |
assertSameURI('http://example.com/b/c', uri.resolveLink('http://example.com/b/c')); | |
assertSameURI('http://example.com/', uri.resolveLink('/')); | |
assertSameURI('http://example.com/a', uri.resolveLink('/a')); | |
assertSameURI('http://example.com/a/c', uri.resolveLink('/a/c')); | |
assertSameURI('http://example.com/a/c', uri.resolveLink('c')); | |
assertSameURI('http://example.com/a/../c', uri.resolveLink('../c')); | |
assertSameURI('http://example.com/a/#d', uri.resolveLink('#d')); | |
}); | |
function assertSameURI(str, uri) { | |
assertEquals(str, uri.toString()); | |
} | |
import("lib-jsunitrunner"); | |
dispatchRunner(); | |
/* appjet:library */ | |
// based on http://d.hatena.ne.jp/javascripter/20090309/1236590529 | |
function URI(uri) { | |
uri = String(uri); | |
var parser = /^([^:\/?#]+:)?\/\/(([^\/?#:]*):?(\d*))?([^?#]*)(\?[^#]*)?(#.*)?$/; | |
var m = uri.match(parser); | |
if (!m) throw new URIError("malformed URI given"); | |
this.href = m[0]; | |
this.protocol = m[1] || ""; | |
this.host = m[2] || ""; | |
this.hostname = m[3] || ""; | |
this.port = m[4] || ""; | |
this.pathname = m[5] || ""; | |
this.search = m[6] === "?" ? "" : m[6] || ""; | |
this.hash = m[7] || ""; | |
} | |
URI.prototype.toString = function () { | |
return this.href; | |
}; | |
URI.prototype.resolveLink = function(link) { | |
var root = [ | |
this.protocol, | |
'//', | |
this.host, | |
].join(''); | |
if(link.match(/^https*:/)) { | |
return new URI(link); | |
} | |
if(link.match(/^\//)) { | |
return new URI(root + link); | |
} | |
if(link.match(/^#/)) { | |
return new URI(root | |
+ this.pathname | |
+ this.search | |
+ link); | |
} | |
return new URI(root | |
+ this.pathname.replace(/[^\/]+$/, '') | |
+ link); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment