From cb9fbd77899154d8e8a3572dee955a0971873b74 Mon Sep 17 00:00:00 2001 From: sasensi Date: Tue, 2 Oct 2018 10:29:39 +0200 Subject: [PATCH 1/3] Fix #1501 document touchstart/touchmove events are passive by default in chrome and event.preventDefault() calls are ignored --- src/dom/DomEvent.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/dom/DomEvent.js b/src/dom/DomEvent.js index 568dc130..69f4c6c0 100644 --- a/src/dom/DomEvent.js +++ b/src/dom/DomEvent.js @@ -23,8 +23,17 @@ var DomEvent = /** @lends DomEvent */{ for (var type in events) { var func = events[type], parts = type.split(/[\s,]+/g); - for (var i = 0, l = parts.length; i < l; i++) - el.addEventListener(parts[i], func, false); + for (var i = 0, l = parts.length; i < l; i++) { + var eventName = parts[i]; + // For touchstart/touchmove events on document, we need to explicitely + // declare that event is not passive (can be prevented). + // Otherwise chrome browser would ignore event.preventDefault() calls. + // See #1501 and https://www.chromestatus.com/features/5093566007214080 + var options = el === document && (eventName === 'touchstart'|| eventName === 'touchmove') + ? {passive: false} + : false; + el.addEventListener(eventName, func, options); + } } } }, From 54e1a0c1efbe05ea0c3b5437867853976d074530 Mon Sep 17 00:00:00 2001 From: sasensi Date: Tue, 2 Oct 2018 10:31:49 +0200 Subject: [PATCH 2/3] Fix: calling event.preventDefault() on not cancelable events produces errors --- src/view/View.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/view/View.js b/src/view/View.js index 39e896da..71368ee6 100644 --- a/src/view/View.js +++ b/src/view/View.js @@ -1440,7 +1440,11 @@ new function() { // Injection scope for event handling on the browser // which can call `preventDefault()` explicitly or return `false`. // - If this is a unhandled mousedown event, but the view or tools // respond to mouseup. - if (called && !mouse.move || mouse.down && responds('mouseup')) + // + // Some events are not cancelable anyway (like during a scroll inertia + // on mobile) so trying to prevent default in those case would result + // in no effect and an error. + if (event.cancelable !== false && (called && !mouse.move || mouse.down && responds('mouseup'))) event.preventDefault(); }, From c034ebf554e374af26f396cf8b8a08b8901f576b Mon Sep 17 00:00:00 2001 From: sasensi Date: Tue, 2 Oct 2018 19:20:12 +0200 Subject: [PATCH 3/3] Refactor code to fit style rules --- src/dom/DomEvent.js | 20 +++++++++++--------- src/view/View.js | 12 ++++++++---- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/dom/DomEvent.js b/src/dom/DomEvent.js index 69f4c6c0..dff9e5b8 100644 --- a/src/dom/DomEvent.js +++ b/src/dom/DomEvent.js @@ -24,15 +24,17 @@ var DomEvent = /** @lends DomEvent */{ var func = events[type], parts = type.split(/[\s,]+/g); for (var i = 0, l = parts.length; i < l; i++) { - var eventName = parts[i]; - // For touchstart/touchmove events on document, we need to explicitely - // declare that event is not passive (can be prevented). - // Otherwise chrome browser would ignore event.preventDefault() calls. - // See #1501 and https://www.chromestatus.com/features/5093566007214080 - var options = el === document && (eventName === 'touchstart'|| eventName === 'touchmove') - ? {passive: false} - : false; - el.addEventListener(eventName, func, options); + var name = parts[i]; + // For touchstart/touchmove events on document, we need to + // explicitely declare that event is not passive (can be + // prevented). Otherwise chrome browser would ignore + // event.preventDefault() calls. See #1501 and + // https://www.chromestatus.com/features/5093566007214080 + var options = ( + el === document + && (name === 'touchstart' || name === 'touchmove') + ) ? { passive: false } : false; + el.addEventListener(name, func, options); } } } diff --git a/src/view/View.js b/src/view/View.js index 71368ee6..e7edb3eb 100644 --- a/src/view/View.js +++ b/src/view/View.js @@ -1441,11 +1441,15 @@ new function() { // Injection scope for event handling on the browser // - If this is a unhandled mousedown event, but the view or tools // respond to mouseup. // - // Some events are not cancelable anyway (like during a scroll inertia - // on mobile) so trying to prevent default in those case would result - // in no effect and an error. - if (event.cancelable !== false && (called && !mouse.move || mouse.down && responds('mouseup'))) + // Some events are not cancelable anyway (like during a scroll + // inertia on mobile) so trying to prevent default in those case + // would result in no effect and an error. + if ( + event.cancelable !== false + && (called && !mouse.move || mouse.down && responds('mouseup')) + ) { event.preventDefault(); + } }, /**