From c703b03253992b523187d3d8d911af885683e42c Mon Sep 17 00:00:00 2001 From: Isiah Meadows Date: Tue, 18 Sep 2018 10:14:21 -0400 Subject: [PATCH] Unbreak accidental back-compat break with event optimization (#2222) This was supposed to be purely additive. See here for more details: https://github.com/MithrilJS/mithril.js/pull/1949#issuecomment-417824513 --- docs/change-log.md | 2 +- mithril.js | 9 +++++++- mithril.min.js | 16 ++++++------- render/render.js | 9 +++++++- render/tests/test-event.js | 47 ++++++++++++++++++++++++++++++++++++++ test-utils/domMock.js | 8 +++++++ 6 files changed, 80 insertions(+), 11 deletions(-) diff --git a/docs/change-log.md b/docs/change-log.md index 3836a05a..ae72b5ae 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -31,7 +31,7 @@ #### News - API: Introduction of `m.redraw.sync()` ([#1592](https://github.com/MithrilJS/mithril.js/pull/1592)) -- API: Event handlers may also be objects with `handleEvent` methods ([#1939](https://github.com/MithrilJS/mithril.js/issues/1939)). +- API: Event handlers may also be objects with `handleEvent` methods ([#1949](https://github.com/MithrilJS/mithril.js/pull/1949), [#2222](https://github.com/MithrilJS/mithril.js/pull/2222)). - API: `m.route.link` accepts an optional `options` object ([#1930](https://github.com/MithrilJS/mithril.js/pull/1930)) - API: `m.request` better error message on JSON parse error - ([#2195](https://github.com/MithrilJS/mithril.js/pull/2195), [@codeclown](https://github.com/codeclown)) - API: `m.request` supports `timeout` as attr - ([#1966](https://github.com/MithrilJS/mithril.js/pull/1966)) diff --git a/mithril.js b/mithril.js index 0b46c1ac..aaae3c1b 100644 --- a/mithril.js +++ b/mithril.js @@ -1182,13 +1182,20 @@ var coreRenderer = function($window) { // 4. The event name is remapped to the handler0 before calling it. // 5. In function-based event handlers, `ev.target === this`. We replicate // that below. + // 6. In function-based event handlers, `return false` prevents the default + // action and stops event propagation. We replicate that below. function EventDict() {} EventDict.prototype = Object.create(null) EventDict.prototype.handleEvent = function (ev) { var handler0 = this["on" + ev.type] - if (typeof handler0 === "function") handler0.call(ev.target, ev) + var result + if (typeof handler0 === "function") result = handler0.call(ev.target, ev) else if (typeof handler0.handleEvent === "function") handler0.handleEvent(ev) if (typeof onevent === "function") onevent.call(ev.target, ev) + if (result === false) { + ev.preventDefault() + ev.stopPropagation() + } } //event function updateEvent(vnode, key2, value) { diff --git a/mithril.min.js b/mithril.min.js index 070304ac..1722b606 100644 --- a/mithril.min.js +++ b/mithril.min.js @@ -35,14 +35,14 @@ typeof d)){if("o"===b[0]&&"n"===b[1])return U(a,b,d);if("xlink:"===b.slice(0,6)) null!==c&&a.dom.value===e||"option"===a.tag&&null!==c&&a.dom.value===e))return;"input"===a.tag&&"type"===b?a.dom.setAttribute(b,d):a.dom[b]=d}}}function w(a){return"oninit"===a||"oncreate"===a||"onupdate"===a||"onremove"===a||"onbeforeremove"===a||"onbeforeupdate"===a}function S(a){return"href"===a||"list"===a||"form"===a||"width"===a||"height"===a}function T(a,b,c){if(null!=b&&null!=c&&"object"===typeof b&&"object"===typeof c&&c!==b){for(var d in c)c[d]!==b[d]&&(a.style[d]=c[d]);for(d in b)d in c|| (a.style[d]="")}else if(b===c&&(a.style.cssText="",b=null),null==c)a.style.cssText="";else if("string"===typeof c)a.style.cssText=c;else for(d in"string"===typeof b&&(a.style.cssText=""),c)a.style[d]=c[d]}function M(){}function U(a,b,c){null!=a.events?a.events[b]!==c&&(null==c||"function"!==typeof c&&"object"!==typeof c?(null!=a.events[b]&&a.dom.removeEventListener(b.slice(2),a.events,!1),a.events[b]=void 0):(null==a.events[b]&&a.dom.addEventListener(b.slice(2),a.events,!1),a.events[b]=c)):null== c||"function"!==typeof c&&"object"!==typeof c||(a.events=new M,a.dom.addEventListener(b.slice(2),a.events,!1),a.events[b]=c)}function N(a,b,c){"function"===typeof a.oninit&&e.call(a.oninit,b);"function"===typeof a.oncreate&&c.push(e.bind(a.oncreate,b))}function L(a,b,c){"function"===typeof a.onupdate&&c.push(e.bind(a.onupdate,b))}var D=a.document;D.createDocumentFragment();var H={svg:"http://www.w3.org/2000/svg",math:"http://www.w3.org/1998/Math/MathML"},I,J={caption:"table",thead:"table",tbody:"table", -tfoot:"table",tr:"tbody",th:"tr",td:"tr",colgroup:"table",col:"colgroup"};M.prototype=Object.create(null);M.prototype.handleEvent=function(a){var b=this["on"+a.type];"function"===typeof b?b.call(a.target,a):"function"===typeof b.handleEvent&&b.handleEvent(a);"function"===typeof I&&I.call(a.target,a)};return{render:function(a,b){if(!a)throw Error("Ensure the DOM element being passed to m.route/m.mount/m.render is not undefined.");var c=[],d=D.activeElement,e=a.namespaceURI;null==a.vnodes&&(a.textContent= -"");Array.isArray(b)||(b=[b]);k(a,a.vnodes,z.normalizeChildren(b),c,null,"http://www.w3.org/1999/xhtml"===e?void 0:e);a.vnodes=b;null!=d&&D.activeElement!==d&&"function"===typeof d.focus&&d.focus();for(d=0;d