123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- define(function() {
-
- function URI(uri) {
- if (uri instanceof URI) {
- this.scheme = uri.scheme;
- this.authority = uri.authority;
- this.path = uri.path;
- this.query = uri.query;
- this.fragment = uri.fragment;
- } else if (uri) {
- var c = parseRegex.exec(uri);
- this.scheme = c[1];
- this.authority = c[2];
- this.path = c[3];
- this.query = c[4];
- this.fragment = c[5];
- }
- };
-
- URI.prototype.scheme = null;
- URI.prototype.authority = null;
- URI.prototype.path = '';
- URI.prototype.query = null;
- URI.prototype.fragment = null;
-
- var parseRegex = new RegExp('^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?$');
-
- URI.prototype.getScheme = function() {
- return this.scheme;
- };
-
- URI.prototype.getAuthority = function() {
- return this.authority;
- };
-
- URI.prototype.getPath = function() {
- return this.path;
- };
-
- URI.prototype.getQuery = function() {
- return this.query;
- };
-
- URI.prototype.getFragment = function() {
- return this.fragment;
- };
-
- URI.prototype.isAbsolute = function() {
- return !!this.scheme && !this.fragment;
- };
-
-
-
-
-
- URI.prototype.isSameDocumentAs = function(uri) {
- return uri.scheme == this.scheme &&
- uri.authority == this.authority &&
- uri.path == this.path &&
- uri.query == this.query;
- };
-
- URI.prototype.equals = function(uri) {
- return this.isSameDocumentAs(uri) && uri.fragment == this.fragment;
- };
-
- URI.prototype.normalize = function() {
- this.removeDotSegments();
- if (this.scheme)
- this.scheme = this.scheme.toLowerCase();
- if (this.authority)
- this.authority = this.authority.replace(authorityRegex, replaceAuthority).
- replace(caseRegex, replaceCase);
- if (this.path)
- this.path = this.path.replace(caseRegex, replaceCase);
- if (this.query)
- this.query = this.query.replace(caseRegex, replaceCase);
- if (this.fragment)
- this.fragment = this.fragment.replace(caseRegex, replaceCase);
- };
- var caseRegex = /%[0-9a-z]{2}/gi;
- var percentRegex = /[a-zA-Z0-9\-\._~]/;
- var authorityRegex = /(.*@)?([^@:]*)(:.*)?/;
- function replaceCase(str) {
- var dec = unescape(str);
- return percentRegex.test(dec) ? dec : str.toUpperCase();
- }
- function replaceAuthority(str, p1, p2, p3) {
- return (p1 || '') + p2.toLowerCase() + (p3 || '');
- }
-
- URI.prototype.resolve = function(baseURI) {
- var uri = new URI();
- if (this.scheme) {
- uri.scheme = this.scheme;
- uri.authority = this.authority;
- uri.path = this.path;
- uri.query = this.query;
- } else {
- uri.scheme = baseURI.scheme;
- if (this.authority) {
- uri.authority = this.authority;
- uri.path = this.path;
- uri.query = this.query;
- } else {
- uri.authority = baseURI.authority;
- if (this.path == '') {
- uri.path = baseURI.path;
- uri.query = this.query || baseURI.query;
- } else {
- if (this.path.charAt(0) == '/') {
- uri.path = this.path;
- uri.removeDotSegments();
- } else {
- if (baseURI.authority && baseURI.path == '') {
- uri.path = '/' + this.path;
- } else {
- uri.path = baseURI.path.substring(0, baseURI.path.lastIndexOf('/') + 1) + this.path;
- }
- uri.removeDotSegments();
- }
- uri.query = this.query;
- }
- }
- }
- uri.fragment = this.fragment;
- return uri;
- };
-
- URI.prototype.removeDotSegments = function() {
- var input = this.path.split('/'),
- output = [],
- segment,
- absPath = input[0] == '';
- if (absPath)
- input.shift();
- var sFirst = input[0] == '' ? input.shift() : null;
- while (input.length) {
- segment = input.shift();
- if (segment == '..') {
- output.pop();
- } else if (segment != '.') {
- output.push(segment);
- }
- }
- if (segment == '.' || segment == '..')
- output.push('');
- if (absPath)
- output.unshift('');
- this.path = output.join('/');
- };
-
-
- URI.prototype.toString = function() {
- var result = '';
- if (this.scheme)
- result += this.scheme + ':';
- if (this.authority)
- result += '//' + this.authority;
- result += this.path;
- if (this.query)
- result += '?' + this.query;
- if (this.fragment)
- result += '#' + this.fragment;
- return result;
- };
- return URI;
- });
|