Created
December 4, 2008 00:32
-
-
Save swdyh/31787 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | |
"http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<script src="http://code.jquery.com/jquery-latest.js"></script> | |
<link rel="stylesheet" href="testsuite.css" type="text/css" media="screen" /> | |
<script> | |
// http://docs.jquery.com/QUnit | |
// resolve_path_xml | |
// http://twitter.com/nanto_vi/status/1035852999 | |
// resolve_path_js | |
// http://d.hatena.ne.jp/Constellation/20081203/1228294417 | |
var resolve_path_xml = function(path, base) { | |
const XHTML_NS = "http://www.w3.org/1999/xhtml" | |
const XML_NS = "http://www.w3.org/XML/1998/namespace" | |
var a = document.createElementNS(XHTML_NS, 'a') | |
a.setAttributeNS(XML_NS, 'xml:base', base) | |
a.href = path | |
return a.href | |
} | |
var resolve_path_js = function(path, base){ | |
base || (base = document.location.href); | |
var pathname = new pathName(base); | |
pathname.parse(path); | |
return pathname.absolute; | |
} | |
// need to be an absolute path. | |
var pathName = function(str){ | |
this.path = str; | |
this.top = str.match(pathName.reg)[0]; | |
this.list = str.replace(this.top, "").split('/'); | |
} | |
pathName.reg = /^https?:\/\/[^\/]+\//; | |
pathName.prototype = { | |
get current(){ | |
var len = this.list.length; | |
return len? this.list[len-1] : ""; | |
}, | |
get absolute(){ | |
return this.top + this.list.join('/'); | |
}, | |
child: function(name){ | |
this.list.push(name); | |
}, | |
parent: function(){ | |
this.list.pop(); | |
}, | |
root: function(){ | |
this.list = [""]; | |
}, | |
parse: function(path){ | |
var list = path.split('/'); | |
var len = list.length - 1; | |
list && list.forEach(function(phase, index){ | |
if(index == 0 && phase == '') return this.root(); | |
var p1 = phase[0]; | |
if(p1 == '.'){ | |
var p = phase[1]; | |
if(!p){ | |
return (!this.current == "" && this.child("")); | |
} else if(p == '.'){ | |
this.parent(); | |
this.parent(); | |
if(!(index == len))this.child(""); | |
} | |
} else { | |
this.parent(); | |
this.child(phase); | |
if(!(index == len))this.child(""); | |
return; | |
} | |
}, this); | |
} | |
} | |
</script> | |
<script> | |
var test_case = [ | |
{ | |
base: 'http://d.hatena.ne.jp/Constellation/', | |
path: './hogehoge/foobar/', | |
expect: 'http://d.hatena.ne.jp/Constellation/hogehoge/foobar/', | |
}, | |
{ | |
base: 'http://d.hatena.ne.jp/Constellation', | |
path: './hogehoge/foobar/', | |
// expect: 'http://d.hatena.ne.jp/Constellation/hogehoge/foobar/', | |
expect: 'http://d.hatena.ne.jp/hogehoge/foobar/', | |
}, | |
{ | |
base: 'http://d.hatena.ne.jp/Constellation/', | |
path: 'hogehoge/foobar/', | |
expect: 'http://d.hatena.ne.jp/Constellation/hogehoge/foobar/', | |
}, | |
{ | |
base: 'http://d.hatena.ne.jp/Constellation', | |
path: 'hogehoge/foobar/', | |
expect: 'http://d.hatena.ne.jp/hogehoge/foobar/', | |
}, | |
{ | |
base: 'http://d.hatena.ne.jp/Constellation/', | |
path: '/hogehoge/.././foobar', | |
expect: 'http://d.hatena.ne.jp/foobar', | |
}, | |
{ | |
base: 'http://d.hatena.ne.jp/Constellation', | |
path: '/hogehoge/.././foobar', | |
expect: 'http://d.hatena.ne.jp/foobar', | |
}, | |
{ | |
base: 'http://ido.nu/kuma/', | |
path: '?page=2', | |
expect: 'http://ido.nu/kuma/?page=2', | |
}, | |
{ | |
base: 'http://diary.jp.aol.com/juicyfruits/', | |
path: '/applet/juicyfruits/archive?b=10', | |
expect: 'http://diary.jp.aol.com/applet/juicyfruits/archive?b=10', | |
}, | |
{ | |
base: 'http://diary.jp.aol.com/applet/juicyfruits/archive?b=10', | |
path: './archive?b=20', | |
expect: 'http://diary.jp.aol.com/applet/juicyfruits/archive?b=20', | |
}, | |
/* | |
{ | |
base: '', | |
path: '', | |
expect: '', | |
}, | |
*/ | |
] | |
$(document).ready(function(){ | |
module("resolve_path_js"); | |
test("resolve_path", function() { | |
test_case.forEach(function(i) { | |
equals(resolve_path_js(i['path'], i['base']), i['expect']) | |
}) | |
}) | |
module("resolve_path_xml"); | |
test("resolve_path", function() { | |
test_case.forEach(function(i) { | |
equals(resolve_path_xml(i['path'], i['base']), i['expect']) | |
}) | |
}) | |
}); | |
</script> | |
</head> | |
<body> | |
<script type="text/javascript" src="testrunner.js"></script> | |
<h1>QUnit example</h1> | |
<h2 id="banner"></h2> | |
<h2 id="userAgent"></h2> | |
<ol id="tests"></ol> | |
<div id="main"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment