blob: e53bca693f78b70f76b47f959dc95670cf31c89a [file] [log] [blame]
window.absolutify = function(url, baseUrl) {
/** returns a relative url in its absolute form, given by baseUrl.
*
* This function is a little odd, because it can take baseUrls that
* aren't necessarily directories. It uses the same rules as the HTML
* <base> tag; if the baseUrl doesn't end with "/", we'll assume
* that it points to a file, and strip the filename off to find its
* base directory.
*
* So absolutify("foo", "http://x/bar") will return "http://x/foo" (stripping off bar),
* whereas absolutify("foo", "http://x/bar/") will return "http://x/bar/foo" (preserving bar).
* Naturally absolutify("foo", "http://x") will return "http://x/foo", appropriately.
*
* @param url the url to make absolute; if this url is already absolute, we'll just return that, unchanged
* @param baseUrl the baseUrl from which we'll absolutify, following the rules above.
* @return 'url' if it was already absolute, or the absolutized version of url if it was not absolute.
*/
// DGF isn't there some library we could use for this?
if (/^\w+:/.test(url)) {
// it's already absolute
return url;
}
var loc;
try {
loc = parseUrl(baseUrl);
} catch (e) {
// is it an absolute windows file path? let's play the hero in that case
if (/^\w:\\/.test(baseUrl)) {
baseUrl = "file:///" + baseUrl.replace(/\\/g, "/");
loc = parseUrl(baseUrl);
} else {
throw new SeleniumError("baseUrl wasn't absolute: " + baseUrl);
}
}
loc.search = null;
loc.hash = null;
// if url begins with /, then that's the whole pathname
if (/^\//.test(url)) {
loc.pathname = url;
var result = reassembleLocation(loc);
return result;
}
// if pathname is null, then we'll just append "/" + the url
if (!loc.pathname) {
loc.pathname = "/" + url;
var result = reassembleLocation(loc);
return result;
}
// if pathname ends with /, just append url
if (/\/$/.test(loc.pathname)) {
loc.pathname += url;
var result = reassembleLocation(loc);
return result;
}
// if we're here, then the baseUrl has a pathname, but it doesn't end with /
// in that case, we replace everything after the final / with the relative url
loc.pathname = loc.pathname.replace(/[^\/\\]+$/, url);
var result = reassembleLocation(loc);
return result;
}
var URL_REGEX = /^((\w+):\/\/)(([^:]+):?([^@]+)?@)?([^\/\?:]*):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(.+)?/;
window.parseUrl = function(url) {
var fields = ['url', null, 'protocol', null, 'username', 'password', 'host', 'port', 'pathname', 'search', 'hash'];
var result = URL_REGEX.exec(url);
if (!result) {
throw new SeleniumError("Invalid URL: " + url);
}
var loc = new Object();
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
if (field == null) {
continue;
}
loc[field] = result[i];
}
return loc;
}
window.reassembleLocation = function(loc) {
if (!loc.protocol) {
throw new Error("Not a valid location object: " + o2s(loc));
}
var protocol = loc.protocol;
protocol = protocol.replace(/:$/, "");
var url = protocol + "://";
if (loc.username) {
url += loc.username;
if (loc.password) {
url += ":" + loc.password;
}
url += "@";
}
if (loc.host) {
url += loc.host;
}
if (loc.port) {
url += ":" + loc.port;
}
if (loc.pathname) {
url += loc.pathname;
}
if (loc.search) {
url += "?" + loc.search;
}
if (loc.hash) {
var hash = loc.hash;
hash = loc.hash.replace(/^#/, "");
url += "#" + hash;
}
return url;
}
window.canonicalize = function(url) {
if(url == "about:blank")
{
return url;
}
var tempLink = window.document.createElement("link");
tempLink.href = url; // this will canonicalize the href on most browsers
var loc = parseUrl(tempLink.href)
if (!/\/\.\.\//.test(loc.pathname)) {
return tempLink.href;
}
// didn't work... let's try it the hard way
var originalParts = loc.pathname.split("/");
var newParts = [];
newParts.push(originalParts.shift());
for (var i = 0; i < originalParts.length; i++) {
var part = originalParts[i];
if (".." == part) {
newParts.pop();
continue;
}
newParts.push(part);
}
loc.pathname = newParts.join("/");
return reassembleLocation(loc);
}