mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-04-05 21:01:35 +08:00
1 line
27 KiB
JavaScript
1 line
27 KiB
JavaScript
!function(t,e){"use strict";"object"==typeof exports?module.exports=e(require("./punycode"),require("./IPv6"),require("./SecondLevelDomains")):"function"==typeof define&&define.amd?define(["./punycode","./IPv6","./SecondLevelDomains"],e):t.URI=e(t.punycode,t.IPv6,t.SecondLevelDomains,t)}(this,function(t,e,r,s){"use strict";function n(t,e){var r=arguments.length>=1,s=arguments.length>=2;if(!(this instanceof n))return r?s?new n(t,e):new n(t):new n;if(void 0===t){if(r)throw new TypeError("undefined is not a valid argument for URI");t="undefined"!=typeof location?location.href+"":""}return this.href(t),void 0!==e?this.absoluteTo(e):this}function i(t){return t.replace(/([.*+?^=!:${}()|[\]\/\\])/g,"\\$1")}function a(t){return void 0===t?"Undefined":String(Object.prototype.toString.call(t)).slice(8,-1)}function o(t){return"Array"===a(t)}function h(t,e){var r,s,n={};if("RegExp"===a(e))n=null;else if(o(e))for(r=0,s=e.length;s>r;r++)n[e[r]]=!0;else n[e]=!0;for(r=0,s=t.length;s>r;r++){var i=n&&void 0!==n[t[r]]||!n&&e.test(t[r]);i&&(t.splice(r,1),s--,r--)}return t}function u(t,e){var r,s;if(o(e)){for(r=0,s=e.length;s>r;r++)if(!u(t,e[r]))return!1;return!0}var n=a(e);for(r=0,s=t.length;s>r;r++)if("RegExp"===n){if("string"==typeof t[r]&&t[r].match(e))return!0}else if(t[r]===e)return!0;return!1}function p(t,e){if(!o(t)||!o(e))return!1;if(t.length!==e.length)return!1;t.sort(),e.sort();for(var r=0,s=t.length;s>r;r++)if(t[r]!==e[r])return!1;return!0}function c(t){return escape(t)}function l(t){return encodeURIComponent(t).replace(/[!'()*]/g,c).replace(/\*/g,"%2A")}function d(t){return function(e,r){return void 0===e?this._parts[t]||"":(this._parts[t]=e||null,this.build(!r),this)}}function f(t,e){return function(r,s){return void 0===r?this._parts[t]||"":(null!==r&&(r+="",r.charAt(0)===e&&(r=r.substring(1))),this._parts[t]=r,this.build(!s),this)}}var m=s&&s.URI;n.version="1.16.1";var g=n.prototype,_=Object.prototype.hasOwnProperty;n._parts=function(){return{protocol:null,username:null,password:null,hostname:null,urn:null,port:null,path:null,query:null,fragment:null,duplicateQueryParameters:n.duplicateQueryParameters,escapeQuerySpace:n.escapeQuerySpace}},n.duplicateQueryParameters=!1,n.escapeQuerySpace=!0,n.protocol_expression=/^[a-z][a-z0-9.+-]*$/i,n.idn_expression=/[^a-z0-9\.-]/i,n.punycode_expression=/(xn--)/i,n.ip4_expression=/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/,n.ip6_expression=/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/,n.find_uri_expression=/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi,n.findUri={start:/\b(?:([a-z][a-z0-9.+-]*:\/\/)|www\.)/gi,end:/[\s\r\n]|$/,trim:/[`!()\[\]{};:'".,<>?«»“”„‘’]+$/},n.defaultPorts={http:"80",https:"443",ftp:"21",gopher:"70",ws:"80",wss:"443"},n.invalid_hostname_characters=/[^a-zA-Z0-9\.-]/,n.domAttributes={a:"href",blockquote:"cite",link:"href",base:"href",script:"src",form:"action",img:"src",area:"href",iframe:"src",embed:"src",source:"src",track:"src",input:"src",audio:"src",video:"src"},n.getDomAttribute=function(t){if(!t||!t.nodeName)return void 0;var e=t.nodeName.toLowerCase();return"input"===e&&"image"!==t.type?void 0:n.domAttributes[e]},n.encode=l,n.decode=decodeURIComponent,n.iso8859=function(){n.encode=escape,n.decode=unescape},n.unicode=function(){n.encode=l,n.decode=decodeURIComponent},n.characters={pathname:{encode:{expression:/%(24|26|2B|2C|3B|3D|3A|40)/gi,map:{"%24":"$","%26":"&","%2B":"+","%2C":",","%3B":";","%3D":"=","%3A":":","%40":"@"}},decode:{expression:/[\/\?#]/g,map:{"/":"%2F","?":"%3F","#":"%23"}}},reserved:{encode:{expression:/%(21|23|24|26|27|28|29|2A|2B|2C|2F|3A|3B|3D|3F|40|5B|5D)/gi,map:{"%3A":":","%2F":"/","%3F":"?","%23":"#","%5B":"[","%5D":"]","%40":"@","%21":"!","%24":"$","%26":"&","%27":"'","%28":"(","%29":")","%2A":"*","%2B":"+","%2C":",","%3B":";","%3D":"="}}},urnpath:{encode:{expression:/%(21|24|27|28|29|2A|2B|2C|3B|3D|40)/gi,map:{"%21":"!","%24":"$","%27":"'","%28":"(","%29":")","%2A":"*","%2B":"+","%2C":",","%3B":";","%3D":"=","%40":"@"}},decode:{expression:/[\/\?#:]/g,map:{"/":"%2F","?":"%3F","#":"%23",":":"%3A"}}}},n.encodeQuery=function(t,e){var r=n.encode(t+"");return void 0===e&&(e=n.escapeQuerySpace),e?r.replace(/%20/g,"+"):r},n.decodeQuery=function(t,e){t+="",void 0===e&&(e=n.escapeQuerySpace);try{return n.decode(e?t.replace(/\+/g,"%20"):t)}catch(r){return t}};var y,v={encode:"encode",decode:"decode"},b=function(t,e){return function(r){try{return n[e](r+"").replace(n.characters[t][e].expression,function(r){return n.characters[t][e].map[r]})}catch(s){return r}}};for(y in v)n[y+"PathSegment"]=b("pathname",v[y]),n[y+"UrnPathSegment"]=b("urnpath",v[y]);var w=function(t,e,r){return function(s){var i;i=r?function(t){return n[e](n[r](t))}:n[e];for(var a=(s+"").split(t),o=0,h=a.length;h>o;o++)a[o]=i(a[o]);return a.join(t)}};n.decodePath=w("/","decodePathSegment"),n.decodeUrnPath=w(":","decodeUrnPathSegment"),n.recodePath=w("/","encodePathSegment","decode"),n.recodeUrnPath=w(":","encodeUrnPathSegment","decode"),n.encodeReserved=b("reserved","encode"),n.parse=function(t,e){var r;return e||(e={}),r=t.indexOf("#"),r>-1&&(e.fragment=t.substring(r+1)||null,t=t.substring(0,r)),r=t.indexOf("?"),r>-1&&(e.query=t.substring(r+1)||null,t=t.substring(0,r)),"//"===t.substring(0,2)?(e.protocol=null,t=t.substring(2),t=n.parseAuthority(t,e)):(r=t.indexOf(":"),r>-1&&(e.protocol=t.substring(0,r)||null,e.protocol&&!e.protocol.match(n.protocol_expression)?e.protocol=void 0:"//"===t.substring(r+1,r+3)?(t=t.substring(r+3),t=n.parseAuthority(t,e)):(t=t.substring(r+1),e.urn=!0))),e.path=t,e},n.parseHost=function(t,e){t=t.replace(/\\/g,"/");var r,s,n=t.indexOf("/");if(-1===n&&(n=t.length),"["===t.charAt(0))r=t.indexOf("]"),e.hostname=t.substring(1,r)||null,e.port=t.substring(r+2,n)||null,"/"===e.port&&(e.port=null);else{var i=t.indexOf(":"),a=t.indexOf("/"),o=t.indexOf(":",i+1);-1!==o&&(-1===a||a>o)?(e.hostname=t.substring(0,n)||null,e.port=null):(s=t.substring(0,n).split(":"),e.hostname=s[0]||null,e.port=s[1]||null)}return e.hostname&&"/"!==t.substring(n).charAt(0)&&(n++,t="/"+t),t.substring(n)||"/"},n.parseAuthority=function(t,e){return t=n.parseUserinfo(t,e),n.parseHost(t,e)},n.parseUserinfo=function(t,e){var r,s=t.indexOf("/"),i=t.lastIndexOf("@",s>-1?s:t.length-1);return i>-1&&(-1===s||s>i)?(r=t.substring(0,i).split(":"),e.username=r[0]?n.decode(r[0]):null,r.shift(),e.password=r[0]?n.decode(r.join(":")):null,t=t.substring(i+1)):(e.username=null,e.password=null),t},n.parseQuery=function(t,e){if(!t)return{};if(t=t.replace(/&+/g,"&").replace(/^\?*&*|&+$/g,""),!t)return{};for(var r,s,i,a={},o=t.split("&"),h=o.length,u=0;h>u;u++)r=o[u].split("="),s=n.decodeQuery(r.shift(),e),i=r.length?n.decodeQuery(r.join("="),e):null,_.call(a,s)?(("string"==typeof a[s]||null===a[s])&&(a[s]=[a[s]]),a[s].push(i)):a[s]=i;return a},n.build=function(t){var e="";return t.protocol&&(e+=t.protocol+":"),t.urn||!e&&!t.hostname||(e+="//"),e+=n.buildAuthority(t)||"","string"==typeof t.path&&("/"!==t.path.charAt(0)&&"string"==typeof t.hostname&&(e+="/"),e+=t.path),"string"==typeof t.query&&t.query&&(e+="?"+t.query),"string"==typeof t.fragment&&t.fragment&&(e+="#"+t.fragment),e},n.buildHost=function(t){var e="";return t.hostname?(e+=n.ip6_expression.test(t.hostname)?"["+t.hostname+"]":t.hostname,t.port&&(e+=":"+t.port),e):""},n.buildAuthority=function(t){return n.buildUserinfo(t)+n.buildHost(t)},n.buildUserinfo=function(t){var e="";return t.username&&(e+=n.encode(t.username),t.password&&(e+=":"+n.encode(t.password)),e+="@"),e},n.buildQuery=function(t,e,r){var s,i,a,h,u="";for(i in t)if(_.call(t,i)&&i)if(o(t[i]))for(s={},a=0,h=t[i].length;h>a;a++)void 0!==t[i][a]&&void 0===s[t[i][a]+""]&&(u+="&"+n.buildQueryParameter(i,t[i][a],r),e!==!0&&(s[t[i][a]+""]=!0));else void 0!==t[i]&&(u+="&"+n.buildQueryParameter(i,t[i],r));return u.substring(1)},n.buildQueryParameter=function(t,e,r){return n.encodeQuery(t,r)+(null!==e?"="+n.encodeQuery(e,r):"")},n.addQuery=function(t,e,r){if("object"==typeof e)for(var s in e)_.call(e,s)&&n.addQuery(t,s,e[s]);else{if("string"!=typeof e)throw new TypeError("URI.addQuery() accepts an object, string as the name parameter");if(void 0===t[e])return void(t[e]=r);"string"==typeof t[e]&&(t[e]=[t[e]]),o(r)||(r=[r]),t[e]=(t[e]||[]).concat(r)}},n.removeQuery=function(t,e,r){var s,i,u;if(o(e))for(s=0,i=e.length;i>s;s++)t[e[s]]=void 0;else if("RegExp"===a(e))for(u in t)e.test(u)&&(t[u]=void 0);else if("object"==typeof e)for(u in e)_.call(e,u)&&n.removeQuery(t,u,e[u]);else{if("string"!=typeof e)throw new TypeError("URI.removeQuery() accepts an object, string, RegExp as the first parameter");void 0!==r?"RegExp"===a(r)?!o(t[e])&&r.test(t[e])?t[e]=void 0:t[e]=h(t[e],r):t[e]===r?t[e]=void 0:o(t[e])&&(t[e]=h(t[e],r)):t[e]=void 0}},n.hasQuery=function(t,e,r,s){if("object"==typeof e){for(var i in e)if(_.call(e,i)&&!n.hasQuery(t,i,e[i]))return!1;return!0}if("string"!=typeof e)throw new TypeError("URI.hasQuery() accepts an object, string as the name parameter");switch(a(r)){case"Undefined":return e in t;case"Boolean":var h=Boolean(o(t[e])?t[e].length:t[e]);return r===h;case"Function":return!!r(t[e],e,t);case"Array":if(!o(t[e]))return!1;var c=s?u:p;return c(t[e],r);case"RegExp":return o(t[e])?s?u(t[e],r):!1:Boolean(t[e]&&t[e].match(r));case"Number":r=String(r);case"String":return o(t[e])?s?u(t[e],r):!1:t[e]===r;default:throw new TypeError("URI.hasQuery() accepts undefined, boolean, string, number, RegExp, Function as the value parameter")}},n.commonPath=function(t,e){var r,s=Math.min(t.length,e.length);for(r=0;s>r;r++)if(t.charAt(r)!==e.charAt(r)){r--;break}return 1>r?t.charAt(0)===e.charAt(0)&&"/"===t.charAt(0)?"/":"":(("/"!==t.charAt(r)||"/"!==e.charAt(r))&&(r=t.substring(0,r).lastIndexOf("/")),t.substring(0,r+1))},n.withinString=function(t,e,r){r||(r={});var s=r.start||n.findUri.start,i=r.end||n.findUri.end,a=r.trim||n.findUri.trim,o=/[a-z0-9-]=["']?$/i;for(s.lastIndex=0;;){var h=s.exec(t);if(!h)break;var u=h.index;if(r.ignoreHtml){var p=t.slice(Math.max(u-3,0),u);if(p&&o.test(p))continue}var c=u+t.slice(u).search(i),l=t.slice(u,c).replace(a,"");if(!r.ignore||!r.ignore.test(l)){c=u+l.length;var d=e(l,u,c,t);t=t.slice(0,u)+d+t.slice(c),s.lastIndex=u+d.length}}return s.lastIndex=0,t},n.ensureValidHostname=function(e){if(e.match(n.invalid_hostname_characters)){if(!t)throw new TypeError('Hostname "'+e+'" contains characters other than [A-Z0-9.-] and Punycode.js is not available');if(t.toASCII(e).match(n.invalid_hostname_characters))throw new TypeError('Hostname "'+e+'" contains characters other than [A-Z0-9.-]')}},n.noConflict=function(t){if(t){var e={URI:this.noConflict()};return s.URITemplate&&"function"==typeof s.URITemplate.noConflict&&(e.URITemplate=s.URITemplate.noConflict()),s.IPv6&&"function"==typeof s.IPv6.noConflict&&(e.IPv6=s.IPv6.noConflict()),s.SecondLevelDomains&&"function"==typeof s.SecondLevelDomains.noConflict&&(e.SecondLevelDomains=s.SecondLevelDomains.noConflict()),e}return s.URI===this&&(s.URI=m),this},g.build=function(t){return t===!0?this._deferred_build=!0:(void 0===t||this._deferred_build)&&(this._string=n.build(this._parts),this._deferred_build=!1),this},g.clone=function(){return new n(this)},g.valueOf=g.toString=function(){return this.build(!1)._string},g.protocol=d("protocol"),g.username=d("username"),g.password=d("password"),g.hostname=d("hostname"),g.port=d("port"),g.query=f("query","?"),g.fragment=f("fragment","#"),g.search=function(t,e){var r=this.query(t,e);return"string"==typeof r&&r.length?"?"+r:r},g.hash=function(t,e){var r=this.fragment(t,e);return"string"==typeof r&&r.length?"#"+r:r},g.pathname=function(t,e){if(void 0===t||t===!0){var r=this._parts.path||(this._parts.hostname?"/":"");return t?(this._parts.urn?n.decodeUrnPath:n.decodePath)(r):r}return this._parts.urn?this._parts.path=t?n.recodeUrnPath(t):"":this._parts.path=t?n.recodePath(t):"/",this.build(!e),this},g.path=g.pathname,g.href=function(t,e){var r;if(void 0===t)return this.toString();this._string="",this._parts=n._parts();var s=t instanceof n,i="object"==typeof t&&(t.hostname||t.path||t.pathname);if(t.nodeName){var a=n.getDomAttribute(t);t=t[a]||"",i=!1}if(!s&&i&&void 0!==t.pathname&&(t=t.toString()),"string"==typeof t||t instanceof String)this._parts=n.parse(String(t),this._parts);else{if(!s&&!i)throw new TypeError("invalid input");var o=s?t._parts:t;for(r in o)_.call(this._parts,r)&&(this._parts[r]=o[r])}return this.build(!e),this},g.is=function(t){var e=!1,s=!1,i=!1,a=!1,o=!1,h=!1,u=!1,p=!this._parts.urn;switch(this._parts.hostname&&(p=!1,s=n.ip4_expression.test(this._parts.hostname),i=n.ip6_expression.test(this._parts.hostname),e=s||i,a=!e,o=a&&r&&r.has(this._parts.hostname),h=a&&n.idn_expression.test(this._parts.hostname),u=a&&n.punycode_expression.test(this._parts.hostname)),t.toLowerCase()){case"relative":return p;case"absolute":return!p;case"domain":case"name":return a;case"sld":return o;case"ip":return e;case"ip4":case"ipv4":case"inet4":return s;case"ip6":case"ipv6":case"inet6":return i;case"idn":return h;case"url":return!this._parts.urn;case"urn":return!!this._parts.urn;case"punycode":return u}return null};var Q=g.protocol,A=g.port,P=g.hostname;g.protocol=function(t,e){if(void 0!==t&&t&&(t=t.replace(/:(\/\/)?$/,""),!t.match(n.protocol_expression)))throw new TypeError('Protocol "'+t+"\" contains characters other than [A-Z0-9.+-] or doesn't start with [A-Z]");return Q.call(this,t,e)},g.scheme=g.protocol,g.port=function(t,e){if(this._parts.urn)return void 0===t?"":this;if(void 0!==t&&(0===t&&(t=null),t&&(t+="",":"===t.charAt(0)&&(t=t.substring(1)),t.match(/[^0-9]/))))throw new TypeError('Port "'+t+'" contains characters other than [0-9]');return A.call(this,t,e)},g.hostname=function(t,e){if(this._parts.urn)return void 0===t?"":this;if(void 0!==t){var r={},s=n.parseHost(t,r);if("/"!==s)throw new TypeError('Hostname "'+t+'" contains characters other than [A-Z0-9.-]');t=r.hostname}return P.call(this,t,e)},g.host=function(t,e){if(this._parts.urn)return void 0===t?"":this;if(void 0===t)return this._parts.hostname?n.buildHost(this._parts):"";var r=n.parseHost(t,this._parts);if("/"!==r)throw new TypeError('Hostname "'+t+'" contains characters other than [A-Z0-9.-]');return this.build(!e),this},g.authority=function(t,e){if(this._parts.urn)return void 0===t?"":this;if(void 0===t)return this._parts.hostname?n.buildAuthority(this._parts):"";var r=n.parseAuthority(t,this._parts);if("/"!==r)throw new TypeError('Hostname "'+t+'" contains characters other than [A-Z0-9.-]');return this.build(!e),this},g.userinfo=function(t,e){if(this._parts.urn)return void 0===t?"":this;if(void 0===t){if(!this._parts.username)return"";var r=n.buildUserinfo(this._parts);return r.substring(0,r.length-1)}return"@"!==t[t.length-1]&&(t+="@"),n.parseUserinfo(t,this._parts),this.build(!e),this},g.resource=function(t,e){var r;return void 0===t?this.path()+this.search()+this.hash():(r=n.parse(t),this._parts.path=r.path,this._parts.query=r.query,this._parts.fragment=r.fragment,this.build(!e),this)},g.subdomain=function(t,e){if(this._parts.urn)return void 0===t?"":this;if(void 0===t){if(!this._parts.hostname||this.is("IP"))return"";var r=this._parts.hostname.length-this.domain().length-1;return this._parts.hostname.substring(0,r)||""}var s=this._parts.hostname.length-this.domain().length,a=this._parts.hostname.substring(0,s),o=new RegExp("^"+i(a));return t&&"."!==t.charAt(t.length-1)&&(t+="."),t&&n.ensureValidHostname(t),this._parts.hostname=this._parts.hostname.replace(o,t),this.build(!e),this},g.domain=function(t,e){if(this._parts.urn)return void 0===t?"":this;if("boolean"==typeof t&&(e=t,t=void 0),void 0===t){if(!this._parts.hostname||this.is("IP"))return"";var r=this._parts.hostname.match(/\./g);if(r&&r.length<2)return this._parts.hostname;var s=this._parts.hostname.length-this.tld(e).length-1;return s=this._parts.hostname.lastIndexOf(".",s-1)+1,this._parts.hostname.substring(s)||""}if(!t)throw new TypeError("cannot set domain empty");if(n.ensureValidHostname(t),!this._parts.hostname||this.is("IP"))this._parts.hostname=t;else{var a=new RegExp(i(this.domain())+"$");this._parts.hostname=this._parts.hostname.replace(a,t)}return this.build(!e),this},g.tld=function(t,e){if(this._parts.urn)return void 0===t?"":this;if("boolean"==typeof t&&(e=t,t=void 0),void 0===t){if(!this._parts.hostname||this.is("IP"))return"";var s=this._parts.hostname.lastIndexOf("."),n=this._parts.hostname.substring(s+1);return e!==!0&&r&&r.list[n.toLowerCase()]?r.get(this._parts.hostname)||n:n}var a;if(!t)throw new TypeError("cannot set TLD empty");if(t.match(/[^a-zA-Z0-9-]/)){if(!r||!r.is(t))throw new TypeError('TLD "'+t+'" contains characters other than [A-Z0-9]');a=new RegExp(i(this.tld())+"$"),this._parts.hostname=this._parts.hostname.replace(a,t)}else{if(!this._parts.hostname||this.is("IP"))throw new ReferenceError("cannot set TLD on non-domain host");a=new RegExp(i(this.tld())+"$"),this._parts.hostname=this._parts.hostname.replace(a,t)}return this.build(!e),this},g.directory=function(t,e){if(this._parts.urn)return void 0===t?"":this;if(void 0===t||t===!0){if(!this._parts.path&&!this._parts.hostname)return"";if("/"===this._parts.path)return"/";var r=this._parts.path.length-this.filename().length-1,s=this._parts.path.substring(0,r)||(this._parts.hostname?"/":"");return t?n.decodePath(s):s}var a=this._parts.path.length-this.filename().length,o=this._parts.path.substring(0,a),h=new RegExp("^"+i(o));return this.is("relative")||(t||(t="/"),"/"!==t.charAt(0)&&(t="/"+t)),t&&"/"!==t.charAt(t.length-1)&&(t+="/"),t=n.recodePath(t),this._parts.path=this._parts.path.replace(h,t),this.build(!e),this},g.filename=function(t,e){if(this._parts.urn)return void 0===t?"":this;if(void 0===t||t===!0){if(!this._parts.path||"/"===this._parts.path)return"";var r=this._parts.path.lastIndexOf("/"),s=this._parts.path.substring(r+1);return t?n.decodePathSegment(s):s}var a=!1;"/"===t.charAt(0)&&(t=t.substring(1)),t.match(/\.?\//)&&(a=!0);var o=new RegExp(i(this.filename())+"$");return t=n.recodePath(t),this._parts.path=this._parts.path.replace(o,t),a?this.normalizePath(e):this.build(!e),this},g.suffix=function(t,e){if(this._parts.urn)return void 0===t?"":this;if(void 0===t||t===!0){if(!this._parts.path||"/"===this._parts.path)return"";var r,s,a=this.filename(),o=a.lastIndexOf(".");return-1===o?"":(r=a.substring(o+1),s=/^[a-z0-9%]+$/i.test(r)?r:"",t?n.decodePathSegment(s):s)}"."===t.charAt(0)&&(t=t.substring(1));var h,u=this.suffix();if(u)h=t?new RegExp(i(u)+"$"):new RegExp(i("."+u)+"$");else{if(!t)return this;this._parts.path+="."+n.recodePath(t)}return h&&(t=n.recodePath(t),this._parts.path=this._parts.path.replace(h,t)),this.build(!e),this},g.segment=function(t,e,r){var s=this._parts.urn?":":"/",n=this.path(),i="/"===n.substring(0,1),a=n.split(s);if(void 0!==t&&"number"!=typeof t&&(r=e,e=t,t=void 0),void 0!==t&&"number"!=typeof t)throw new Error('Bad segment "'+t+'", must be 0-based integer');if(i&&a.shift(),0>t&&(t=Math.max(a.length+t,0)),void 0===e)return void 0===t?a:a[t];if(null===t||void 0===a[t])if(o(e)){a=[];for(var h=0,u=e.length;u>h;h++)(e[h].length||a.length&&a[a.length-1].length)&&(a.length&&!a[a.length-1].length&&a.pop(),a.push(e[h]))}else(e||"string"==typeof e)&&(""===a[a.length-1]?a[a.length-1]=e:a.push(e));else e?a[t]=e:a.splice(t,1);return i&&a.unshift(""),this.path(a.join(s),r)},g.segmentCoded=function(t,e,r){var s,i,a;if("number"!=typeof t&&(r=e,e=t,t=void 0),void 0===e){if(s=this.segment(t,e,r),o(s))for(i=0,a=s.length;a>i;i++)s[i]=n.decode(s[i]);else s=void 0!==s?n.decode(s):void 0;return s}if(o(e))for(i=0,a=e.length;a>i;i++)e[i]=n.encode(e[i]);else e="string"==typeof e||e instanceof String?n.encode(e):e;return this.segment(t,e,r)};var x=g.query;return g.query=function(t,e){if(t===!0)return n.parseQuery(this._parts.query,this._parts.escapeQuerySpace);if("function"==typeof t){var r=n.parseQuery(this._parts.query,this._parts.escapeQuerySpace),s=t.call(this,r);return this._parts.query=n.buildQuery(s||r,this._parts.duplicateQueryParameters,this._parts.escapeQuerySpace),this.build(!e),this}return void 0!==t&&"string"!=typeof t?(this._parts.query=n.buildQuery(t,this._parts.duplicateQueryParameters,this._parts.escapeQuerySpace),this.build(!e),this):x.call(this,t,e)},g.setQuery=function(t,e,r){var s=n.parseQuery(this._parts.query,this._parts.escapeQuerySpace);if("string"==typeof t||t instanceof String)s[t]=void 0!==e?e:null;else{if("object"!=typeof t)throw new TypeError("URI.addQuery() accepts an object, string as the name parameter");for(var i in t)_.call(t,i)&&(s[i]=t[i])}return this._parts.query=n.buildQuery(s,this._parts.duplicateQueryParameters,this._parts.escapeQuerySpace),"string"!=typeof t&&(r=e),this.build(!r),this},g.addQuery=function(t,e,r){var s=n.parseQuery(this._parts.query,this._parts.escapeQuerySpace);return n.addQuery(s,t,void 0===e?null:e),this._parts.query=n.buildQuery(s,this._parts.duplicateQueryParameters,this._parts.escapeQuerySpace),"string"!=typeof t&&(r=e),this.build(!r),this},g.removeQuery=function(t,e,r){var s=n.parseQuery(this._parts.query,this._parts.escapeQuerySpace);return n.removeQuery(s,t,e),this._parts.query=n.buildQuery(s,this._parts.duplicateQueryParameters,this._parts.escapeQuerySpace),"string"!=typeof t&&(r=e),this.build(!r),this},g.hasQuery=function(t,e,r){var s=n.parseQuery(this._parts.query,this._parts.escapeQuerySpace);return n.hasQuery(s,t,e,r)},g.setSearch=g.setQuery,g.addSearch=g.addQuery,g.removeSearch=g.removeQuery,g.hasSearch=g.hasQuery,g.normalize=function(){return this._parts.urn?this.normalizeProtocol(!1).normalizePath(!1).normalizeQuery(!1).normalizeFragment(!1).build():this.normalizeProtocol(!1).normalizeHostname(!1).normalizePort(!1).normalizePath(!1).normalizeQuery(!1).normalizeFragment(!1).build()},g.normalizeProtocol=function(t){return"string"==typeof this._parts.protocol&&(this._parts.protocol=this._parts.protocol.toLowerCase(),this.build(!t)),this},g.normalizeHostname=function(r){return this._parts.hostname&&(this.is("IDN")&&t?this._parts.hostname=t.toASCII(this._parts.hostname):this.is("IPv6")&&e&&(this._parts.hostname=e.best(this._parts.hostname)),this._parts.hostname=this._parts.hostname.toLowerCase(),this.build(!r)),this},g.normalizePort=function(t){return"string"==typeof this._parts.protocol&&this._parts.port===n.defaultPorts[this._parts.protocol]&&(this._parts.port=null,this.build(!t)),this},g.normalizePath=function(t){var e=this._parts.path;if(!e)return this;if(this._parts.urn)return this._parts.path=n.recodeUrnPath(this._parts.path),this.build(!t),this;if("/"===this._parts.path)return this;var r,s,i,a="";for("/"!==e.charAt(0)&&(r=!0,e="/"+e),("/.."===e.slice(-3)||"/."===e.slice(-2))&&(e+="/"),e=e.replace(/(\/(\.\/)+)|(\/\.$)/g,"/").replace(/\/{2,}/g,"/"),r&&(a=e.substring(1).match(/^(\.\.\/)+/)||"",a&&(a=a[0]));;){if(s=e.indexOf("/.."),-1===s)break;0!==s?(i=e.substring(0,s).lastIndexOf("/"),-1===i&&(i=s),e=e.substring(0,i)+e.substring(s+3)):e=e.substring(3)}return r&&this.is("relative")&&(e=a+e.substring(1)),e=n.recodePath(e),this._parts.path=e,this.build(!t),this},g.normalizePathname=g.normalizePath,g.normalizeQuery=function(t){return"string"==typeof this._parts.query&&(this._parts.query.length?this.query(n.parseQuery(this._parts.query,this._parts.escapeQuerySpace)):this._parts.query=null,this.build(!t)),this},g.normalizeFragment=function(t){return this._parts.fragment||(this._parts.fragment=null,this.build(!t)),this},g.normalizeSearch=g.normalizeQuery,g.normalizeHash=g.normalizeFragment,g.iso8859=function(){var t=n.encode,e=n.decode;n.encode=escape,n.decode=decodeURIComponent;try{this.normalize()}finally{n.encode=t,n.decode=e}return this},g.unicode=function(){var t=n.encode,e=n.decode;n.encode=l,n.decode=unescape;try{this.normalize()}finally{n.encode=t,n.decode=e}return this},g.readable=function(){var e=this.clone();e.username("").password("").normalize();var r="";if(e._parts.protocol&&(r+=e._parts.protocol+"://"),e._parts.hostname&&(e.is("punycode")&&t?(r+=t.toUnicode(e._parts.hostname),e._parts.port&&(r+=":"+e._parts.port)):r+=e.host()),e._parts.hostname&&e._parts.path&&"/"!==e._parts.path.charAt(0)&&(r+="/"),r+=e.path(!0),e._parts.query){for(var s="",i=0,a=e._parts.query.split("&"),o=a.length;o>i;i++){var h=(a[i]||"").split("=");s+="&"+n.decodeQuery(h[0],this._parts.escapeQuerySpace).replace(/&/g,"%26"),void 0!==h[1]&&(s+="="+n.decodeQuery(h[1],this._parts.escapeQuerySpace).replace(/&/g,"%26"))}r+="?"+s.substring(1)}return r+=n.decodeQuery(e.hash(),!0)},g.absoluteTo=function(t){var e,r,s,i=this.clone(),a=["protocol","username","password","hostname","port"];if(this._parts.urn)throw new Error("URNs do not have any generally defined hierarchical components");if(t instanceof n||(t=new n(t)),i._parts.protocol||(i._parts.protocol=t._parts.protocol),this._parts.hostname)return i;for(r=0;s=a[r];r++)i._parts[s]=t._parts[s];return i._parts.path?".."===i._parts.path.substring(-2)&&(i._parts.path+="/"):(i._parts.path=t._parts.path,i._parts.query||(i._parts.query=t._parts.query)),"/"!==i.path().charAt(0)&&(e=t.directory(),e=e?e:0===t.path().indexOf("/")?"/":"",i._parts.path=(e?e+"/":"")+i._parts.path,i.normalizePath()),i.build(),i},g.relativeTo=function(t){var e,r,s,i,a,o=this.clone().normalize();if(o._parts.urn)throw new Error("URNs do not have any generally defined hierarchical components");if(t=new n(t).normalize(),e=o._parts,r=t._parts,i=o.path(),a=t.path(),"/"!==i.charAt(0))throw new Error("URI is already relative");if("/"!==a.charAt(0))throw new Error("Cannot calculate a URI relative to another relative URI");if(e.protocol===r.protocol&&(e.protocol=null),e.username!==r.username||e.password!==r.password)return o.build();if(null!==e.protocol||null!==e.username||null!==e.password)return o.build();if(e.hostname!==r.hostname||e.port!==r.port)return o.build();if(e.hostname=null,e.port=null,i===a)return e.path="",o.build();if(s=n.commonPath(i,a),!s)return o.build();var h=r.path.substring(s.length).replace(/[^\/]*$/,"").replace(/.*?\//g,"../");return e.path=h+e.path.substring(s.length)||"./",o.build()},g.equals=function(t){var e,r,s,i=this.clone(),a=new n(t),h={},u={},c={};if(i.normalize(),a.normalize(),i.toString()===a.toString())return!0;if(e=i.query(),r=a.query(),i.query(""),a.query(""),i.toString()!==a.toString())return!1;if(e.length!==r.length)return!1;h=n.parseQuery(e,this._parts.escapeQuerySpace),u=n.parseQuery(r,this._parts.escapeQuerySpace);for(s in h)if(_.call(h,s)){if(o(h[s])){if(!p(h[s],u[s]))return!1}else if(h[s]!==u[s])return!1;c[s]=!0}for(s in u)if(_.call(u,s)&&!c[s])return!1;return!0},g.duplicateQueryParameters=function(t){return this._parts.duplicateQueryParameters=!!t,this},g.escapeQuerySpace=function(t){return this._parts.escapeQuerySpace=!!t,this},n}); |