%PDF- %PDF-
| Direktori : /data/old/home/stash/atlassian-stash/static/bower/jsuri/src/ |
| Current File : //data/old/home/stash/atlassian-stash/static/bower/jsuri/src/uri.js |
var Uri = function (uriString) {
// uri string parsing, attribute manipulation and stringification
'use strict';
/*global Query: true */
/*jslint regexp: false, plusplus: false */
var strictMode = false,
// parseUri(str) parses the supplied uri and returns an object containing its components
parseUri = function (str) {
/*jslint unparam: true */
var parsers = {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
},
keys = ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"],
q = {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
m = parsers[strictMode ? "strict" : "loose"].exec(str),
uri = {},
i = 14;
while (i--) {
uri[keys[i]] = m[i] || "";
}
uri[q.name] = {};
uri[keys[12]].replace(q.parser, function ($0, $1, $2) {
if ($1) {
uri[q.name][$1] = $2;
}
});
return uri;
},
uriParts = parseUri(uriString || ''),
queryObj = new Query(uriParts.query),
/*
Basic get/set functions for all properties
*/
protocol = function (val) {
if (typeof val !== 'undefined') {
uriParts.protocol = val;
}
return uriParts.protocol;
},
hasAuthorityPrefixUserPref = null,
// hasAuthorityPrefix: if there is no protocol, the leading // can be enabled or disabled
hasAuthorityPrefix = function (val) {
if (typeof val !== 'undefined') {
hasAuthorityPrefixUserPref = val;
}
if (hasAuthorityPrefixUserPref === null) {
return (uriParts.source.indexOf('//') !== -1);
} else {
return hasAuthorityPrefixUserPref;
}
},
userInfo = function (val) {
if (typeof val !== 'undefined') {
uriParts.userInfo = val;
}
return uriParts.userInfo;
},
host = function (val) {
if (typeof val !== 'undefined') {
uriParts.host = val;
}
return uriParts.host;
},
port = function (val) {
if (typeof val !== 'undefined') {
uriParts.port = val;
}
return uriParts.port;
},
path = function (val) {
if (typeof val !== 'undefined') {
uriParts.path = val;
}
return uriParts.path;
},
query = function (val) {
if (typeof val !== 'undefined') {
queryObj = new Query(val);
}
return queryObj;
},
anchor = function (val) {
if (typeof val !== 'undefined') {
uriParts.anchor = val;
}
return uriParts.anchor;
},
/*
Fluent setters for Uri uri properties
*/
setProtocol = function (val) {
protocol(val);
return this;
},
setHasAuthorityPrefix = function (val) {
hasAuthorityPrefix(val);
return this;
},
setUserInfo = function (val) {
userInfo(val);
return this;
},
setHost = function (val) {
host(val);
return this;
},
setPort = function (val) {
port(val);
return this;
},
setPath = function (val) {
path(val);
return this;
},
setQuery = function (val) {
query(val);
return this;
},
setAnchor = function (val) {
anchor(val);
return this;
},
/*
Query method wrappers
*/
getQueryParamValue = function (key) {
return query().getParamValue(key);
},
getQueryParamValues = function (key) {
return query().getParamValues(key);
},
deleteQueryParam = function (key, val) {
if (arguments.length === 2) {
query().deleteParam(key, val);
} else {
query().deleteParam(key);
}
return this;
},
addQueryParam = function (key, val, index) {
if (arguments.length === 3) {
query().addParam(key, val, index);
} else {
query().addParam(key, val);
}
return this;
},
replaceQueryParam = function (key, newVal, oldVal) {
if (arguments.length === 3) {
query().replaceParam(key, newVal, oldVal);
} else {
query().replaceParam(key, newVal);
}
return this;
},
/*
Serialization
*/
// toString() stringifies the current state of the uri
toString = function () {
var s = '',
is = function (s) {
return (s !== null && s !== '');
};
if (is(protocol())) {
s += protocol();
if (protocol().indexOf(':') !== protocol().length - 1) {
s += ':';
}
s += '//';
} else {
if (hasAuthorityPrefix() && is(host())) {
s += '//';
}
}
if (is(userInfo()) && is(host())) {
s += userInfo();
if (userInfo().indexOf('@') !== userInfo().length - 1) {
s += '@';
}
}
if (is(host())) {
s += host();
if (is(port())) {
s += ':' + port();
}
}
if (is(path())) {
s += path();
} else {
if (is(host()) && (is(query().toString()) || is(anchor()))) {
s += '/';
}
}
if (is(query().toString())) {
if (query().toString().indexOf('?') !== 0) {
s += '?';
}
s += query().toString();
}
if (is(anchor())) {
if (anchor().indexOf('#') !== 0) {
s += '#';
}
s += anchor();
}
return s;
},
/*
Cloning
*/
// clone() returns a new, identical Uri instance
clone = function () {
return new Uri(toString());
};
// public api
return {
protocol: protocol,
hasAuthorityPrefix: hasAuthorityPrefix,
userInfo: userInfo,
host: host,
port: port,
path: path,
query: query,
anchor: anchor,
setProtocol: setProtocol,
setHasAuthorityPrefix: setHasAuthorityPrefix,
setUserInfo: setUserInfo,
setHost: setHost,
setPort: setPort,
setPath: setPath,
setQuery: setQuery,
setAnchor: setAnchor,
getQueryParamValue: getQueryParamValue,
getQueryParamValues: getQueryParamValues,
deleteQueryParam: deleteQueryParam,
addQueryParam: addQueryParam,
replaceQueryParam: replaceQueryParam,
toString: toString,
clone: clone
};
};