From 5c312064b2939e5632e1b467eff3ea51c778149c Mon Sep 17 00:00:00 2001 From: webby1111 Date: Wed, 1 Dec 2021 02:45:08 +0800 Subject: [PATCH] Fixed hash routing hanging for malformed routes. --- aris.js | 58 +++++++++++++++++++++++++++++++++++++++++++---------- aris.min.js | 2 +- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/aris.js b/aris.js index d8955ab..e4d2fcb 100644 --- a/aris.js +++ b/aris.js @@ -214,10 +214,10 @@ "use strict"; var document = window.document; - var gcs = window.getComputedStyle; + var gcs = function (x) { return window.getComputedStyle(x) }; var isUndefined = function (x) { return typeof(x) == 'undefined'; }; - var noBrowser = isUndefined(document) || isUndefined(gcs); + var noBrowser = isUndefined(document); var toSet = function (s) { for (var d = {}, i = (s = s.split(',')).length; i--;) d[s[i]] = 1; @@ -236,9 +236,6 @@ var stringTrimRe = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g; var splitWordsRe = /(?:(?:^|[A-Z])[a-z]+|[0-9]+|[A-Za-z]+)/g; - var hashPopRe = /(?:(^\/)|\/)[^\/]+[\/]*$/; // $1 - var hashResolveRe = /^#\/?|(\/)(?:\.?\/)+|(?:(?!\.\.?)[^\/])+\/\.\.(?:\/|$)|([^\^])\/+$/; - var hashCompsRe = /(?:(^|\/)(:?)([^\/]+))/g; var locationPathPopRe = /((^|\/)[^\/]*)$/; var htmlTagRe = /^[\w]+$/; @@ -854,8 +851,29 @@ svgPath[c] = svgPathAppender(c); HTML.SVG.Path = svgPath; + + var onceDones = {}; + var once = function (k, t, f) { + if (onceDones[k]) { + if (isFunction(f)) f(); + return false; + } else { + onceDones[k] = 1; + if (isFunction(t)) t(); + } + return true; + }; + once.clear = function (k) { + delete onceDones[k]; + return once; + }; + HTML.once = once; + + var hashPopRe = /(?:(^\/)|\/)[^\/]+[\/]*$/; // $1 + var hashResolveRe = /^#?\/?|(\/)(?:\.?\/)+|(?:(?!\.\.?)[^\/])+\/\.\.(?:\/|$)|([^\^])\/+$/; + var hashCompsRe = /(?:(^|\/)(:?)([^\/]+))/g; - var routes = {}, savedRoutes = {}, routesInited = 0, refreshable = 0; + var routes = {}, savedRoutes = {}, routesInited = 0, refreshable = 0, visited = []; var hashResolve = function (h) { for (var p, t = 1; t; ) { @@ -865,9 +883,18 @@ } return h; }; + var setVisited = function (p) { + if (visited.length < 1) { + setTimeout(function () { + visited = []; + }, 60); + } + visited.push(p); + } + //HTML.hashResolve = hashResolve; var execRoute = function (h, s) { - var p = '', j, c, m, a = hashResolve(h); + var p = '', j, c, m, a = hashResolve(h), t = h.length; while (m = hashCompsRe.exec(a)) { p += m[1]; c = m[3]; @@ -876,13 +903,24 @@ if (!s) savedRoutes[p] = c; p += c; } - for (j = p; j; j = j.replace(hashPopRe, '$1')) { + for (j = p; j && t > 0; j = j.replace(hashPopRe, '$1')) { if (routes[j]) { p = j; j = ''; } + --t; + } + if (!s && isFunction(c = routes[p])) { + if (visited.indexOf(p) < 0) { + setVisited(p); + c(); + } else { + var v = h.replace('/:', '/'); + if (v != h && visited.indexOf(v) < 0) { + execRoute(v); + } + } } - if (!s && isFunction(c = routes[p])) c(); return p; }; @@ -938,8 +976,6 @@ // HTML.route('a', fn).route('b', fn) return HTML; } - // If no args are provided, - // return the window.location.hash splitted. return routes[execRoute(hashResolve(r), 1)]; }; diff --git a/aris.min.js b/aris.min.js index b93aff8..f129d27 100644 --- a/aris.min.js +++ b/aris.min.js @@ -1 +1 @@ -!function(n,r){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=r(n,1):r(n)}("undefined"!=typeof window?window:this,function(c,n){"use strict";function l(n){return"undefined"==typeof n}function p(n){for(var r={},e=(n=n[u](","))[j];e--;)r[n[e]]=1;return r}function d(n){return n}function y(n){for(var r,e=[];r=a.exec(n);)e.push(r[0]);return e}function r(r){return function(n){return n&&Object.prototype.toString.call(n)=="[object "+r+"]"}}function w(n){return String.prototype.trim?n.trim():n[v](i,"")}var g=c.document,u="split",j="length",$="toLowerCase",v="replace",h="createElement",s="toUpperCase",m="appendChild",S="hasOwnProperty",k="match",e=c.getComputedStyle,t=l(g)||l(e),x=p("column-count,fill-opacity,font-weight,line-height,opacity,orphans,widows,z-index,zoom"),A=p("area,base,br,col,embed,hr,img,input,keygen,link,meta,param,source,track,wbr"),b=/((?:\\.|("|')(?:\\.|.)*?\2|[^{}])*)([{}])/g,z=/(?:^|\{|\s|;)([A-Za-z0-9\-]+)\s*\:\s*?((?:\\.|("|')(?:\\.|.)*?\3|[^;}])*)/g,o=/url\(\s*?["']?(.*?)["']?\s*?\)/g,f=/^\s*?(data\:|.{1,6}\:\/\/)/,C=/\/\*[\s\S]*?\*\//g,E=/\.([A-Za-z0-9]+)(?:[\?#]|$)/,F=/^(?:\s*?(js|css)\s*?\:)?\s*?(\S[\S\s]*)/i,i=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,a=/(?:(?:^|[A-Z])[a-z]+|[0-9]+|[A-Za-z]+)/g,T=/(?:(^\/)|\/)[^\/]+[\/]*$/,L=/^#\/?|(\/)(?:\.?\/)+|(?:(?!\.\.?)[^\/])+\/\.\.(?:\/|$)|([^\^])\/+$/,M=/(?:(^|\/)(:?)([^\/]+))/g,R=/((^|\/)[^\/]*)$/,H=/^[\w]+$/,O=t?d:g.head,Z=r("Array"),q=r("Object"),G=r("Function"),I=t?[]:e(g.documentElement,null),P=t?[]:g[h]("div").style,V=[],N={},U="",B=0,W=p("-webkit-,-moz-,-ms-,-o-");if(t&&(W={}),I&&0B&&(B=W[nn],U=nn);function rn(r,n,e,t){var o=RegExp(n+"("+r.join("|")+")"+e,"g");return function(n){return r[j]?n[v](o,t):n}}function en(n){var r,e,t,o,i,u,f="",a=0;if(q(n)){for(i in u={},n)if(n[S](i)){if(r=n[i],i=w(i),"number"!=typeof r||x[i]||(r+="px"),r=cn(r),!N[i])for(o in W)u[o+i]=r;u[i]=r}return u}for(n=n[v](C,"");t=b.exec(n);){var c=t[3];if("{"==c)f+=""+t[1]+c,++a;else if("}"==c){for(u={};e=z.exec(t[1]);){if(!N[e[1]])for(o in W)u[o+e[1]]=e[2];u[e[1]]=e[2]}for(i in u)f+=i+":"+u[i]+";";f+=""+c,a=0",r};function gn(n){return hn[n]}dn.bool=function(n,r){var e={};return r&&(e[n]=n),e},dn.hash=function(){var n,r,e,t=arguments,o=0;for(e=0;e":">",'"':""","'":"'"},vn=/[&<>"']/g;dn.escape=function(n){return n[v](vn,gn)},dn.SVG=function(n,r){var e,t=arguments,o=["svg",{width:n+"px",height:r+"px",viewBox:"0 0 "+n+" "+r,xmlns:"http://www.w3.org/2000/svg"}];for(e=2;eV&&(V=N[K],P=K);function Q(r,n,e,t){var o=RegExp(n+"("+r.join("|")+")"+e,"g");return function(n){return r[j]?n[v](o,t):n}}function Y(n){var r,e,t,o,i,u,f="",a=0;if(R(n)){for(i in u={},n)if(n[S](i)){if(r=n[i],i=w(i),"number"!=typeof r||x[i]||(r+="px"),r=un(r),!I[i])for(o in N)u[o+i]=r;u[i]=r}return u}for(n=n[v](C,"");t=b.exec(n);){var c=t[3];if("{"==c)f+=""+t[1]+c,++a;else if("}"==c){for(u={};e=z.exec(t[1]);){if(!I[e[1]])for(o in N)u[o+e[1]]=e[2];u[e[1]]=e[2]}for(i in u)f+=i+":"+u[i]+";";f+=""+c,a=0",r};function ln(n){return pn[n]}sn.bool=function(n,r){var e={};return r&&(e[n]=n),e},sn.hash=function(){var n,r,e,t=arguments,o=0;for(e=0;e":">",'"':""","'":"'"},dn=/[&<>"']/g;sn.escape=function(n){return n[v](dn,ln)},sn.SVG=function(n,r){var e,t=arguments,o=["svg",{width:n+"px",height:r+"px",viewBox:"0 0 "+n+" "+r,xmlns:"http://www.w3.org/2000/svg"}];for(e=2;e