From 9b499301a60602b3800f173734c05821d45756e6 Mon Sep 17 00:00:00 2001 From: Bill Keese Date: Fri, 14 Feb 2020 08:50:28 +0900 Subject: [PATCH] Fix initialization when dpointer loaded before document.body exists. Remove vestigial code for "-ms-touch-action" (IE10 is no longer supported). Fix harmless (but useless) double-creation of CSS rule, refs #30. Fix has("touch-device") to be true on iPad with iOS 13. Note though that iOS and Mac now finally support pointer events natively, so this library has mostly outlived its usefulness. --- README.md | 2 +- events.js | 16 +++------------- handlers/features.js | 13 +++++++++---- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2384e9f..32862bc 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This project proposes a unified and consistent javascript events API which aims This API is a shim of the [W3C Pointer Events specification][W3C_pointer] and adds features that are out of scope of the current specification. - Generates **Pointer Events** according to the current specification. -- Use attribute `touch-action` to set touch action on HTML elements; generates `touch-action` and `ms-touch-action` CSS properties when supported by the browser. +- Use attribute `touch-action` to set touch action on HTML elements; generates `touch-action` CSS properties when supported by the browser. - Support **Pointer Capture** with mouse and touch events. - Normalize **click** (Tap) events, **double click** (double Tap) events, and event **button/buttons/which** values. - Supports immediate clicks (no ~300ms delay) diff --git a/events.js b/events.js index f3283bd..8391c53 100644 --- a/events.js +++ b/events.js @@ -117,22 +117,12 @@ define([ document.head.insertBefore(styleElement, document.head.firstChild); } - // CSS rule when user agent implements W3C Pointer Events or when a polyfill is in place. - if (has("pointer-events")) { + // CSS rule when user agent implements W3C Pointer Events or when a polyfill is in place, + // or user agent has native support for touch-action. + if (has("pointer-events") || has("css-touch-action")) { insertTouchActionCSSRule("touch-action"); } - // CSS rule to map CSS attribute in case user agent has native support for touch-action or -ms-touch-action - // CSS property. - if (has("css-touch-action")) { - insertTouchActionCSSRule("touch-action"); - } else { - // CSS rule for IE10 and IE11 preview - if (has("css-ms-touch-action")) { - insertTouchActionCSSRule("-ms-touch-action"); - } - } - // start listening to native events pointerEvents.enable(); diff --git a/handlers/features.js b/handlers/features.js index d9f70a6..152faa4 100644 --- a/handlers/features.js +++ b/handlers/features.js @@ -1,5 +1,5 @@ /** - * + * Feature detection tests. */ define([ "requirejs-dplugins/has" @@ -7,9 +7,14 @@ define([ if (typeof document !== "undefined") { has.add("touch-events", "ontouchstart" in document); // UA supports Touch Events has.add("pointer-events", "onpointerdown" in document); // UA supports Pointer Events - has.add("touch-device", /(mobile)|(android)/i.test(navigator.userAgent)); // mobile device - has.add("css-touch-action", "touchAction" in document.body.style);// touch-action CSS - has.add("css-ms-touch-action", "msTouchAction" in document.body.style);// -ms-touch-action CSS + + // Mobile device + // Special test for iOS 13.1+, to counteract misleading userAgent string. + var ios13 = /Safari/.test(navigator.userAgent) && "ontouchstart" in document; + has.add("touch-device", /(mobile)|(android)/i.test(navigator.userAgent) || ios13); // mobile device + + has.add("css-touch-action", "touchAction" in document.createElement("div").style); } + return has; });