|
|
@ -20,17 +20,17 @@ var protocolPattern = /^([a-z0-9]+:)/, |
|
|
|
|
|
|
|
function urlParse (url, parseQueryString) { |
|
|
|
if (url && typeof(url) === "object" && url.href) return url; |
|
|
|
|
|
|
|
|
|
|
|
var out = { href : url }, |
|
|
|
rest = url; |
|
|
|
|
|
|
|
|
|
|
|
var proto = protocolPattern.exec(rest); |
|
|
|
if (proto) { |
|
|
|
proto = proto[0]; |
|
|
|
out.protocol = proto; |
|
|
|
rest = rest.substr(proto.length); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// figure out if it's got a host
|
|
|
|
var slashes = rest.substr(0, 2) === "//"; |
|
|
|
if (slashes && !(proto && hostlessProtocol[proto])) { |
|
|
@ -48,19 +48,19 @@ function urlParse (url, parseQueryString) { |
|
|
|
} |
|
|
|
if (firstNonHost !== -1) { |
|
|
|
out.host = rest.substr(0, firstNonHost); |
|
|
|
rest = rest.substr(firstNonHost); |
|
|
|
rest = rest.substr(firstNonHost); |
|
|
|
} else { |
|
|
|
out.host = rest; |
|
|
|
rest = ""; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// pull out the auth and port.
|
|
|
|
var p = parseHost(out.host); |
|
|
|
for (var i in p) out[i] = p[i]; |
|
|
|
// we've indicated that there is a hostname, so even if it's empty, it has to be present.
|
|
|
|
out.hostname = out.hostname || ""; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// now rest is set to the post-host stuff.
|
|
|
|
// chop off from the tail first.
|
|
|
|
var hash = rest.indexOf("#"); |
|
|
@ -79,7 +79,7 @@ function urlParse (url, parseQueryString) { |
|
|
|
rest = rest.slice(0, qm); |
|
|
|
} |
|
|
|
if (rest) out.pathname = rest; |
|
|
|
|
|
|
|
|
|
|
|
return out; |
|
|
|
}; |
|
|
|
|
|
|
@ -88,37 +88,37 @@ function urlFormat (obj) { |
|
|
|
// ensure it's an object, and not a string url. If it's an obj, this is a no-op.
|
|
|
|
// this way, you can call url_format() on strings to clean up potentially wonky urls.
|
|
|
|
if (typeof(obj) === "string") obj = urlParse(obj); |
|
|
|
|
|
|
|
|
|
|
|
var protocol = obj.protocol || "", |
|
|
|
host = (obj.host !== undefined) ? obj.host |
|
|
|
: obj.hostname !== undefined ? ( |
|
|
|
(obj.auth ? obj.auth + "@" : "") |
|
|
|
+ obj.hostname |
|
|
|
+ (obj.port ? ":" + obj.port : "") |
|
|
|
) |
|
|
|
) |
|
|
|
: false, |
|
|
|
pathname = obj.pathname || "", |
|
|
|
search = obj.search || ( |
|
|
|
obj.query && ( "?" + ( |
|
|
|
typeof(obj.query) === "object" |
|
|
|
typeof(obj.query) === "object" |
|
|
|
? querystring.stringify(obj.query) |
|
|
|
: String(obj.query) |
|
|
|
)) |
|
|
|
) || "", |
|
|
|
hash = obj.hash || ""; |
|
|
|
|
|
|
|
|
|
|
|
if (protocol && protocol.substr(-1) !== ":") protocol += ":"; |
|
|
|
|
|
|
|
|
|
|
|
// only the slashedProtocols get the //. Not mailto:, xmpp:, etc.
|
|
|
|
// unless they had them to begin with.
|
|
|
|
if (obj.slashes || (!protocol || slashedProtocol[protocol]) && host !== false) { |
|
|
|
host = "//" + (host || ""); |
|
|
|
if (pathname && pathname.charAt(0) !== "/") pathname = "/" + pathname; |
|
|
|
} else if (!host) host = ""; |
|
|
|
|
|
|
|
|
|
|
|
if (hash && hash.charAt(0) !== "#") hash = "#" + hash; |
|
|
|
if (search && search.charAt(0) !== "?") search = "?" + search; |
|
|
|
|
|
|
|
|
|
|
|
return protocol + host + pathname + search + hash; |
|
|
|
}; |
|
|
|
|
|
|
@ -128,30 +128,30 @@ function urlResolve (source, relative) { |
|
|
|
|
|
|
|
function urlResolveObject (source, relative) { |
|
|
|
if (!source) return relative; |
|
|
|
|
|
|
|
|
|
|
|
source = urlParse(urlFormat(source)); |
|
|
|
relative = urlParse(urlFormat(relative)); |
|
|
|
|
|
|
|
// hash is always overridden, no matter what.
|
|
|
|
source.hash = relative.hash; |
|
|
|
|
|
|
|
|
|
|
|
if (relative.href === "") return source; |
|
|
|
|
|
|
|
|
|
|
|
// hrefs like //foo/bar always cut to the protocol.
|
|
|
|
if (relative.slashes && !relative.protocol) { |
|
|
|
relative.protocol = source.protocol; |
|
|
|
return relative; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (relative.protocol && relative.protocol !== source.protocol) { |
|
|
|
// if it's a known url protocol, then changing the protocol does weird things
|
|
|
|
// first, if it's not file:, then we MUST have a host, and if there was a path
|
|
|
|
// to begin with, then we MUST have a path.
|
|
|
|
// if it is file:, then the host is dropped, because that's known to be hostless.
|
|
|
|
// anything else is assumed to be absolute.
|
|
|
|
|
|
|
|
|
|
|
|
if (!slashedProtocol[relative.protocol]) return relative; |
|
|
|
|
|
|
|
|
|
|
|
source.protocol = relative.protocol; |
|
|
|
if (!relative.host && !hostlessProtocol[relative.protocol]) { |
|
|
|
var relPath = (relative.pathname || "").split("/"); |
|
|
@ -181,13 +181,13 @@ function urlResolveObject (source, relative) { |
|
|
|
srcPath = source.pathname && source.pathname.split("/") || [], |
|
|
|
relPath = relative.pathname && relative.pathname.split("/") || [], |
|
|
|
psychotic = source.protocol && !slashedProtocol[source.protocol] && source.host !== undefined; |
|
|
|
|
|
|
|
|
|
|
|
// if the url is a non-slashed url, then relative links like ../.. should be able
|
|
|
|
// to crawl up to the hostname, as well. This is strange.
|
|
|
|
// source.protocol has already been set by now.
|
|
|
|
// Later on, put the first path part into the host field.
|
|
|
|
if ( psychotic ) { |
|
|
|
|
|
|
|
|
|
|
|
delete source.hostname; |
|
|
|
delete source.auth; |
|
|
|
delete source.port; |
|
|
@ -196,7 +196,7 @@ function urlResolveObject (source, relative) { |
|
|
|
else srcPath.unshift(source.host); |
|
|
|
} |
|
|
|
delete source.host; |
|
|
|
|
|
|
|
|
|
|
|
if (relative.protocol) { |
|
|
|
delete relative.hostname; |
|
|
|
delete relative.auth; |
|
|
@ -209,7 +209,7 @@ function urlResolveObject (source, relative) { |
|
|
|
} |
|
|
|
mustEndAbs = mustEndAbs && (relPath[0] === "" || srcPath[0] === ""); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (isRelAbs) { |
|
|
|
// it's absolute.
|
|
|
|
source.host = (relative.host || relative.host === "") ? relative.host : source.host; |
|
|
@ -242,7 +242,7 @@ function urlResolveObject (source, relative) { |
|
|
|
delete source.pathname; |
|
|
|
return source; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// resolve dots.
|
|
|
|
// if a url ENDs in . or .., then it must get a trailing slash.
|
|
|
|
// however, if it ends in anything else non-slashy, then it must NOT get a trailing slash.
|
|
|
@ -251,7 +251,7 @@ function urlResolveObject (source, relative) { |
|
|
|
(source.host || relative.host) && (last === "." || last === "..") |
|
|
|
|| last === "" |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// Figure out if this has to end up as an absolute url, or should continue to be relative.
|
|
|
|
srcPath = path.normalizeArray(srcPath, true); |
|
|
|
if (srcPath.length === 1 && srcPath[0] === ".") srcPath = []; |
|
|
@ -262,23 +262,23 @@ function urlResolveObject (source, relative) { |
|
|
|
if (dir === "..") dirs.pop(); |
|
|
|
else if (dir !== ".") dirs.push(dir); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (mustEndAbs && dirs[0] !== "") { |
|
|
|
dirs.unshift(""); |
|
|
|
} |
|
|
|
srcPath = dirs; |
|
|
|
} |
|
|
|
if (hasTrailingSlash && (srcPath.length < 2 || srcPath.slice(-1)[0] !== "")) srcPath.push(""); |
|
|
|
|
|
|
|
|
|
|
|
// put the host back
|
|
|
|
if ( psychotic ) source.host = srcPath[0] === "" ? "" : srcPath.shift(); |
|
|
|
|
|
|
|
|
|
|
|
mustEndAbs = mustEndAbs || (source.host && srcPath.length); |
|
|
|
|
|
|
|
|
|
|
|
if (mustEndAbs && srcPath[0] !== "") srcPath.unshift("") |
|
|
|
|
|
|
|
source.pathname = srcPath.join("/"); |
|
|
|
|
|
|
|
|
|
|
|
return source; |
|
|
|
}; |
|
|
|
|
|
|
|