From 109f6947a26a02bebfd7f3885c85bb90a28ccaee Mon Sep 17 00:00:00 2001 From: Gabriel Reitz Giannattasio Date: Sun, 19 Jul 2015 23:45:14 -0700 Subject: [PATCH] Only apply builtify if the option is enabled and the basePath is fulfilled --- bower.json | 2 +- component.json | 2 +- package.json | 2 +- push-state-tree.js | 7 ++++--- push-state-tree.min.js | 6 +++--- src/pushStateTree.js | 3 ++- test/specs/route-beutifyLocation.js | 22 ++++++++++++++++++++++ 7 files changed, 34 insertions(+), 10 deletions(-) diff --git a/bower.json b/bower.json index 1005b13..1bd742c 100644 --- a/bower.json +++ b/bower.json @@ -1 +1 @@ -{"name":"push-state-tree","main":"pushStateTree.js","dependencies":{},"version":"0.13.0","description":"A standalone powerful library to manage browser routing with nested level support, complex match expressions and on-fly rules change (convenient to lazy module loading).","repository":{"type":"git","url":"https://github.com/gartz/pushStateTree.git"}} +{"name":"push-state-tree","main":"pushStateTree.js","dependencies":{},"version":"0.13.1","description":"A standalone powerful library to manage browser routing with nested level support, complex match expressions and on-fly rules change (convenient to lazy module loading).","repository":{"type":"git","url":"https://github.com/gartz/pushStateTree.git"}} diff --git a/component.json b/component.json index 4fafca4..5b3b252 100644 --- a/component.json +++ b/component.json @@ -1 +1 @@ -{"name":"push-state-tree","description":"A standalone powerful library to manage browser routing with nested level support, complex match expressions and on-fly rules change (convenient to lazy module loading).","version":"0.13.0","keywords":["route","routes","router","pushstate","hashchange","history","location","navigation","controller","MVC","event","events","dispatch","url","SPA","Single Page Application","Lazy Load","apps","app","routing","URI","shim","navigator","navigation"],"main":"push-state-tree.js","development":{"assemble":"~0.4.37","grunt":"~0.4.5","grunt-contrib-clean":"~0.6.0","grunt-contrib-concat":"~0.5.1","grunt-contrib-connect":"~0.10.1","grunt-contrib-jasmine":"~0.8.2","grunt-contrib-jshint":"~0.11.2","grunt-contrib-nodeunit":"~0.4.1","grunt-contrib-qunit":"~0.7.0","grunt-contrib-rename":"0.0.3","grunt-contrib-uglify":"~0.9.1","grunt-contrib-watch":"~0.6.1","grunt-coveralls":"^1.0.0","grunt-json-generator":"~0.1.0","grunt-merge-json":"~0.9.4","grunt-template-jasmine-istanbul":"~0.3.1","grunt-update-json":"~0.2.1","jshint-junit-reporter":"0.3.0","jshint-stylish":"~2.0.0","load-grunt-tasks":"~3.2.0","node-static":"^0.7.6","codeclimate-test-reporter":"0.0.4"}} +{"name":"push-state-tree","description":"A standalone powerful library to manage browser routing with nested level support, complex match expressions and on-fly rules change (convenient to lazy module loading).","version":"0.13.1","keywords":["route","routes","router","pushstate","hashchange","history","location","navigation","controller","MVC","event","events","dispatch","url","SPA","Single Page Application","Lazy Load","apps","app","routing","URI","shim","navigator","navigation"],"main":"push-state-tree.js","development":{"assemble":"~0.4.37","grunt":"~0.4.5","grunt-contrib-clean":"~0.6.0","grunt-contrib-concat":"~0.5.1","grunt-contrib-connect":"~0.10.1","grunt-contrib-jasmine":"~0.8.2","grunt-contrib-jshint":"~0.11.2","grunt-contrib-nodeunit":"~0.4.1","grunt-contrib-qunit":"~0.7.0","grunt-contrib-rename":"0.0.3","grunt-contrib-uglify":"~0.9.1","grunt-contrib-watch":"~0.6.1","grunt-coveralls":"^1.0.0","grunt-json-generator":"~0.1.0","grunt-merge-json":"~0.9.4","grunt-template-jasmine-istanbul":"~0.3.1","grunt-update-json":"~0.2.1","jshint-junit-reporter":"0.3.0","jshint-stylish":"~2.0.0","load-grunt-tasks":"~3.2.0","node-static":"^0.7.6","codeclimate-test-reporter":"0.0.4"}} diff --git a/package.json b/package.json index dadd06b..9731d6b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "push-state-tree", "description": "A standalone powerful library to manage browser routing with nested level support, complex match expressions and on-fly rules change (convenient to lazy module loading).", - "version": "0.13.0", + "version": "0.13.1", "homepage": "https://github.com/gartz/pushStateTree/", "author": { "name": "Gabriel Reitz Giannattasio ", diff --git a/push-state-tree.js b/push-state-tree.js index 514dfaa..7693066 100644 --- a/push-state-tree.js +++ b/push-state-tree.js @@ -1,8 +1,8 @@ -//! push-state-tree - v0.13.0 - 2015-07-19 +//! push-state-tree - v0.13.1 - 2015-07-19 //* https://github.com/gartz/pushStateTree/ //* Copyright (c) 2015 Gabriel Reitz Giannattasio ; Licensed -var PushStateTree = {options: {VERSION: '0.13.0'}}; +var PushStateTree = {options: {VERSION: '0.13.1'}}; (function (root) { 'use strict'; @@ -450,7 +450,8 @@ var PushStateTree = {options: {VERSION: '0.13.0'}}; // Remove all begin # chars from the location when using hash uri = root.location.hash.match(/^(#*)?(.*\/?)/)[2]; - if (rootElement.beautifyLocation && rootElement[USE_PUSH_STATE]) { + var usePushState = rootElement[USE_PUSH_STATE]; + if (rootElement.beautifyLocation && rootElement.isPathValid && usePushState) { // when using pushState, replace the browser location to avoid ugly URLs rootElement.replaceState( diff --git a/push-state-tree.min.js b/push-state-tree.min.js index e1c5422..718c3d7 100644 --- a/push-state-tree.min.js +++ b/push-state-tree.min.js @@ -1,6 +1,6 @@ -//! push-state-tree - v0.13.0 - 2015-07-19 +//! push-state-tree - v0.13.1 - 2015-07-19 //* https://github.com/gartz/pushStateTree/ //* Copyright (c) 2015 Gabriel Reitz Giannattasio ; Licensed -var PushStateTree = {options: {VERSION: '0.13.0'}}; -var PushStateTree={options:{VERSION:"0.13.0"}};!function(a){"use strict";function b(a){return!isNaN(parseFloat(a))&&a%1===0&&isFinite(a)}function c(a,b,c){Object.defineProperty(a,b,{get:function(){return c},set:function(){}})}function d(a){return/^[a-z0-9]+:\/\//i.test(a)}function e(a){return/^[^#/]/.test(a)}function f(b){function d(a){if("function"==typeof f.prototype[a])e[a]=function(){return f.prototype[a].apply(this,arguments)};else{if("undefined"!=typeof e[a])return;Object.defineProperty(e,a,{get:function(){return f.prototype[a]},set:function(b){f.prototype[a]=b}})}}if(b=b||{},b[n]=b[n]!==!1,!(this instanceof A))return f.apply(h.createElement("pushstatetree-route"),arguments);var e=this;this.VERSION=z;for(var g in b)b.hasOwnProperty(g)&&(e[g]=b[g]);if(f.prototype[o]){var i=b[n];Object.defineProperty(e,n,{get:function(){return i},set:function(a){i=a!==!1}})}else c(e,n,!1);Object.defineProperty(e,"beautifyLocation",{get:function(){return!!f.prototype.beautifyLocation},set:function(a){f.prototype.beautifyLocation=a===!0}}),e.beautifyLocation=b.beautifyLocation;var m;Object.defineProperty(e,"basePath",{get:function(){return m},set:function(a){a=a||"",m=a.match(/^(\/)?((.*?)\/?)(\/*)$/)[3]+"/",m.length>1&&(m="/"+m)}}),e.basePath=b.basePath;for(var r in f.prototype)f.prototype.hasOwnProperty(r)&&d(r);c(e,"length",a.history.length),c(e,"state",a.history.state);var s={url:"",uri:""};Object.defineProperty(e,"uri",{get:function(){if(s.url===a.location.href)return s.uri;var b;return a.location.hash.length||"#"===a.location.href[j.href.length-1]?(b=a.location.hash.match(/^(#*)?(.*\/?)/)[2],e.beautifyLocation&&e[n]&&e.replaceState(e.state,e.title,"/"===b[0]?b:"/"+b)):(b=a.location.pathname+a.location.search,this.isPathValid&&(b=b.slice(this.basePath.length))),b=b.replace(/^[\/]+/,""),e.getAttribute("uri")!==b&&e.setAttribute("uri",b),s.url=a.location.href,s.uri=b,b},configurable:!0}),Object.defineProperty(e,"isPathValid",{get:function(){var b=a.location.pathname+a.location.search;return!this.basePath||0===b.indexOf(this.basePath)}}),e.eventStack={leave:[],change:[],enter:[],match:[]},a.addEventListener(q,function(){var a=e.uri,b=e.state;e.rulesDispatcher(),C=a,B=b,E&&this.dispatch()}.bind(e));var t=!1,u=function(){if(!t&&C!==e.uri){var a=e.uri,b=e.state;e.rulesDispatcher(),C=a,B=b,E&&this.dispatch()}}.bind(e);e.avoidHashchangeHandler=function(){a.removeEventListener(p,u),t=!0},a.addEventListener(p,u);var v=function(){a.dispatchEvent(new l(p))};return h.addEventListener("DOMContentLoaded",v),a.addEventListener("readystatechange",v),a.addEventListener("load",function(){v(),k&&a.setInterval(function(){return e.uri!==C?void v():void(t&&(t=!1,C=e.uri,a.addEventListener(p,u)))}.bind(e),50)}.bind(e)),this}function g(b){var c=b;this[b]=function(){var b=Array.prototype.slice.call(arguments);if(("pushState"===c||"replaceState"===c)&&!d(b[2])){if(e(b[2])){var f=this.uri.match(/^([^?#]]*)\//);f=f?f[1]+"/":"",b[2]=f+b[2]}else b[2]=b[2].match(/^([#/]*)?(.*)/)[2];this[n]?b[2]=this.basePath+b[2]:b[2]="#"+b[2]}return a.history[c].apply(a.history,b),this}}var h=a.document,i=a.window,j=a.location,k=function(){var a=i.navigator.userAgent.indexOf("Trident");return a>=0}();!function(){a.location.origin||(a.location.origin=a.location.protocol+"//"+a.location.host)}(),function(){Function.prototype.bind||(Function.prototype.bind=function(a){if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");var b=Array.prototype.slice.call(arguments,1),c=this,d=function(){},e=function(){var e=a;return this instanceof d&&a&&(e=this),c.apply(e,b.concat(Array.prototype.slice.call(arguments)))};return d.prototype=this.prototype,e.prototype=new d,e})}();var l=a.HashChangeEvent,m=a.Event;!function(){function b(a,b){b=b||{bubbles:!1,cancelable:!1,detail:void 0};var c=h.createEvent("CustomEvent");return c.initCustomEvent(a,b.bubbles,b.cancelable,b.detail),c}if(Element.prototype.addEventListener){b.prototype=m.prototype,(!a.CustomEvent||k)&&(a.CustomEvent=b),(!l||a.opera&&a.opera.version()<15||k)&&(l=a.CustomEvent),k&&(m=b);try{new l("hashchange")}catch(c){l=b}try{new m("popstate")}catch(c){m=b}}}(),function(){if(!Element.prototype.addEventListener&&Object.defineProperty){var b=h.createEventObject().constructor.prototype;Object.defineProperty(b,"target",{get:function(){return this.srcElement}});var c=function(a,b){this.__bindedFunctions||(this.__bindedFunctions=[]);var c=b;if(!("on"+a in this)||"hashchange"===a){this.__elemetIEid=this.__elemetIEid||"__ie__"+Math.random();var d=a+this.__elemetIEid,e=this,f=function(a){a.propertyName===d&&c.call(e,h.documentElement[d])};if(this.__bindedFunctions.push({original:c,binded:f}),h.documentElement.attachEvent("onpropertychange",f),"hashchange"!==a)return}var g=c.bind(this);this.__bindedFunctions.push({original:c,binded:g}),this.attachEvent("on"+a,g)};HTMLDocument.prototype.addEventListener=c,Element.prototype.addEventListener=c,i.addEventListener=c;var d=function(a,b){this.__bindedFunctions||(this.__bindedFunctions=[]);var c,d=b;if(!("on"+a in this)||"hashchange"===a){for(var e=0;ec;c++)c in this&&a.push(this[c]);return b.apply(a,arguments)}}"forEach"in Array.prototype||(Array.prototype.forEach=function(a,b){for(var c=0;c>>0;if("function"!=typeof a)throw new TypeError;for(var d=[],e=arguments.length>=2?arguments[1]:void 0,f=0;c>f;f++)if(f in b){var g=b[f];a.call(e,g,f,b)&&d.push(g)}return d})}();var n="usePushState",o="hasPushState",p="hashchange",q="popstate",r="leave",s="update",t="enter",u="change",v="match",w="oldMatch",x=a.PushStateTree&&a.PushStateTree.options||{},y=a.DEBUG||x.DEBUG,z=x.VERSION||"development",A="undefined"!=typeof HTMLElement?HTMLElement:Element,B=null,C=null,D=[],E=!1,F=!1;f.prototype={beautifyLocation:!0,createRule:function(a){var c=h.createElement("pushstatetree-rule"),d=new RegExp("");Object.defineProperty(c,"rule",{get:function(){return d},set:function(a){if(a instanceof RegExp)d=a;else{if(a===d.toString())return;var b=(a+"").match(/^\/(.+)\/([gmi]*)|(.*)/);d=new RegExp(b[1]||b[3],b[2])}c.setAttribute("rule",d.toString())}}),Object.defineProperty(c,"parentGroup",{get:function(){var a=c.getAttribute("parent-group");return a&&b(a)?+a:null},set:function(a){b(a)?c.setAttribute("parent-group",a):c.removeAttribute("parent-group")}});for(var e in a)a.hasOwnProperty(e)&&(c[e]=a[e]);var f=[];Object.defineProperty(c,v,{get:function(){return f},set:function(a){f=a instanceof Array?a:[]}});var g=[];return Object.defineProperty(c,w,{get:function(){return g},set:function(a){g=a instanceof Array?a:[]}}),c[v]=[],c[w]=[],["assign","navigate","replace","dispatch","pushState","replaceState"].forEach(function(a){c[a]=function(){this.parentElement[a].apply(this.parentElement,arguments)}}),c},add:function(a){return this.appendChild(this.createRule(a)),this},remove:function(a){var b=a;return"string"==typeof a&&(b=this.querySelector(a)),b&&b.parentElement?(b.parentElement.removeChild(b),b):void 0},dispatch:function(){return F?(E=!0,this):(E=!1,a.dispatchEvent(new m(q)),this)},assign:function(a){return this.pushState(null,null,a).dispatch()},replace:function(a){return this.replaceState(null,null,a).dispatch()},navigate:function(){this.assign.apply(this,arguments)},rulesDispatcher:function(){function b(a,b){return Array.prototype.slice.call(this.children||this.childNodes).forEach(c.bind(this,a,b)),a}function c(b,e,f){function g(b,c){c=c||{},c.detail=c.detail||{},c.detail[v]=k||[],c.detail[w]=l||[],c.cancelable=!0,d&&"object"==typeof console&&(console.log({name:b,ruleElement:f,params:c,useURI:i,useOldURI:j}),console.trace&&console.trace());var e=new a.CustomEvent(b,c);return e}if(f.rule){var h,i=b,j=e;"number"==typeof f.parentGroup&&(i="",h=f.parentElement,h[v].length>f.parentGroup&&(i=h[v][f.parentGroup]||""),j="",h[w].length>f.parentGroup&&(j=h[w][f.parentGroup]||"")),f[v]=i[v](f.rule),"string"==typeof j?f[w]=j[v](f.rule):f[w]=[];var k=f[v],l=f[w],m=Array.prototype.slice.call(f.children);if(0===k.length){if(0===l.length||f.routerURI!==e)return;return f.uri=null,f.removeAttribute("uri"),m.forEach(c.bind(this,b,e)),f.dispatchEvent(new g(s,{detail:{type:r}})),void f.dispatchEvent(new g(r))}this.eventStack[v].push({element:f,events:[new g(v)]});var n=f.routerURI!==e;if(f.routerURI=this.uri,f.uri=k[0],f.setAttribute("uri",k[0]),0===l.length||n)return this.eventStack[t].push({element:f,events:[new g(s,{detail:{type:t}}),new g(t)]}),void m.forEach(c.bind(this,b,e));k[0]!==l[0]&&this.eventStack[u].push({element:f,events:[new g(s,{detail:{type:u}}),new g(u)]}),m.forEach(c.bind(this,b,e))}}var d=this.debug===!0||y;if(this.isPathValid&&(D.push(b.bind(this,this.uri)),!(D.length>1))){for(var e=C;D.length>0;)e=D[0].call(null,e),D.shift();F=!0;var f=this.eventStack;[u,t,v].forEach(function(a){for(;f[a].length>0;){for(var b=f[a][0].events,c=f[a][0].element;b.length>0;)c.dispatchEvent(b[0]),b.shift();f[a].shift()}}),F=!1}}};for(var G in a.history)"function"==typeof a.history[G]&&g.call(f.prototype,G);f.prototype[o]=a.history&&!!a.history.pushState,f.prototype[o]||(f.prototype[n]=!1);var H=null;f.prototype.pushState||(f.prototype.pushState=function(b,c,f){var g=h.title||"";return f=f||"",null!==H&&(h.title=H),this.avoidHashchangeHandler(),d(f)&&(a.location.href=f),"#"===f[0]&&(f=f.slice(1)),e(f)&&(f=a.location.hash.slice(1,a.location.hash.lastIndexOf("/")+1)+f),a.location.hash=f,h.title=g,H=c,this}),f.prototype.replaceState||(f.prototype.replaceState=function(b,c,f){var g=h.title||"";if(f=f||"",null!==H&&(h.title=H),this.avoidHashchangeHandler(),d(f))throw new Error("Invalid url replace.");if("#"===f[0]&&(f=f.slice(1)),e(f)){var i=a.location.hash.lastIndexOf("/")+1;f=a.location.hash.slice(1,i)+f}return f="#"+f,a.location.replace(f),h.title=g,H=c,this}),a.PushStateTree=f,"undefined"!=typeof module&&(module.exports=f)}(function(){return this}()); \ No newline at end of file +var PushStateTree = {options: {VERSION: '0.13.1'}}; +var PushStateTree={options:{VERSION:"0.13.1"}};!function(a){"use strict";function b(a){return!isNaN(parseFloat(a))&&a%1===0&&isFinite(a)}function c(a,b,c){Object.defineProperty(a,b,{get:function(){return c},set:function(){}})}function d(a){return/^[a-z0-9]+:\/\//i.test(a)}function e(a){return/^[^#/]/.test(a)}function f(b){function d(a){if("function"==typeof f.prototype[a])e[a]=function(){return f.prototype[a].apply(this,arguments)};else{if("undefined"!=typeof e[a])return;Object.defineProperty(e,a,{get:function(){return f.prototype[a]},set:function(b){f.prototype[a]=b}})}}if(b=b||{},b[n]=b[n]!==!1,!(this instanceof A))return f.apply(h.createElement("pushstatetree-route"),arguments);var e=this;this.VERSION=z;for(var g in b)b.hasOwnProperty(g)&&(e[g]=b[g]);if(f.prototype[o]){var i=b[n];Object.defineProperty(e,n,{get:function(){return i},set:function(a){i=a!==!1}})}else c(e,n,!1);Object.defineProperty(e,"beautifyLocation",{get:function(){return!!f.prototype.beautifyLocation},set:function(a){f.prototype.beautifyLocation=a===!0}}),e.beautifyLocation=b.beautifyLocation;var m;Object.defineProperty(e,"basePath",{get:function(){return m},set:function(a){a=a||"",m=a.match(/^(\/)?((.*?)\/?)(\/*)$/)[3]+"/",m.length>1&&(m="/"+m)}}),e.basePath=b.basePath;for(var r in f.prototype)f.prototype.hasOwnProperty(r)&&d(r);c(e,"length",a.history.length),c(e,"state",a.history.state);var s={url:"",uri:""};Object.defineProperty(e,"uri",{get:function(){if(s.url===a.location.href)return s.uri;var b;if(a.location.hash.length||"#"===a.location.href[j.href.length-1]){b=a.location.hash.match(/^(#*)?(.*\/?)/)[2];var c=e[n];e.beautifyLocation&&e.isPathValid&&c&&e.replaceState(e.state,e.title,"/"===b[0]?b:"/"+b)}else b=a.location.pathname+a.location.search,this.isPathValid&&(b=b.slice(this.basePath.length));return b=b.replace(/^[\/]+/,""),e.getAttribute("uri")!==b&&e.setAttribute("uri",b),s.url=a.location.href,s.uri=b,b},configurable:!0}),Object.defineProperty(e,"isPathValid",{get:function(){var b=a.location.pathname+a.location.search;return!this.basePath||0===b.indexOf(this.basePath)}}),e.eventStack={leave:[],change:[],enter:[],match:[]},a.addEventListener(q,function(){var a=e.uri,b=e.state;e.rulesDispatcher(),C=a,B=b,E&&this.dispatch()}.bind(e));var t=!1,u=function(){if(!t&&C!==e.uri){var a=e.uri,b=e.state;e.rulesDispatcher(),C=a,B=b,E&&this.dispatch()}}.bind(e);e.avoidHashchangeHandler=function(){a.removeEventListener(p,u),t=!0},a.addEventListener(p,u);var v=function(){a.dispatchEvent(new l(p))};return h.addEventListener("DOMContentLoaded",v),a.addEventListener("readystatechange",v),a.addEventListener("load",function(){v(),k&&a.setInterval(function(){return e.uri!==C?void v():void(t&&(t=!1,C=e.uri,a.addEventListener(p,u)))}.bind(e),50)}.bind(e)),this}function g(b){var c=b;this[b]=function(){var b=Array.prototype.slice.call(arguments);if(("pushState"===c||"replaceState"===c)&&!d(b[2])){if(e(b[2])){var f=this.uri.match(/^([^?#]]*)\//);f=f?f[1]+"/":"",b[2]=f+b[2]}else b[2]=b[2].match(/^([#/]*)?(.*)/)[2];this[n]?b[2]=this.basePath+b[2]:b[2]="#"+b[2]}return a.history[c].apply(a.history,b),this}}var h=a.document,i=a.window,j=a.location,k=function(){var a=i.navigator.userAgent.indexOf("Trident");return a>=0}();!function(){a.location.origin||(a.location.origin=a.location.protocol+"//"+a.location.host)}(),function(){Function.prototype.bind||(Function.prototype.bind=function(a){if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");var b=Array.prototype.slice.call(arguments,1),c=this,d=function(){},e=function(){var e=a;return this instanceof d&&a&&(e=this),c.apply(e,b.concat(Array.prototype.slice.call(arguments)))};return d.prototype=this.prototype,e.prototype=new d,e})}();var l=a.HashChangeEvent,m=a.Event;!function(){function b(a,b){b=b||{bubbles:!1,cancelable:!1,detail:void 0};var c=h.createEvent("CustomEvent");return c.initCustomEvent(a,b.bubbles,b.cancelable,b.detail),c}if(Element.prototype.addEventListener){b.prototype=m.prototype,(!a.CustomEvent||k)&&(a.CustomEvent=b),(!l||a.opera&&a.opera.version()<15||k)&&(l=a.CustomEvent),k&&(m=b);try{new l("hashchange")}catch(c){l=b}try{new m("popstate")}catch(c){m=b}}}(),function(){if(!Element.prototype.addEventListener&&Object.defineProperty){var b=h.createEventObject().constructor.prototype;Object.defineProperty(b,"target",{get:function(){return this.srcElement}});var c=function(a,b){this.__bindedFunctions||(this.__bindedFunctions=[]);var c=b;if(!("on"+a in this)||"hashchange"===a){this.__elemetIEid=this.__elemetIEid||"__ie__"+Math.random();var d=a+this.__elemetIEid,e=this,f=function(a){a.propertyName===d&&c.call(e,h.documentElement[d])};if(this.__bindedFunctions.push({original:c,binded:f}),h.documentElement.attachEvent("onpropertychange",f),"hashchange"!==a)return}var g=c.bind(this);this.__bindedFunctions.push({original:c,binded:g}),this.attachEvent("on"+a,g)};HTMLDocument.prototype.addEventListener=c,Element.prototype.addEventListener=c,i.addEventListener=c;var d=function(a,b){this.__bindedFunctions||(this.__bindedFunctions=[]);var c,d=b;if(!("on"+a in this)||"hashchange"===a){for(var e=0;ec;c++)c in this&&a.push(this[c]);return b.apply(a,arguments)}}"forEach"in Array.prototype||(Array.prototype.forEach=function(a,b){for(var c=0;c>>0;if("function"!=typeof a)throw new TypeError;for(var d=[],e=arguments.length>=2?arguments[1]:void 0,f=0;c>f;f++)if(f in b){var g=b[f];a.call(e,g,f,b)&&d.push(g)}return d})}();var n="usePushState",o="hasPushState",p="hashchange",q="popstate",r="leave",s="update",t="enter",u="change",v="match",w="oldMatch",x=a.PushStateTree&&a.PushStateTree.options||{},y=a.DEBUG||x.DEBUG,z=x.VERSION||"development",A="undefined"!=typeof HTMLElement?HTMLElement:Element,B=null,C=null,D=[],E=!1,F=!1;f.prototype={beautifyLocation:!0,createRule:function(a){var c=h.createElement("pushstatetree-rule"),d=new RegExp("");Object.defineProperty(c,"rule",{get:function(){return d},set:function(a){if(a instanceof RegExp)d=a;else{if(a===d.toString())return;var b=(a+"").match(/^\/(.+)\/([gmi]*)|(.*)/);d=new RegExp(b[1]||b[3],b[2])}c.setAttribute("rule",d.toString())}}),Object.defineProperty(c,"parentGroup",{get:function(){var a=c.getAttribute("parent-group");return a&&b(a)?+a:null},set:function(a){b(a)?c.setAttribute("parent-group",a):c.removeAttribute("parent-group")}});for(var e in a)a.hasOwnProperty(e)&&(c[e]=a[e]);var f=[];Object.defineProperty(c,v,{get:function(){return f},set:function(a){f=a instanceof Array?a:[]}});var g=[];return Object.defineProperty(c,w,{get:function(){return g},set:function(a){g=a instanceof Array?a:[]}}),c[v]=[],c[w]=[],["assign","navigate","replace","dispatch","pushState","replaceState"].forEach(function(a){c[a]=function(){this.parentElement[a].apply(this.parentElement,arguments)}}),c},add:function(a){return this.appendChild(this.createRule(a)),this},remove:function(a){var b=a;return"string"==typeof a&&(b=this.querySelector(a)),b&&b.parentElement?(b.parentElement.removeChild(b),b):void 0},dispatch:function(){return F?(E=!0,this):(E=!1,a.dispatchEvent(new m(q)),this)},assign:function(a){return this.pushState(null,null,a).dispatch()},replace:function(a){return this.replaceState(null,null,a).dispatch()},navigate:function(){this.assign.apply(this,arguments)},rulesDispatcher:function(){function b(a,b){return Array.prototype.slice.call(this.children||this.childNodes).forEach(c.bind(this,a,b)),a}function c(b,e,f){function g(b,c){c=c||{},c.detail=c.detail||{},c.detail[v]=k||[],c.detail[w]=l||[],c.cancelable=!0,d&&"object"==typeof console&&(console.log({name:b,ruleElement:f,params:c,useURI:i,useOldURI:j}),console.trace&&console.trace());var e=new a.CustomEvent(b,c);return e}if(f.rule){var h,i=b,j=e;"number"==typeof f.parentGroup&&(i="",h=f.parentElement,h[v].length>f.parentGroup&&(i=h[v][f.parentGroup]||""),j="",h[w].length>f.parentGroup&&(j=h[w][f.parentGroup]||"")),f[v]=i[v](f.rule),"string"==typeof j?f[w]=j[v](f.rule):f[w]=[];var k=f[v],l=f[w],m=Array.prototype.slice.call(f.children);if(0===k.length){if(0===l.length||f.routerURI!==e)return;return f.uri=null,f.removeAttribute("uri"),m.forEach(c.bind(this,b,e)),f.dispatchEvent(new g(s,{detail:{type:r}})),void f.dispatchEvent(new g(r))}this.eventStack[v].push({element:f,events:[new g(v)]});var n=f.routerURI!==e;if(f.routerURI=this.uri,f.uri=k[0],f.setAttribute("uri",k[0]),0===l.length||n)return this.eventStack[t].push({element:f,events:[new g(s,{detail:{type:t}}),new g(t)]}),void m.forEach(c.bind(this,b,e));k[0]!==l[0]&&this.eventStack[u].push({element:f,events:[new g(s,{detail:{type:u}}),new g(u)]}),m.forEach(c.bind(this,b,e))}}var d=this.debug===!0||y;if(this.isPathValid&&(D.push(b.bind(this,this.uri)),!(D.length>1))){for(var e=C;D.length>0;)e=D[0].call(null,e),D.shift();F=!0;var f=this.eventStack;[u,t,v].forEach(function(a){for(;f[a].length>0;){for(var b=f[a][0].events,c=f[a][0].element;b.length>0;)c.dispatchEvent(b[0]),b.shift();f[a].shift()}}),F=!1}}};for(var G in a.history)"function"==typeof a.history[G]&&g.call(f.prototype,G);f.prototype[o]=a.history&&!!a.history.pushState,f.prototype[o]||(f.prototype[n]=!1);var H=null;f.prototype.pushState||(f.prototype.pushState=function(b,c,f){var g=h.title||"";return f=f||"",null!==H&&(h.title=H),this.avoidHashchangeHandler(),d(f)&&(a.location.href=f),"#"===f[0]&&(f=f.slice(1)),e(f)&&(f=a.location.hash.slice(1,a.location.hash.lastIndexOf("/")+1)+f),a.location.hash=f,h.title=g,H=c,this}),f.prototype.replaceState||(f.prototype.replaceState=function(b,c,f){var g=h.title||"";if(f=f||"",null!==H&&(h.title=H),this.avoidHashchangeHandler(),d(f))throw new Error("Invalid url replace.");if("#"===f[0]&&(f=f.slice(1)),e(f)){var i=a.location.hash.lastIndexOf("/")+1;f=a.location.hash.slice(1,i)+f}return f="#"+f,a.location.replace(f),h.title=g,H=c,this}),a.PushStateTree=f,"undefined"!=typeof module&&(module.exports=f)}(function(){return this}()); \ No newline at end of file diff --git a/src/pushStateTree.js b/src/pushStateTree.js index b2ddd53..e8592ed 100644 --- a/src/pushStateTree.js +++ b/src/pushStateTree.js @@ -445,7 +445,8 @@ // Remove all begin # chars from the location when using hash uri = root.location.hash.match(/^(#*)?(.*\/?)/)[2]; - if (rootElement.beautifyLocation && rootElement[USE_PUSH_STATE]) { + var usePushState = rootElement[USE_PUSH_STATE]; + if (rootElement.beautifyLocation && rootElement.isPathValid && usePushState) { // when using pushState, replace the browser location to avoid ugly URLs rootElement.replaceState( diff --git a/test/specs/route-beutifyLocation.js b/test/specs/route-beutifyLocation.js index 3d47043..7cd1c57 100644 --- a/test/specs/route-beutifyLocation.js +++ b/test/specs/route-beutifyLocation.js @@ -88,4 +88,26 @@ describe('PushStateTree beutifyLocation should', function() { expect(location.pathname).toEqual('/abc'); }); + it('not apply beautifyLocation when the basePath is not fulfilled', function(){ + history.pushState(null, null, '/invalidBasePath/'); + var pst = new PushStateTree({ + beautifyLocation: true, + basePath: '/test/' + }); + location.hash = '/abc'; + expect(pst.uri).toEqual('abc'); + expect(location.hash).toEqual('#/abc'); + }); + + it('apply beautifyLocation when the basePath is fulfilled', function(){ + history.pushState(null, null, '/test/'); + var pst = new PushStateTree({ + beautifyLocation: true, + basePath: '/test/' + }); + location.hash = '/abc'; + expect(pst.uri).toEqual('abc'); + expect(location.hash).toEqual(''); + }); + });