preact-router
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -136,2 +136,9 @@ (function (global, factory) { | ||
// hangs off all elements created by preact | ||
var ATTR_KEY = typeof Symbol !== 'undefined' ? Symbol.for('preactattr') : '__preactattr_'; | ||
function isPreactElement(node) { | ||
return ATTR_KEY in node; | ||
} | ||
function route(url) { | ||
@@ -203,3 +210,3 @@ var replace = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; | ||
do { | ||
if (String(t.nodeName).toUpperCase() === 'A' && t.getAttribute('href')) { | ||
if (String(t.nodeName).toUpperCase() === 'A' && t.getAttribute('href') && isPreactElement(t)) { | ||
// if link is handled by the router, prevent browser defaults | ||
@@ -206,0 +213,0 @@ if (routeFromLink(t)) { |
@@ -1,2 +0,2 @@ | ||
!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}); | ||
!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]?y: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 f=Math.max(t.length,e.length),l=0;f>l;l++)if(e[l]&&":"===e[l].charAt(0)){var h=e[l].replace(/(^\:|[+*?]+$)/g,""),d=(e[l].match(/[+*?]+$/)||y)[0]||"",v=~d.indexOf("+"),m=~d.indexOf("*"),g=t[l]||"";if(!g&&!m&&(d.indexOf("?")<0||v)){u=!1;break}if(a[h]=decodeURIComponent(g),v||m){a[h]=t.slice(l).map(decodeURIComponent).join("/");break}}else if(e[l]!==t[l]){u=!1;break}return r["default"]!==!0&&u===!1?!1:a}function r(t,e){var r=t.attributes||y,n=e.attributes||y;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){return g in t}function u(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)),c(t)}function c(t){var e=!1;return v.forEach(function(r){r.routeTo(t)===!0&&(e=!0)}),e}function p(){var t="undefined"!=typeof location?location:m;return""+(t.pathname||"")+(t.search||"")}function s(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 u(e)}}function f(t){return s(t.currentTarget||t.target||this),l(t)}function l(t){return t&&(t.stopImmediatePropagation&&t.stopImmediatePropagation(),t.stopPropagation&&t.stopPropagation(),t.preventDefault()),!1}function h(t){if(!(t.ctrlKey||t.metaKey||t.altKey||t.shiftKey)){var e=t.target;do if("A"===String(e.nodeName).toUpperCase()&&e.getAttribute("href")&&a(e)&&s(e))return l(t);while(e=e.parentNode)}}var d={};d.classCallCheck=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},d["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},d.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)},d.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},d.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 y={},v=[],m={},g="undefined"!=typeof Symbol?Symbol["for"]("preactattr"):"__preactattr_";"function"==typeof addEventListener&&(addEventListener("popstate",function(){return c(p())}),addEventListener("click",h));var b=function(e){var r=e.children,n=d.objectWithoutProperties(e,["children"]);return t.h("a",d["extends"]({},n,{onClick:f}),r)},C=function(t){function n(){var e,r,o;d.classCallCheck(this,n);for(var i=arguments.length,a=Array(i),u=0;i>u;u++)a[u]=arguments[u];return e=r=d.possibleConstructorReturn(this,t.call.apply(t,[this].concat(a))),r.state={url:r.props.url||p()},o=e,d.possibleConstructorReturn(r,o)}return d.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),O=function(e){var r=e.component,n=e.url,o=e.matches;return t.h(r,{url:n,matches:o})};return C.route=u,C.Router=C,C.Route=O,C.Link=b,C}); | ||
//# sourceMappingURL=preact-router.min.js.map |
{ | ||
"name": "preact-router", | ||
"amdName": "preactRouter", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Connect your components up to that address bar.", | ||
@@ -41,4 +41,8 @@ "main": "dist/preact-router.js", | ||
"babel-cli": "^6.9.0", | ||
"babel-core": "^6.9.1", | ||
"babel-eslint": "^6.0.4", | ||
"babel-loader": "^6.2.4", | ||
"babel-plugin-transform-class-properties": "^6.9.1", | ||
"babel-plugin-transform-es2015-classes": "^6.9.0", | ||
"babel-plugin-transform-react-jsx": "^6.8.0", | ||
"babel-preset-es2015": "^6.9.0", | ||
@@ -51,6 +55,6 @@ "babel-preset-es2015-minimal": "^2.0.0", | ||
"diff": "^2.2.3", | ||
"eslint": "^2.11.1", | ||
"eslint": "^3.0.0", | ||
"eslint-plugin-react": "^5.1.1", | ||
"gzip-size-cli": "^1.0.0", | ||
"karma": "^0.13.22", | ||
"karma": "^1.0.0", | ||
"karma-chai-sinon": "^0.1.5", | ||
@@ -66,6 +70,6 @@ "karma-mocha": "^1.0.1", | ||
"phantomjs-prebuilt": "^2.1.7", | ||
"preact": "^4.8.0", | ||
"preact": "^5.3.1", | ||
"pretty-bytes-cli": "^1.0.0", | ||
"rimraf": "^2.5.1", | ||
"rollup": "^0.26.0", | ||
"rollup": "^0.34.1", | ||
"rollup-plugin-babel": "^2.4.0", | ||
@@ -72,0 +76,0 @@ "rollup-plugin-memory": "^1.0.0", |
@@ -8,4 +8,5 @@ # preact-router | ||
`preact-router` provides a `<Router />` component that conditionally renders its children when the URL matches their `path`. It also includes a convenient `<Link />` component as a drop-in replacement for `<a />` wired up to the router. | ||
`preact-router` provides a `<Router />` component that conditionally renders its children when the URL matches their `path`. It also automatically wires up `<a />` elements up to the router. | ||
#### [See a Real-world Example :arrow_right:](http://jsfiddle.net/developit/qc73v9va/) | ||
@@ -12,0 +13,0 @@ --- |
@@ -8,3 +8,11 @@ import { h, Component } from 'preact'; | ||
// hangs off all elements created by preact | ||
const ATTR_KEY = typeof Symbol!=='undefined' ? Symbol.for('preactattr') : '__preactattr_'; | ||
function isPreactElement(node) { | ||
return ATTR_KEY in node; | ||
} | ||
function route(url, replace=false) { | ||
@@ -81,3 +89,3 @@ if (typeof url!=='string' && url.url) { | ||
do { | ||
if (String(t.nodeName).toUpperCase()==='A' && t.getAttribute('href')) { | ||
if (String(t.nodeName).toUpperCase()==='A' && t.getAttribute('href') && isPreactElement(t)) { | ||
// if link is handled by the router, prevent browser defaults | ||
@@ -84,0 +92,0 @@ if (routeFromLink(t)) { |
@@ -82,2 +82,18 @@ import { Router, Link, route } from 'src'; | ||
}); | ||
it('should not intercept non-preact elements', () => { | ||
let onChange = sinon.spy(); | ||
mount( | ||
<div> | ||
<div dangerouslySetInnerHTML={{ __html: `<a href="#foo">foo</a>` }} /> | ||
<Router onChange={onChange}> | ||
<div default /> | ||
</Router> | ||
</div> | ||
); | ||
onChange.reset(); | ||
$('a').click(); | ||
expect(onChange).not.to.have.been.called; | ||
expect(location.href).to.contain('#foo'); | ||
}); | ||
}); | ||
@@ -84,0 +100,0 @@ |
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
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
54854
17430
895
63
38