-
-
Save pjt33/efb2f1134bab986113fd 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
/*jslint regexp: true, maxerr: 50, indent: 2 */ | |
(function (global) { | |
"use strict"; | |
function URLUtils(url, baseURL) { | |
var m = String(url).replace(/^\s+|\s+$/g, "").match(/^([^:\/?#]+:)?(?:\/\/(?:([^:@]*)(?::([^:@]*))?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/); | |
if (!m) { | |
throw new RangeError(); | |
} | |
var href = m[0] || ""; | |
var protocol = m[1] || ""; | |
var username = m[2] || ""; | |
var password = m[3] || ""; | |
var host = m[4] || ""; | |
var hostname = m[5] || ""; | |
var port = m[6] || ""; | |
var pathname = m[7] || ""; | |
var search = m[8] || ""; | |
var hash = m[9] || ""; | |
if (baseURL !== undefined) { | |
var base = new URLUtils(baseURL); | |
var flag = protocol === "" && host === "" && username === ""; | |
if (flag && pathname === "" && search === "") { | |
search = base.search; | |
} | |
if (flag && pathname.charAt(0) !== "/") { | |
pathname = (pathname !== "" ? (base.pathname.slice(0, base.pathname.lastIndexOf("/") + 1) + pathname) : base.pathname); | |
} | |
// dot segments removal | |
var output = []; | |
pathname.replace(/^(\.\.?(\/|$))+/, "") | |
.replace(/\/(\.(\/|$))+/g, "/") | |
.replace(/\/\.\.$/, "/../") | |
.replace(/\/?[^\/]*/g, function (p) { | |
if (p === "/..") { | |
output.pop(); | |
} else { | |
output.push(p); | |
} | |
}); | |
pathname = output.join("").replace(/^\//, pathname.charAt(0) === "/" ? "/" : ""); | |
if (flag) { | |
port = base.port; | |
hostname = base.hostname; | |
host = base.host; | |
password = base.password; | |
username = base.username; | |
} | |
if (protocol === "") { | |
protocol = base.protocol; | |
} | |
href = protocol + (host !== "" ? "//" : "") + (username !== "" ? username + (password !== "" ? ":" + password : "") + "@" : "") + host + pathname + search + hash; | |
} | |
this.href = href; | |
this.origin = protocol + (host !== "" ? "//" + host : ""); | |
this.protocol = protocol; | |
this.username = username; | |
this.password = password; | |
this.host = host; | |
this.hostname = hostname; | |
this.port = port; | |
this.pathname = pathname; | |
this.search = search; | |
this.hash = hash; | |
} | |
global.URLUtils = URLUtils; | |
}(this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment