Guest User

Untitled

a guest
Jun 22nd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. /*jslint regexp: true, maxerr: 50, indent: 2 */
  2.  
  3. (function (global) {
  4. "use strict";
  5.  
  6. function URLUtils(url, baseURL) {
  7. var m = String(url).replace(/^\s+|\s+$/g, "").match(/^([^:\/?#]+:)?(?:\/\/(?:([^:@\/?#]*)(?::([^:@\/?#]*))?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);
  8. if (!m) {
  9. throw new RangeError();
  10. }
  11. var protocol = m[1] || "";
  12. var username = m[2] || "";
  13. var password = m[3] || "";
  14. var host = m[4] || "";
  15. var hostname = m[5] || "";
  16. var port = m[6] || "";
  17. var pathname = m[7] || "";
  18. var search = m[8] || "";
  19. var hash = m[9] || "";
  20. if (baseURL !== undefined) {
  21. var base = new URLUtils(baseURL);
  22. var flag = protocol === "" && host === "" && username === "";
  23. if (flag && pathname === "" && search === "") {
  24. search = base.search;
  25. }
  26. if (flag && pathname.charAt(0) !== "/") {
  27. pathname = (pathname !== "" ? (((base.host !== "" || base.username !== "") && base.pathname === "" ? "/" : "") + base.pathname.slice(0, base.pathname.lastIndexOf("/") + 1) + pathname) : base.pathname);
  28. }
  29. // dot segments removal
  30. var output = [];
  31. pathname.replace(/^(\.\.?(\/|$))+/, "")
  32. .replace(/\/(\.(\/|$))+/g, "/")
  33. .replace(/\/\.\.$/, "/../")
  34. .replace(/\/?[^\/]*/g, function (p) {
  35. if (p === "/..") {
  36. output.pop();
  37. } else {
  38. output.push(p);
  39. }
  40. });
  41. pathname = output.join("").replace(/^\//, pathname.charAt(0) === "/" ? "/" : "");
  42. if (flag) {
  43. port = base.port;
  44. hostname = base.hostname;
  45. host = base.host;
  46. password = base.password;
  47. username = base.username;
  48. }
  49. if (protocol === "") {
  50. protocol = base.protocol;
  51. }
  52. }
  53. this.origin = protocol + (protocol !== "" || host !== "" ? "//" : "") + host;
  54. this.href = protocol + (protocol !== "" || host !== "" ? "//" : "") + (username !== "" ? username + (password !== "" ? ":" + password : "") + "@" : "") + host + pathname + search + hash;
  55. this.protocol = protocol;
  56. this.username = username;
  57. this.password = password;
  58. this.host = host;
  59. this.hostname = hostname;
  60. this.port = port;
  61. this.pathname = pathname;
  62. this.search = search;
  63. this.hash = hash;
  64. }
  65.  
  66. global.URLUtils = URLUtils;
  67.  
  68. }(this));
Add Comment
Please, Sign In to add comment