/**
*
* Dromeo
* Simple and Flexible Pattern Routing Framework for PHP, JavaScript, Python
* @version: 1.3.0
*
* https://github.com/foo123/Dromeo
*
**/
!function(e,n,t){"use strict";var r;"undefined"!=typeof Components&&"object"==typeof Components.classes&&"object"==typeof Components.classesByID&&Components.utils&&"function"==typeof Components.utils.import?(e.EXPORTED_SYMBOLS=[n],e[n]=t.call(e)):"object"==typeof module&&module.exports?module.exports=t.call(e):"function"==typeof define&&define.amd&&"function"==typeof require&&"function"==typeof require.specified&&require.specified(n)?define(n,["require","exports","module"],function(){return t.call(e)}):n in e||(e[n]=r=t.call(e))&&"function"==typeof define&&define.amd&&define(function(){return r})}("undefined"!=typeof self?self:this,"Dromeo",function(C){"use strict";var e="prototype",n=Object[e],i=(Array[e],Function[e],n.toString),k=n.hasOwnProperty,T=/^([^|]+(\|[^|]+)+)$/,j=/\((\d+)\)$/,t=/^\s+|\s+$/g,r=/([*+\[\]\(\)?^$\/\\:.])/g,A=String[e].trim?function(e){return String(e).trim()}:function(e){return String(e).replace(t,"")};function N(e){return 0<e.length}function x(e){return e.replace(r,"\\$1")}function w(e){return"[object Array]"===i.call(e)}function y(e){return"[object Object]"===i.call(e)&&"function"==typeof e.constructor&&"Object"===e.constructor.name}function m(e){return"string"==typeof e||"[object String]"===i.call(e)}function _(e){return"function"==typeof e}function S(e){if("function"==typeof Object.keys)return Object.keys(e);var n,t,r;if(w(e))for(n=new Array(r=e.length),t=0;t<r;++t)n[t]=String(t);else for(t in n=[],e)k.call(e,t)&&n.push(t);return n}function l(e,n,t){var r,l=n.length;if(!0===t)return 0<l&&-1!==n.indexOf(e);for(r=0;r<l;++r)if(e==n[r])return 1}function P(e,n,t){var r,l,u;if(t=!0===t,n)for(r in n)k.call(n,r)&&(l=n[r],"number"==typeof(u=l)||"[object Number]"===i.call(u)?e[r]=0+l:m(l)?e[r]=l.slice():w(l)?e[r]=t?P(new Array(l.length),l,t):l:y(l)?e[r]=t?P({},l,t):l:e[r]=l);return e}function b(e){return decodeURIComponent(String(e))}function a(e){return encodeURIComponent(String(e)).split("!").join("%21").split("'").join("%27").split("(").join("%28").split(")").join("%29").split("*").join("%2A")}function s(e){return a(e).split("%20").join("+")}var o={php:/^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/},f=["source","scheme","authority","userInfo","user","pass","host","port","relative","path","directory","file","query","fragment"];function u(e){for(var n,t,r,l,u,i,o,a,s,f,c,h,p=e.replace(/^&+|&+$/g,"").split("&"),d=p.length,g={},m=[],_=0;_<d;++_)if(i=p[_].split("="),u=b(A(i[0])),i=i.length<2?"":b(A(i[1])),(u=-1<(n=u.indexOf("\0"))?u.slice(0,n):u)&&"["!==u.charAt(0)){for(a=[],n=o=0;n<u.length;++n)if("["!==u.charAt(n)||o){if("]"===u.charAt(n)&&o&&(a.length||a.push(u.slice(0,o-1)),a.push(u.substr(o,n-o)),o=0,"["!==u.charAt(n+1)))break}else o=n+1;for(a.length||(a=[u]),n=0;n<a[0].length&&(" "!==(l=a[0].charAt(n))&&"."!==l&&"["!==l||(a[0]=a[0].substr(0,n)+"_"+a[0].substr(n+1)),"["!==l);++n);for(u=null,t=r=g,f=a.length?A(a[a.length-1].replace(/^['"]|['"]$/g,"")):null,n=0,s=a.length;n<s;++n)c=u,u=a[n].replace(/^['"]|['"]$/g,""),h=t,t=r,""!==A(u)||0===n?(k.call(r,u)||(r[u]=n+1===s-1&&""===f?[]:{}),r=r[u]):u=!0;!0===u?t.push(i):(u==+u&&m.push({key:c,obj:h}),t[u]=i)}for(_=m.length-1;0<=_;--_)!function(e){if(w(e))return 1;if(y(e)){for(var n=S(e),t=n.length,r=0;r<t;++r)if(r!==+n[r])return;return 1}}(r=m[_].key?m[_].obj[m[_].key]:m[_].obj)||(r=function(e){if(w(e))return e;if("function"==typeof Object.values)return Object.values(e);var n,t=[];for(n in e)k.call(e,n)&&t.push(e[n]);return t}(r),m[_].key?m[_].obj[m[_].key]=r:g=r);return g}function c(e,n,t){if("undefined"!=typeof URLSearchParams){var r;try{r=new URLSearchParams(function e(n,t,r){if(y(n)||w(n)){for(var l=S(n),u=0,i=l.length;u<i;++u){var o=l[u],a=n[o],o=String(null==r?o:r+"["+o+"]");y(a)||w(a)?t=e(a,t,o):t[o]=a}return t}return n}(e,{}))}catch(e){r=null}if(r)return r=r.toString(),r="&"!==n?r.split("&").join(n):r}var l,u,i=[];for(l in arguments.length<2&&(n="&"),arguments.length<3&&(t=!1),e)k.call(e,l)&&""!=(u=function e(n,t,r,l){var u,i,o=l?a:s;if(!0===t?t="1":!1===t&&(t="0"),null==t)return"";if("object"!=typeof t)return o(n)+"="+o(t);for(u in i=[],t)k.call(t,u)&&null!=t[u]&&i.push(e(n+"["+u+"]",t[u],r,l));return i.join(r)}(l,e[l],n,t))&&i.push(u);return i.join(n)}function O(e,n,t){if(n!==t&&t){var r,l,u=[];for(e=e.split(n),l=0;l<e.length;++l)r=(r=e[l]).split(t),u.push(r[0]),1<r.length&&u.push(r[1]);return u}return e.split(n)}function Y(n){return function(e){return n}}function L(n){return function(e){return e[n]?e[n].length:0}}function h(e,n,t,r,l){var u,i,o,a,s,f,c,h,p,d,g,m,_,y,S,P,b,R,v,E;if(t.indexOf(e[0])<0)return[t,l&&l.length?l+t:t,{},r,!0,[t]];for(a=(u=O(t,e[0],e[1])).length,c=!1,h="",R=[],d=b=0,m={},P=[],l&&l.length&&(h+=x(l),b=l.length),o=0;o<a;++o)i=u[o],c=c?(f=s=!1,g=null,v=[],p=i.split(e[4]),A(p[0]).length||(p[0]=e[2]+"PART"+e[3]),y=function(e,n,t){for(var r,l,u,i=0,o={},a=[],s=[],f=null,c=(t=O(t,e[2],e[3])).length,h=!(l=[]),p=0;p<c;++p)h=h?(t[p].length&&(k.call(n,t[p])?(a.push("("+n[t[p]][0]+")"),++i,n[t[p]][1]&&(o[i]=n[t[p]][1]),null==f&&(f=a[a.length-1]),l.push([i])):(r=t[p].match(T))?(a.push("("+r[1].split("|").filter(N).map(x).join("|")+")"),null==f&&(f=a[a.length-1]),l.push([++i])):t[p].length&&(a.push("("+x(t[p])+")"),null==f&&(f=a[a.length-1]),l.push([++i]))),s.push(!0),!1):(t[p].length&&(a.push(x(t[p])),s.push(t[p]),l.push(t[p].length)),!0);return 1===a.length&&1===i?(o[0]=o[1]||null,[u=a.join(""),i,o,s,f||u,l]):(o[0]=null,[u="("+a.join("")+")",i+1,o,s,f||u,l])}(e,n,p[0]),1<p.length&&((p=(_=(s=(_=A(p[1])).length&&"?"===_.charAt(0))?_.slice(1):_).match(j))?(_=_.slice(0,-p[0].length),S=parseInt(p[1],10),g=k.call(y[2],S)?y[2][S]:null,0<S&&S<y[1]?(E=!1,v=y[5].reduce(function(e,n){return(E=w(n)&&n[0]>=S?!0:E)||e.push(w(n)?L(n[0]+d+1):Y(n)),e},[]),S+=d+1):S=d+1):(g=y[2][0]||null,S=d+1),f=0<_.length),h+=y[0],s&&(h+="?"),f&&(m[_]=[S,g,function(e){return function(t){return e.reduce(function(e,n){return e+n(t)},0)}}(R.concat(v))]),f&&P.push({name:_,optional:s,re:new RegExp("^"+y[4]+"$"),tpl:y[3]}),b=0,R.push(L(d+1)),d+=y[1],!1):(h+=x(i),b+=i.length,P.push(i),R.push(Y(b)),!0);return[t,new RegExp("^"+h+"$"),m,r,!1,P]}function p(e,n){return n.join(",")+"->"+e}function d(e){return(e=l("*",e=e?e.map?e.map(function(e){return e.toLowerCase()}):[String(e).toLowerCase()]:["*"])?["*"]:e).sort(),e}function g(e,n,t){if(n&&m(n.route)&&n.handler&&_(n.handler)){t=!0===t;for(var r=n.handler,l=n.defaults||{},u=n.types||null,i=n.name||null,o=d(n.method),a=p(n=e.key+n.route,o),s=null,f=0,c=e._routes.length;f<c;++f)if(a===e._routes[f].key){s=e._routes[f];break}s||(s=new E(e._delims,e._patterns,n,o,i,e._prefix),e._routes.push(s),e._addNamedRoute(s)),s.handlers.push([r,l,u,t,0])}}function R(e,n){for(var t,r=e._routes.length-1;0<=r;--r)n===e._routes[r].key&&(t=e._routes[r],e._routes.splice(r,1),e._delNamedRoute(t),t.dispose())}function v(e){Error.call(this,e),this.message=e,this.name="DromeoException"}function E(e,n,t,r,l,u){var i=this;i.__args__=[e,n],i.isParsed=!1,i.handlers=[],i.route=null!=t?String(t):"",i.prefix=null!=u?String(u):"",i.method=r,i.pattern=null,i.captures=null,i.literal=!1,i.namespace=null,i.tpl=null,i.name=null!=l?String(l):null,i.key=p(i.route,i.method)}function D(e,n,t){var r=this;if(!(r instanceof D))return new D(e,n,t);r._delims=["{","}","%","%",":"],r._patterns={},r.definePattern("ALPHA","[a-zA-Z\\-_]+"),r.definePattern("ALNUM","[a-zA-Z0-9\\-_]+"),r.definePattern("ASCII","[ -~]+"),r.definePattern("NUMBR","[0-9]+"),r.definePattern("INT","[0-9]+","INT"),r.definePattern("PART","[^\\/?#]+"),r.definePattern("VAR","[^=?&#\\/]+","VAR"),r.definePattern("QUERY","\\?[^?#]+"),r.definePattern("FRAGMENT","#[^?#]+"),r.definePattern("URLENCODED","[^\\/?#]+","URLENCODED"),r.definePattern("ALL",".+"),r.definePattern("ANY","[\\s\\S]+"),r._routes=[],r._named_routes={},r._fallback=!1,r._top=t instanceof D?t:r,r.key=r===r._top?"":r._top.key+String(n),r._prefix=null==e?"":String(e)}return(v[e]=Object.create(Error[e])).constructor=v,E.to_key=p,E[e]={constructor:E,__args__:null,isParsed:!1,handlers:null,route:null,prefix:null,pattern:null,captures:null,tpl:null,method:null,literal:null,namespace:null,name:null,key:null,dispose:function(){var e=this;return e.__args__=null,e.isParsed=null,e.handlers=null,e.route=null,e.prefix=null,e.pattern=null,e.captures=null,e.tpl=null,e.method=null,e.literal=null,e.namespace=null,e.name=null,e.key=null,e},parse:function(){var e,n=this;return n.isParsed||(e=h(n.__args__[0],n.__args__[1],n.route,n.method,n.prefix),n.pattern=e[1],n.captures=e[2],n.tpl=e[5],n.literal=!0===e[4],n.__args__=null,n.isParsed=!0),n},match:function(e,n){var t=this;return l(n=n||"*",t.method)||"*"===t.method[0]?(t.isParsed||t.parse(),e=String(e),t.literal?e===t.pattern?[]:null:e.match(t.pattern)):null},make:function(e,n){var t,r,l,u,i,o,a,s,f=this,c="";for(e=e||{},n=!0===n,f.isParsed||f.parse(),t=0,r=(s=f.tpl).length;t<r;++t)if(m(s[t]))c+=s[t];else if(k.call(e,s[t].name)&&null!=e[s[t].name]){if(o=(o=w(o=e[s[t].name])?o:[o]).map(String),n&&!s[t].re.test(o[0]))throw new v('Dromeo: Route "'+f.name+'" (Pattern: "'+f.route+'") parameter "'+s[t].name+'" value "'+o[0]+'" does not match pattern!');for(i=l=0,u=(a=s[t].tpl).length;l<u;++l)!0===a[l]?(c+=i<o.length?o[i]:o[0],++i):c+=a[l]}else if(!s[t].optional)throw new v('Dromeo: Route "'+f.name+'" (Pattern: "'+f.route+'") missing parameter "'+s[t].name+'"!');return c},sub:function(t,e,n,r){var l,u,i,o,a,s,f,c,h,p,d,g=this;if(g.isParsed&&!g.literal){for(l in s=t[0],f=_(r),c=[],g.captures)k.call(g.captures,l)&&(u=g.captures[l],c.push([l,u]));for(c.sort(function(e,n){return e[1][2](t)-n[1][2](t)}),i=0,o=c.length;i<o;++i)l=c[i][0],h=(u=c[i][1])[0],a=u[1],p=u[2],t[h]?(h=t[h],p=f?(p=p(t),String(r(l,h,p,p+h.length,s))):h,n&&k.call(n,l)&&n[l]?(m(d=n[l])&&k.call(D.TYPES,d)&&(d=D.TYPES[d]),e[l]=_(d)?d(p):p):e[l]=a&&_(d=a)?d(p):p):k.call(e,l)||(e[l]=null)}return g}},D.VERSION="1.3.0",D.Exception=v,D.Route=E,D.to_method=d,(D.TYPES={INTEGER:function(e){return parseInt(e,10)||0},STRING:function(e){return m(e)?e:""+String(e)},URLDECODE:function(e){return b(String(e).split("+").join("%20"))},ARRAY:function(e){return w(e)?e:[e]},PARAMS:function(e){return m(e)?D.unglue_params(e):e}}).INT=D.TYPES.INTEGER,D.TYPES.STR=D.TYPES.STRING,D.TYPES.VAR=D.TYPES.URLDECODE,D.TYPES.URLENCODED=D.TYPES.PARAMS,D.glue_params=function(e){var n="";return e&&(n+=c(e,"&",!0)),n},D.unglue_params=function(e){return e?u(e):{}},D.parse_components=function(e,n,t){var r={};return e&&((arguments.length<3||null==t)&&(t="fragment_params"),(arguments.length<2||null==n)&&(n="query_params"),r=function(e,n,t){var r,l,u=null;if("undefined"!=typeof URL){try{r=new URL(e)}catch(e){r=null}r&&(u={},r.protocol&&(u.scheme=r.protocol.slice(0,-1)),r.username&&(u.user=r.username),r.password&&(u.pass=r.password),r.hostname&&(u.host=r.hostname),r.port&&r.port.length&&(u.port=r.port),r.pathname&&r.pathname.length?u.path=r.pathname:u.path="",r.search&&r.search.length?u.query=r.search.slice(1):u.query="",r.hash&&r.hash.length?u.fragment=r.hash.slice(1):u.fragment="")}if(!u)for(r=o[t||"php"].exec(e),l=f.length,u={};l--;)l&&r[l]&&(u[f[l]]=r[l]);return k.call(u,"port")&&(u.port=parseInt(u.port,10)),n?u[n.replace("PHP_URL_","").toLowerCase()]||null:u}(e),n&&(r.query?r[n]=this.unglue_params(r.query):r[n]={}),t)&&(r.fragment?r[t]=this.unglue_params(r.fragment):r[t]={}),r},D.build_components=function(e,n,t,r,l){e=""+e;return(arguments.length<5||null==l)&&(l="#"),(arguments.length<4||null==r)&&(r="?"),n&&(e+=r+this.glue_params(n)),t&&(e+=l+this.glue_params(t)),e},D.defType=function(e,n){e&&_(n)&&(D.TYPES[e]=n)},D.TYPE=function(e){return e&&k.call(D.TYPES,e)?D.TYPES[e]:null},D[e]={constructor:D,_delims:null,_patterns:null,_routes:null,_named_routes:null,_fallback:!1,_prefix:"",_top:null,key:"",dispose:function(){var e,n,t=this;if(t._top=null,t._delims=null,t._patterns=null,t._fallback=null,t._prefix=null,t._routes)for(e=0,n=t._routes.length;e<n;++e)t._routes[e].dispose();return t._routes=null,t._named_routes=null,t},top:function(){return this._top},isTop:function(){return null==this._top||this===this._top},clone:function(e){var n,t,r=this,l=new D(r._prefix,e,r);for(n in l.defineDelimiters(r._delims),r._patterns)k.call(r._patterns,n)&&(t=r._patterns[n],l.definePattern(n,t[0],1<t.length?t[1]:null));return l},reset:function(){return this._routes=[],this._named_routes={},this._fallback=!1,this},defineDelimiters:function(e){var n,t=this._delims;return e&&(0<(n=e.length)&&e[0]&&(t[0]=e[0]),1<n&&e[1]&&(t[1]=e[1]),2<n&&e[2]&&(t[2]=e[2]),3<n&&e[3]&&(t[3]=e[3]),4<n)&&e[4]&&(t[4]=e[4]),this},definePattern:function(e,n,t){return(t=t&&m(t)&&t.length&&k.call(D.TYPES,t)?D.TYPES[t]:t)&&_(t)||(t=null),this._patterns[e]=[n,t],this},dropPattern:function(e){var n=this._patterns;return k.call(n,e)&&delete n[e],this},defineType:function(e,n){return D.defType(e,n),this},glue:function(e){return D.glue_params(e)},unglue:function(e){return D.unglue_params(e)},parse:function(e,n,t){return D.parse_components(e,n,t)},build:function(e,n,t,r,l){return D.build_components(e,n,t,r,l)},onGroup:function(e,n){return(e=String(e)).length&&_(n)&&(e=this.clone(e),this._routes.push(e),n(e)),this},on:function(){for(var e=arguments,n=e.length,t=1===n?w(e[0])?e[0]:[e[0]]:2===n&&m(e[0])&&_(e[1])?[{route:e[0],handler:e[1],method:"*",defaults:{},types:null}]:e,r=0,l=t.length;r<l;++r)g(this,t[r],!1);return this},one:function(){for(var e=arguments,n=e.length,t=1===n?w(e[0])?e[0]:[e[0]]:2===n&&m(e[0])&&_(e[1])?[{route:e[0],handler:e[1],method:"*",defaults:{},types:null}]:e,r=0,l=t.length;r<l;++r)g(this,t[r],!0);return this},off:function(e,n,t){var r,l,u,i,o=this,a=o._routes;if(e)if(null==t&&(t="*"),y(e)){if(n=e.handler||n,t=e.method||t,!(e=e.route))return o;for(i=p(e=String(e),d(t)),l=null,r=0,u=a.length;r<u;++r)if(a[r]instanceof D)a[r].off(e,n,t);else if(i===a[r].key){l=a[r];break}if(!l)return o;if(n&&_(n)){for(r=(u=l.handlers.length)-1;0<=r;--r)n===l.handlers[r][0]&&l.handlers.splice(r,1);l.handlers.length||R(o,i)}else R(o,i)}else if(m(e)&&e.length){for(i=p(e=String(e),d(t)),l=null,r=0,u=a.length;r<u;++r)if(a[r]instanceof D){if(e===a[r].key){l=a[r];break}a[r].off(e,n,t)}else if(i===a[r].key){l=a[r];break}if(!l)return o;if(l instanceof D)a.splice(r,1),l.dispose();else if(n&&_(n)){for(r=(u=l.handlers.length)-1;0<=r;--r)n===l.handlers[r][0]&&l.handlers.splice(r,1);l.handlers.length||R(o,i)}else R(o,i)}return o},fallback:function(e){return!1!==(e=arguments.length<1?!1:e)&&null!==e&&!_(e)||(this._fallback=e),this},make:function(e,n,t){var r=this._named_routes;return k.call(r,e)?r[e].make(n,t):null},route:function(e,n,t,r){var l,u,i,o,a,s,f,c,h,p,d,g,m,_,y,S=this;if(S.isTop()||S._routes.length){if(y=!(l=!0),e=null!=e?String(e):"",l=(u=S._prefix+S.key).length?u===e.slice(0,u.length):l){for(t=!1!==t,n=null!=n?String(n).toLowerCase():"*",h=(i=S._routes.slice()).length,c=0;c<h;++c){if((o=i[c])instanceof D){if(!(g=o.route(e,n,t,r)))continue;y=!0}else{if(null==(g=o.match(e,n)))continue;for(y=!0,f=[],p=(m=o.handlers.slice()).length,d=0;d<p;++d)(_=m[d])[3]&&_[4]?f.unshift(d):(a=_[1],s=_[2],a={route:e,method:n,pattern:o.route,fallback:!1,data:P({},a,!0)},o.sub(g,a.data,s,r),_[4]=1,_[3]&&f.unshift(d),_[0](a));for(d=0,p=f.length;d<p;++d)o.handlers.splice(f[d],1);o.handlers.length||R(S,o.key)}if(t)return!0}if(y)return!0}S._fallback&&S.isTop()&&S._fallback({route:e,method:n,pattern:null,fallback:!0,data:null})}return!1},_addNamedRoute:function(e){return this.isTop()?e instanceof D.Route&&e.name&&e.name.length&&(this._named_routes[e.name]=e):this.top()._addNamedRoute(e),this},_delNamedRoute:function(e){var n=this;return n.isTop()?e instanceof D.Route&&e.name&&k.call(n._named_routes,e.name)&&delete n._named_routes[e.name]:n.top()._delNamedRoute(e),n}},D});
|