preact-router
Advanced tools
Comparing version 2.0.0-beta1 to 2.0.0
@@ -15,20 +15,2 @@ (function (global, factory) { | ||
babelHelpers.createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
babelHelpers.extends = Object.assign || function (target) { | ||
@@ -155,4 +137,2 @@ for (var i = 1; i < arguments.length; i++) { | ||
var DOM = typeof document !== 'undefined'; | ||
function route(url) { | ||
@@ -165,3 +145,3 @@ var replace = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; | ||
} | ||
if (DOM && typeof history !== 'undefined') { | ||
if (typeof history !== 'undefined' && history.pushState) { | ||
if (replace === true) { | ||
@@ -191,19 +171,41 @@ history.replaceState(null, null, url); | ||
function handleLinkClick(e, target) { | ||
target = target || e && (e.currentTarget || e.target) || this; | ||
if (!target) return; | ||
if (route(target.getAttribute('href')) === true) { | ||
function routeFromLink(node) { | ||
// only valid elements | ||
if (!node || !node.getAttribute) return; | ||
var href = node.getAttribute('href'), | ||
target = node.getAttribute('target'); | ||
// ignore links with targets and non-path URLs | ||
if (!href || !href.match(/^\//g) || target && !target.match(/^_?self$/i)) return; | ||
// attempt to route, if no match simply cede control to browser | ||
return route(href); | ||
} | ||
function handleLinkClick(e) { | ||
routeFromLink(e.currentTarget || e.target || this); | ||
return prevent(e); | ||
} | ||
function prevent(e) { | ||
if (e) { | ||
if (e.stopImmediatePropagation) e.stopImmediatePropagation(); | ||
e.stopPropagation(); | ||
if (e.stopPropagation) e.stopPropagation(); | ||
e.preventDefault(); | ||
return false; | ||
} | ||
return false; | ||
} | ||
function linkHandler(e) { | ||
function delegateLinkHandler(e) { | ||
// ignore events the browser takes care of already: | ||
if (e.ctrlKey || e.metaKey || e.altKey || e.shiftKey) return; | ||
var t = e.target; | ||
do { | ||
var href = String(t.nodeName).toUpperCase() === 'A' && t.getAttribute('href'); | ||
if (href && href.match(/^\//g)) { | ||
return handleLinkClick(e, t); | ||
if (String(t.nodeName).toUpperCase() === 'A' && t.getAttribute('href')) { | ||
// if link is handled by the router, prevent browser defaults | ||
if (routeFromLink(t)) { | ||
return prevent(e); | ||
} | ||
} | ||
@@ -213,13 +215,13 @@ } while (t = t.parentNode); | ||
if (DOM) { | ||
if (typeof addEventListener === 'function') { | ||
addEventListener('popstate', function () { | ||
return routeTo(getCurrentUrl()); | ||
}); | ||
document.body.addEventListener('click', linkHandler); | ||
addEventListener('click', delegateLinkHandler); | ||
} | ||
var Link = function Link(_ref) { | ||
var Link = function (_ref) { | ||
var children = _ref.children; | ||
var props = babelHelpers.objectWithoutProperties(_ref, ['children']); | ||
return React.createElement( | ||
return preact.h( | ||
'a', | ||
@@ -235,4 +237,2 @@ babelHelpers.extends({}, props, { onClick: handleLinkClick }), | ||
function Router() { | ||
var _Object$getPrototypeO; | ||
var _temp, _this, _ret; | ||
@@ -246,3 +246,3 @@ | ||
return _ret = (_temp = (_this = babelHelpers.possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(Router)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.state = { | ||
return _ret = (_temp = (_this = babelHelpers.possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.state = { | ||
url: _this.props.url || getCurrentUrl() | ||
@@ -252,73 +252,73 @@ }, _temp), babelHelpers.possibleConstructorReturn(_this, _ret); | ||
babelHelpers.createClass(Router, [{ | ||
key: 'routeTo', | ||
value: function routeTo(url) { | ||
this._didRoute = false; | ||
this.setState({ url: url }); | ||
this.forceUpdate(); | ||
return this._didRoute; | ||
} | ||
}, { | ||
key: 'componentWillMount', | ||
value: function componentWillMount() { | ||
ROUTERS.push(this); | ||
} | ||
}, { | ||
key: 'componentWillUnmount', | ||
value: function componentWillUnmount() { | ||
ROUTERS.splice(ROUTERS.indexOf(this), 1); | ||
} | ||
}, { | ||
key: 'render', | ||
value: function render(_ref2, _ref3) { | ||
var children = _ref2.children; | ||
var onChange = _ref2.onChange; | ||
var url = _ref3.url; | ||
Router.prototype.shouldComponentUpdate = function shouldComponentUpdate(props) { | ||
if (props.static !== true) return true; | ||
return props.url !== this.props.url || props.onChange !== this.props.onChange; | ||
}; | ||
var active = children.slice().sort(pathRankSort).filter(function (_ref4) { | ||
var attributes = _ref4.attributes; | ||
Router.prototype.routeTo = function routeTo(url) { | ||
this._didRoute = false; | ||
this.setState({ url: url }); | ||
this.forceUpdate(); | ||
return this._didRoute; | ||
}; | ||
var path = attributes.path, | ||
matches = exec(url, path, attributes); | ||
if (matches) { | ||
attributes.url = url; | ||
attributes.matches = matches; | ||
// copy matches onto props | ||
for (var i in matches) { | ||
if (matches.hasOwnProperty(i)) { | ||
attributes[i] = matches[i]; | ||
} | ||
Router.prototype.componentWillMount = function componentWillMount() { | ||
ROUTERS.push(this); | ||
}; | ||
Router.prototype.componentWillUnmount = function componentWillUnmount() { | ||
ROUTERS.splice(ROUTERS.indexOf(this), 1); | ||
}; | ||
Router.prototype.render = function render(_ref2, _ref3) { | ||
var children = _ref2.children; | ||
var onChange = _ref2.onChange; | ||
var url = _ref3.url; | ||
var active = children.slice().sort(pathRankSort).filter(function (_ref4) { | ||
var attributes = _ref4.attributes; | ||
var path = attributes.path, | ||
matches = exec(url, path, attributes); | ||
if (matches) { | ||
attributes.url = url; | ||
attributes.matches = matches; | ||
// copy matches onto props | ||
for (var i in matches) { | ||
if (matches.hasOwnProperty(i)) { | ||
attributes[i] = matches[i]; | ||
} | ||
return true; | ||
} | ||
}); | ||
return true; | ||
} | ||
}); | ||
var current = active[0] || null; | ||
this._didRoute = !!current; | ||
var current = active[0] || null; | ||
this._didRoute = !!current; | ||
var previous = this.previousUrl; | ||
if (url !== previous) { | ||
this.previousUrl = url; | ||
if (typeof onChange === 'function') { | ||
onChange({ | ||
router: this, | ||
url: url, | ||
previous: previous, | ||
active: active, | ||
current: current | ||
}); | ||
} | ||
var previous = this.previousUrl; | ||
if (url !== previous) { | ||
this.previousUrl = url; | ||
if (typeof onChange === 'function') { | ||
onChange({ | ||
router: this, | ||
url: url, | ||
previous: previous, | ||
active: active, | ||
current: current | ||
}); | ||
} | ||
} | ||
return current; | ||
} | ||
}]); | ||
return current; | ||
}; | ||
return Router; | ||
}(preact.Component); | ||
var Route = function Route(_ref5) { | ||
var Route = function (_ref5) { | ||
var RoutedComponent = _ref5.component; | ||
var url = _ref5.url; | ||
var matches = _ref5.matches; | ||
return React.createElement(RoutedComponent, { url: url, matches: matches }); | ||
return preact.h(RoutedComponent, { url: url, matches: matches }); | ||
}; | ||
@@ -325,0 +325,0 @@ |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("preact")):"function"==typeof define&&define.amd?define(["preact"],t):e.preactRouter=t(e.preact)}(this,function(e){"use strict";function t(e,t){var r=arguments.length<=2||void 0===arguments[2]?p:arguments[2],o=/(?:\?([^#]*))?(#.*)?$/,i=e.match(o),a={},u=void 0;if(i&&i[1])for(var c=i[1].split("&"),l=0;l<c.length;l++){var f=c[l].split("=");a[decodeURIComponent(f[0])]=decodeURIComponent(f.slice(1).join("="))}e=n(e.replace(o,"")),t=n(t||"");for(var s=Math.max(e.length,t.length),h=0;s>h;h++)if(t[h]&&":"===t[h].charAt(0)){var d=t[h].replace(/(^\:|[+*?]+$)/g,""),v=(t[h].match(/[+*?]+$/)||p)[0]||"",y=~v.indexOf("+"),m=~v.indexOf("*"),b=e[h]||"";if(!b&&!m&&(v.indexOf("?")<0||y)){u=!1;break}if(a[d]=decodeURIComponent(b),y||m){a[d]=e.slice(h).map(decodeURIComponent).join("/");break}}else if(t[h]!==e[h]){u=!1;break}return r["default"]!==!0&&u===!1?!1:a}function r(e,t){var r=e.attributes||p,n=t.attributes||p;if(r["default"])return 1;if(n["default"])return-1;var i=o(r.path)-o(n.path);return i||r.path.length-n.path.length}function n(e){return i(e).split("/")}function o(e){return(i(e).match(/\/+/g)||"").length}function i(e){return e.replace(/(^\/+|\/+$)/g,"")}function a(e){var t=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return"string"!=typeof e&&e.url&&(t=e.replace,e=e.url),v&&"undefined"!=typeof history&&(t===!0?history.replaceState(null,null,e):history.pushState(null,null,e)),u(e)}function u(e){var t=!1;return h.forEach(function(r){r.routeTo(e)===!0&&(t=!0)}),t}function c(){var e="undefined"!=typeof location?location:d;return""+(e.pathname||"")+(e.search||"")}function l(e,t){return(t=t||e&&(e.currentTarget||e.target)||this)&&a(t.getAttribute("href"))===!0?(e.stopImmediatePropagation&&e.stopImmediatePropagation(),e.stopPropagation(),e.preventDefault(),!1):void 0}function f(e){var t=e.target;do{var r="A"===String(t.nodeName).toUpperCase()&&t.getAttribute("href");if(r&&r.match(/^\//g))return l(e,t)}while(t=t.parentNode)}var s={};s.a=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},s.createClass=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),s["extends"]=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},s.inherits=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},s.objectWithoutProperties=function(e,t){var r={};for(var n in e)t.indexOf(n)>=0||Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n]);return r},s.possibleConstructorReturn=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t};var p={},h=[],d={},v="undefined"!=typeof document;v&&(addEventListener("popstate",function(){return u(c())}),document.body.addEventListener("click",f));var y=function(e){var t=e.children,r=s.objectWithoutProperties(e,["children"]);return React.createElement("a",s["extends"]({},r,{onClick:l}),t)},m=function(e){function n(){var e,t,r,o;s.a(this,n);for(var i=arguments.length,a=Array(i),u=0;i>u;u++)a[u]=arguments[u];return t=r=s.possibleConstructorReturn(this,(e=Object.getPrototypeOf(n)).call.apply(e,[this].concat(a))),r.state={url:r.props.url||c()},o=t,s.possibleConstructorReturn(r,o)}return s.inherits(n,e),s.createClass(n,[{key:"routeTo",value:function(e){return this._didRoute=!1,this.setState({url:e}),this.forceUpdate(),this._didRoute}},{key:"componentWillMount",value:function(){h.push(this)}},{key:"componentWillUnmount",value:function(){h.splice(h.indexOf(this),1)}},{key:"render",value:function(e,n){var o=e.children,i=e.onChange,a=n.url,u=o.slice().sort(r).filter(function(e){var r=e.attributes,n=r.path,o=t(a,n,r);if(o){r.url=a,r.matches=o;for(var i in o)o.hasOwnProperty(i)&&(r[i]=o[i]);return!0}}),c=u[0]||null;this._didRoute=!!c;var l=this.previousUrl;return a!==l&&(this.previousUrl=a,"function"==typeof i&&i({router:this,url:a,previous:l,active:u,current:c})),c}}]),n}(e.Component),b=function(e){var t=e.component,r=e.url,n=e.matches;return React.createElement(t,{url:r,matches:n})};return m.route=a,m.Router=m,m.Route=b,m.Link=y,m}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("preact")):"function"==typeof define&&define.amd?define(["preact"],e):t.preactRouter=e(t.preact)}(this,function(t){"use strict";function e(t,e){var r=arguments.length<=2||void 0===arguments[2]?d:arguments[2],o=/(?:\?([^#]*))?(#.*)?$/,i=t.match(o),a={},u=void 0;if(i&&i[1])for(var c=i[1].split("&"),p=0;p<c.length;p++){var s=c[p].split("=");a[decodeURIComponent(s[0])]=decodeURIComponent(s.slice(1).join("="))}t=n(t.replace(o,"")),e=n(e||"");for(var l=Math.max(t.length,e.length),f=0;l>f;f++)if(e[f]&&":"===e[f].charAt(0)){var h=e[f].replace(/(^\:|[+*?]+$)/g,""),v=(e[f].match(/[+*?]+$/)||d)[0]||"",y=~v.indexOf("+"),g=~v.indexOf("*"),m=t[f]||"";if(!m&&!g&&(v.indexOf("?")<0||y)){u=!1;break}if(a[h]=decodeURIComponent(m),y||g){a[h]=t.slice(f).map(decodeURIComponent).join("/");break}}else if(e[f]!==t[f]){u=!1;break}return r["default"]!==!0&&u===!1?!1:a}function r(t,e){var r=t.attributes||d,n=e.attributes||d;if(r["default"])return 1;if(n["default"])return-1;var i=o(r.path)-o(n.path);return i||r.path.length-n.path.length}function n(t){return i(t).split("/")}function o(t){return(i(t).match(/\/+/g)||"").length}function i(t){return t.replace(/(^\/+|\/+$)/g,"")}function a(t){var e=arguments.length<=1||void 0===arguments[1]?!1:arguments[1];return"string"!=typeof t&&t.url&&(e=t.replace,t=t.url),"undefined"!=typeof history&&history.pushState&&(e===!0?history.replaceState(null,null,t):history.pushState(null,null,t)),u(t)}function u(t){var e=!1;return v.forEach(function(r){r.routeTo(t)===!0&&(e=!0)}),e}function c(){var t="undefined"!=typeof location?location:y;return""+(t.pathname||"")+(t.search||"")}function p(t){if(t&&t.getAttribute){var e=t.getAttribute("href"),r=t.getAttribute("target");if(e&&e.match(/^\//g)&&(!r||r.match(/^_?self$/i)))return a(e)}}function s(t){return p(t.currentTarget||t.target||this),l(t)}function l(t){return t&&(t.stopImmediatePropagation&&t.stopImmediatePropagation(),t.stopPropagation&&t.stopPropagation(),t.preventDefault()),!1}function f(t){if(!(t.ctrlKey||t.metaKey||t.altKey||t.shiftKey)){var e=t.target;do if("A"===String(e.nodeName).toUpperCase()&&e.getAttribute("href")&&p(e))return l(t);while(e=e.parentNode)}}var h={};h.classCallCheck=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},h["extends"]=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},h.inherits=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},h.objectWithoutProperties=function(t,e){var r={};for(var n in t)e.indexOf(n)>=0||Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n]);return r},h.possibleConstructorReturn=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e};var d={},v=[],y={};"function"==typeof addEventListener&&(addEventListener("popstate",function(){return u(c())}),addEventListener("click",f));var g=function(e){var r=e.children,n=h.objectWithoutProperties(e,["children"]);return t.h("a",h["extends"]({},n,{onClick:s}),r)},m=function(t){function n(){var e,r,o;h.classCallCheck(this,n);for(var i=arguments.length,a=Array(i),u=0;i>u;u++)a[u]=arguments[u];return e=r=h.possibleConstructorReturn(this,t.call.apply(t,[this].concat(a))),r.state={url:r.props.url||c()},o=e,h.possibleConstructorReturn(r,o)}return h.inherits(n,t),n.prototype.shouldComponentUpdate=function(t){return t["static"]!==!0?!0:t.url!==this.props.url||t.onChange!==this.props.onChange},n.prototype.routeTo=function(t){return this._didRoute=!1,this.setState({url:t}),this.forceUpdate(),this._didRoute},n.prototype.componentWillMount=function(){v.push(this)},n.prototype.componentWillUnmount=function(){v.splice(v.indexOf(this),1)},n.prototype.render=function(t,n){var o=t.children,i=t.onChange,a=n.url,u=o.slice().sort(r).filter(function(t){var r=t.attributes,n=r.path,o=e(a,n,r);if(o){r.url=a,r.matches=o;for(var i in o)o.hasOwnProperty(i)&&(r[i]=o[i]);return!0}}),c=u[0]||null;this._didRoute=!!c;var p=this.previousUrl;return a!==p&&(this.previousUrl=a,"function"==typeof i&&i({router:this,url:a,previous:p,active:u,current:c})),c},n}(t.Component),b=function(e){var r=e.component,n=e.url,o=e.matches;return t.h(r,{url:n,matches:o})};return m.route=a,m.Router=m,m.Route=b,m.Link=g,m}); | ||
//# sourceMappingURL=preact-router.min.js.map |
{ | ||
"name": "preact-router", | ||
"amdName": "preactRouter", | ||
"version": "2.0.0-beta1", | ||
"version": "2.0.0", | ||
"description": "Connect your components up to that address bar.", | ||
@@ -13,6 +13,9 @@ "main": "dist/preact-router.js", | ||
"transpile": "rollup -c rollup.config.js -m ${npm_package_main}.map -f umd -n $npm_package_amdName $npm_package_jsnext_main -o $npm_package_main", | ||
"minify": "uglifyjs $npm_package_main --mangle-regex=\"/^(classCallCheck|components|normalizeName|add|clean|process|collect|create|nodes|itemsOffline)$/\" --mangle-props -cm -o $npm_package_minified_main -p relative --in-source-map ${npm_package_main}.map --source-map ${npm_package_minified_main}.map", | ||
"minify": "uglifyjs $npm_package_main -cm -o $npm_package_minified_main -p relative --in-source-map ${npm_package_main}.map --source-map ${npm_package_minified_main}.map", | ||
"size": "size=$(gzip-size $npm_package_minified_main) && echo \"gzip size: $size / $(pretty-bytes $size)\"", | ||
"test": "eslint {src,test} && mocha --compilers js:babel-register test/**/*.js", | ||
"prepublish": "npm run build", | ||
"test": "npm-run-all lint build test:karma", | ||
"lint": "eslint {src,test}", | ||
"test:karma": "karma start --single-run", | ||
"test:watch": "karma start", | ||
"prepublish": "npm-run-all build test", | ||
"release": "npm run build && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" | ||
@@ -40,14 +43,24 @@ }, | ||
"babel-eslint": "^6.0.4", | ||
"babel-loader": "^6.2.4", | ||
"babel-preset-es2015": "^6.9.0", | ||
"babel-preset-es2015-rollup": "^1.1.1", | ||
"babel-preset-es2015-minimal": "^2.0.0", | ||
"babel-preset-es2015-minimal-rollup": "^2.0.0", | ||
"babel-preset-react": "^6.5.0", | ||
"babel-preset-stage-0": "^6.5.0", | ||
"babel-register": "^6.9.0", | ||
"chai": "^3.5.0", | ||
"diff": "^2.2.3", | ||
"eslint": "^2.11.1", | ||
"eslint-plugin-react": "^5.1.1", | ||
"gzip-size-cli": "^1.0.0", | ||
"karma": "^0.13.22", | ||
"karma-chai-sinon": "^0.1.5", | ||
"karma-mocha": "^1.0.1", | ||
"karma-mocha-reporter": "^2.0.3", | ||
"karma-phantomjs-launcher": "^1.0.0", | ||
"karma-sourcemap-loader": "^0.3.7", | ||
"karma-webpack": "^1.7.0", | ||
"mkdirp": "^0.5.1", | ||
"mocha": "^2.5.0", | ||
"npm-run-all": "^2.0.0", | ||
"phantomjs-prebuilt": "^2.1.7", | ||
"preact": "^4.8.0", | ||
@@ -61,4 +74,5 @@ "pretty-bytes-cli": "^1.0.0", | ||
"sinon-chai": "^2.8.0", | ||
"uglify-js": "^2.6.1" | ||
"uglify-js": "^2.6.1", | ||
"webpack": "^1.13.1" | ||
} | ||
} |
@@ -0,4 +1,7 @@ | ||
import fs from 'fs'; | ||
import babel from 'rollup-plugin-babel'; | ||
import memory from 'rollup-plugin-memory'; | ||
var babelRc = JSON.parse(fs.readFileSync('.babelrc','utf8')); // eslint-disable-line | ||
export default { | ||
@@ -13,3 +16,4 @@ exports: 'default', | ||
babelrc: false, | ||
presets: ['es2015-rollup', 'stage-0', 'react'], | ||
presets: ['es2015-minimal-rollup'].concat(babelRc.presets.slice(1)), | ||
plugins: babelRc.plugins, | ||
exclude: 'node_modules/**' | ||
@@ -16,0 +20,0 @@ }) |
@@ -8,5 +8,3 @@ import { h, Component } from 'preact'; | ||
const DOM = typeof document!=='undefined'; | ||
function route(url, replace=false) { | ||
@@ -17,3 +15,3 @@ if (typeof url!=='string' && url.url) { | ||
} | ||
if (DOM && typeof history!=='undefined') { | ||
if (typeof history!=='undefined' && history.pushState) { | ||
if (replace===true) { | ||
@@ -47,20 +45,44 @@ history.replaceState(null, null, url); | ||
function handleLinkClick(e, target) { | ||
target = target || (e && (e.currentTarget || e.target)) || this; | ||
if (!target) return; | ||
if (route(target.getAttribute('href'))===true) { | ||
function routeFromLink(node) { | ||
// only valid elements | ||
if (!node || !node.getAttribute) return; | ||
let href = node.getAttribute('href'), | ||
target = node.getAttribute('target'); | ||
// ignore links with targets and non-path URLs | ||
if (!href || !href.match(/^\//g) || (target && !target.match(/^_?self$/i))) return; | ||
// attempt to route, if no match simply cede control to browser | ||
return route(href); | ||
} | ||
function handleLinkClick(e) { | ||
routeFromLink(e.currentTarget || e.target || this); | ||
return prevent(e); | ||
} | ||
function prevent(e) { | ||
if (e) { | ||
if (e.stopImmediatePropagation) e.stopImmediatePropagation(); | ||
e.stopPropagation(); | ||
if (e.stopPropagation) e.stopPropagation(); | ||
e.preventDefault(); | ||
return false; | ||
} | ||
return false; | ||
} | ||
function linkHandler(e) { | ||
function delegateLinkHandler(e) { | ||
// ignore events the browser takes care of already: | ||
if (e.ctrlKey || e.metaKey || e.altKey || e.shiftKey) return; | ||
let t = e.target; | ||
do { | ||
let href = String(t.nodeName).toUpperCase()==='A' && t.getAttribute('href'); | ||
if (href && href.match(/^\//g)) { | ||
return handleLinkClick(e, t); | ||
if (String(t.nodeName).toUpperCase()==='A' && t.getAttribute('href')) { | ||
// if link is handled by the router, prevent browser defaults | ||
if (routeFromLink(t)) { | ||
return prevent(e); | ||
} | ||
} | ||
@@ -71,5 +93,5 @@ } while ((t=t.parentNode)); | ||
if (DOM) { | ||
if (typeof addEventListener==='function') { | ||
addEventListener('popstate', () => routeTo(getCurrentUrl())); | ||
document.body.addEventListener('click', linkHandler); | ||
addEventListener('click', delegateLinkHandler); | ||
} | ||
@@ -88,2 +110,7 @@ | ||
shouldComponentUpdate(props) { | ||
if (props.static!==true) return true; | ||
return props.url!==this.props.url || props.onChange!==this.props.onChange; | ||
} | ||
routeTo(url) { | ||
@@ -90,0 +117,0 @@ this._didRoute = false; |
import { h } from 'preact'; | ||
import { expect } from 'chai'; | ||
const router = require('../'); // eslint-disable-line | ||
const router = require('../'); | ||
const { Router, Link, route } = router; | ||
/** @jsx h */ | ||
@@ -7,0 +5,0 @@ describe('dist', () => { |
@@ -1,8 +0,3 @@ | ||
import { Router, Link, route } from '../src'; | ||
import { Router, Link, route } from 'src'; | ||
import { h } from 'preact'; | ||
import chai, { expect } from 'chai'; | ||
import { spy } from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
chai.use(sinonChai); | ||
/** @jsx h */ | ||
@@ -93,3 +88,3 @@ describe('preact-router', () => { | ||
expect(new Router({})).to.have.deep.property('state.url', ''); | ||
expect(new Router({})).to.have.deep.property('state.url', location.pathname + (location.search || '')); | ||
}); | ||
@@ -110,3 +105,3 @@ }); | ||
spy(router, 'routeTo'); | ||
sinon.spy(router, 'routeTo'); | ||
@@ -113,0 +108,0 @@ router.componentWillMount(); |
@@ -1,3 +0,2 @@ | ||
import { exec, pathRankSort, segmentize, rank, strip } from '../src/util'; | ||
import { expect } from 'chai'; | ||
import { exec, pathRankSort, segmentize, rank, strip } from 'src/util'; | ||
@@ -4,0 +3,0 @@ describe('util', () => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
53107
19
869
1
34
1