[Pkg-javascript-commits] [dojo] 02/04: Refs #18168. Rework dojo/on's way to decide about using event delegation. Now uses feature detection, which fixes the event handling broken on Safari iOS 8

David Prévot taffit at moszumanska.debian.org
Tue Dec 9 17:50:38 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to annotated tag 1.7.7
in repository dojo.

commit 7b156f9ca999534709c17a850278184e4bcf153d
Author: Adrian Vasiliu <vasiliu at fr.ibm.com>
Date:   Tue Sep 23 16:44:33 2014 +0200

    Refs #18168. Rework dojo/on's way to decide about using event delegation. Now uses feature detection, which fixes the event handling broken on Safari iOS 8
---
 on.js | 43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)

diff --git a/on.js b/on.js
index 57bff67..a4fa6e4 100644
--- a/on.js
+++ b/on.js
@@ -38,6 +38,30 @@ define(["./has!dom-addeventlistener?:./aspect", "./_base/kernel", "./has"], func
 		has.add("event-focusin", function(global, doc, element){
 			return 'onfocusin' in element;
 		});
+		
+		if(has("touch")){
+			has.add("touch-can-modify-event-delegate", function(){
+				// This feature test checks whether deleting a property of an event delegate works
+				// for a touch-enabled device. If it works, event delegation can be used as fallback
+				// for browsers such as Safari in older iOS where deleting properties of the original
+				// event does not work.
+				var EventDelegate = function(){};
+				EventDelegate.prototype =
+					document.createEvent("MouseEvents"); // original event
+				// Attempt to modify a property of an event delegate and check if
+				// it succeeds. Depending on browsers and on whether dojo/on's
+				// strict mode is stripped in a Dojo build, there are 3 known behaviors:
+				// it may either succeed, or raise an error, or fail to set the property
+				// without raising an error.
+				try{
+					var eventDelegate = new EventDelegate;
+					eventDelegate.target = null;
+					return eventDelegate.target === null;
+				}catch(e){
+					return false; // cannot use event delegation
+				}
+			});
+		}
 	}
 	var on = function(target, type, listener, dontFix){
 		if(typeof target.on == "function" && typeof type != "function" && !target.nodeType){
@@ -411,7 +435,7 @@ define(["./has!dom-addeventlistener?:./aspect", "./_base/kernel", "./has"], func
 		};
 	}
 	if(has("touch")){ 
-		var Event = function (){};
+		var EventDelegate = function (){};
 		var windowOrientation = window.orientation; 
 		var fixTouchListener = function(listener){ 
 			return function(originalEvent){ 
@@ -428,9 +452,20 @@ define(["./has!dom-addeventlistener?:./aspect", "./_base/kernel", "./has"], func
 						delete originalEvent.type; // on some JS engines (android), deleting properties make them mutable
 					}catch(e){} 
 					if(originalEvent.type){
-						// deleting properties doesn't work (older iOS), have to use delegation
-						Event.prototype = originalEvent;
-						var event = new Event;
+						// Deleting the property of the original event did not work (this is the case of
+						// browsers such as older Safari iOS), hence fallback:
+						if(has("touch-can-modify-event-delegate")){
+							// If deleting properties of delegated event works, use event delegation:
+							EventDelegate.prototype = originalEvent;
+							event = new EventDelegate;
+						}else{
+							// Otherwise last fallback: other browsers, such as mobile Firefox, do not like
+							// delegated properties, so we have to copy
+							event = {};
+							for(var name in originalEvent){
+								event[name] = originalEvent[name];
+							}
+						}
 						// have to delegate methods to make them work
 						event.preventDefault = function(){
 							originalEvent.preventDefault();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/dojo.git



More information about the Pkg-javascript-commits mailing list