[Pkg-javascript-commits] [dojo] 16/28: Improve focusin detection support

David Prévot taffit at moszumanska.debian.org
Thu Aug 21 17:39:47 UTC 2014


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

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

commit c40eec64afdab97a34d22f5a7f28605e4028e9d9
Author: Colin Snover <github.com at zetafleet.com>
Date:   Wed Jan 1 16:42:05 2014 -0500

    Improve focusin detection support
    
    This change prevents unintentional scrolling and unfocusing of
    whatever element was in focus before the feature detect in browsers
    that need to do the long feature detect.
    
    Adds test case.
    
    Refs #17599
    
    (cherry picked from commit 2536e5cd0935cc7356d584bab6ac2c73d3ad6164)
    
    Conflicts:
    	tests/on/on.js
    
    (cherry picked from commit f774568707ea95b9a56bf646b2ce46e1c57907f8)
    
    Conflicts:
    	tests/on/on.js
---
 on.js                       |  6 ++--
 tests/on/event-focusin.html | 46 ++++++++++++++++++++++++++
 tests/on/on.js              | 79 ++++++++++++++++++++++++++-------------------
 3 files changed, 95 insertions(+), 36 deletions(-)

diff --git a/on.js b/on.js
index e8a4a8a..71e8f70 100644
--- a/on.js
+++ b/on.js
@@ -15,14 +15,14 @@ define(["./has!dom-addeventlistener?:./aspect", "./_base/kernel", "./has"], func
 
 				try {
 					var element = doc.createElement('input'),
-						style = element.style;
-					style.position = 'absolute';
-					style.top = element.style.left = '0';
+						activeElement = doc.activeElement;
+					element.style.position = 'fixed';
 					element.addEventListener('focusin', testFocus, false);
 					doc.body.appendChild(element);
 					element.focus();
 					doc.body.removeChild(element);
 					element.removeEventListener('focusin', testFocus, false);
+					activeElement.focus();
 				} catch (e) {}
 
 				return hasFocusInEvent;
diff --git a/tests/on/event-focusin.html b/tests/on/event-focusin.html
new file mode 100644
index 0000000..44cc298
--- /dev/null
+++ b/tests/on/event-focusin.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<html>
+	<head>
+		<title>dojo/on focusin feature detection</title>
+	</head>
+	<body>
+		<script src="../../dojo.js" data-dojo-config="async:1"></script>
+		<div style="padding: 4000px 0 0 4000px;"><input id="a"></div>
+		<script>
+require([ "doh" ], function(doh){
+	doh.register(function focusInTest(t){
+		function getScrollPosition() {
+			var docElement = document.documentElement,
+				body = document.body;
+
+			return {
+				x: docElement.scrollLeft || body.scrollLeft,
+				y: docElement.scrollTop || body.scrollTop
+			};
+		}
+
+		var dfd = new doh.Deferred(),
+			input = document.getElementById("a");
+
+		input.focus();
+
+		var lastScroll = getScrollPosition();
+
+		t.t(lastScroll.x > 0 && lastScroll.y > 0, "Focus on element should scroll viewport");
+
+		require([ "dojo/has", "dojo/on" ], dfd.getTestCallback(function(has){
+			var newScroll = getScrollPosition();
+			t.is("boolean", typeof has("event-focusin"), "focusin feature detection should have executed");
+			t.is(lastScroll.x, newScroll.x, "Horizontal scroll should not have changed");
+			t.is(lastScroll.y, newScroll.y, "Vertical scroll should not have changed");
+			t.is(input, document.activeElement, "Focus should still be set on the originally focused input");
+		}));
+
+		return dfd;
+	});
+
+	doh.run();
+});
+		</script>
+	</body>
+</html>
diff --git a/tests/on/on.js b/tests/on/on.js
index e1dec89..c5aeecd 100644
--- a/tests/on/on.js
+++ b/tests/on/on.js
@@ -1,14 +1,12 @@
-dojo.provide("dojo.tests.on");
+define([
+	"doh", "require",
+	"dojo/_base/declare",  "dojo/Evented", "dojo/has", "dojo/on", "dojo/query", "dojo/topic"
+], function(doh, require, declare, Evented, has, on, query, topic){
 
-var on = dojo.require("dojo.on");
-var has = dojo.require("dojo.has");
-var topic = dojo.require("dojo.topic");
-var Evented = dojo.require("dojo.Evented");
-doh.register("tests.on",
-	[
+	doh.register("tests.on", [
 		function object(t){
 			var order = [];
-			var obj = new dojo.Evented();
+			var obj = new Evented();
 			obj.oncustom = function(event){
 				order.push(event.a);
 				return event.a+1;
@@ -48,7 +46,7 @@ doh.register("tests.on",
 		},
 		function once(t){
 			var order = [];
-			var obj = new dojo.Evented();
+			var obj = new Evented();
 			obj.on("custom", function(event){
 				order.push(event.a);
 			});
@@ -120,31 +118,42 @@ doh.register("tests.on",
 				bubbles: true,
 				cancelable: true
 			}));
-			var button = div.appendChild(document.createElement("button"));
-			// make sure we are propagating natively created events too
-			signal = on(div, "click", function(event){
-				order.push(7);
+
+			// make sure we are propagating natively created events too, and that defaultPrevented works
+			var button = span.appendChild(document.createElement("button")),
+				defaultPrevented = false,
+				signal2Fired = false;
+			signal = on(span, "click", function(event){
 				event.preventDefault();
-				t.t(event.defaultPrevented);
+			});
+			signal2 = on(div, "click", function(event){
+				order.push(7);
+				signal2Fired = true;
+				defaultPrevented = event.defaultPrevented;
 			});
 			button.click();
+			t.t(signal2Fired, "bubbled click event on div");
+			t.t(defaultPrevented, "defaultPrevented for click event");
 			signal.remove();
 			signal2.remove();
 
-			// make sure 'document' and 'window' can also emit events
-			var eventEmitted;
-			var globalObjects = [document, window];
-			for(var i = 0, len = globalObjects.length; i < len; i++) {
-				eventEmitted = false;
-				on(globalObjects[i], 'custom-test-event', function () {
-					eventEmitted = true;
-				});
-				on.emit(globalObjects[i], 'custom-test-event', {});
-				t.is(true, eventEmitted);
-			}
+			// make sure that evt.defaultPrevented gets set for synthetic events too
+			signal = on(span, "click", function(event){
+				event.preventDefault();
+			});
+			signal2 = on(div, "click", function(event){
+				signal2Fired = true;
+				defaultPrevented = event.defaultPrevented;
+			});
+			signal2Fired = false;
+			on.emit(button, "click", {bubbles: true, cancelable: true});
+			t.t(signal2Fired, "bubbled synthetic event on div");
+			t.t(defaultPrevented, "defaultPrevented set for synthetic event on div");
+			signal.remove();
+			signal2.remove();
 
 			// test out event delegation
-			if(dojo.query){
+			if(query){
 				// if dojo.query is loaded, test event delegation
 				on(div, "button:click", function(){
 					order.push(8);
@@ -164,8 +173,8 @@ doh.register("tests.on",
 			t.is(order, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
 			on(span, "propertychange", function(){}); // make sure it doesn't throw an error
 		},
-/*
- This only works if the test page has the focus, so you can enable if you want to test focus functionality and allow the test page to have focus  
+		/*
+		 This only works if the test page has the focus, so you can enable if you want to test focus functionality and allow the test page to have focus
  		function focus(t){
 			var div = document.body.appendChild(document.createElement("div"));
 			var input = div.appendChild(document.createElement("input"));
@@ -194,10 +203,10 @@ doh.register("tests.on",
 			var customEvent = function(target, listener){
 				return on(target, "custom", listener);
 			};
-			var signal = on(div, customEvent, function(event){
+			on(div, customEvent, function(event){
 				order.push(event.a);
 			});
-			var signal = on(div, on.selector("span", customEvent), function(event){
+			on(div, on.selector("span", customEvent), function(event){
 				order.push(+this.getAttribute("foo"));
 			});
 			on.emit(div, "custom", {
@@ -218,7 +227,7 @@ doh.register("tests.on",
 			t.is(order, [0, 1, 2, 3]);
 		},
 		function testEvented(t){
-			var MyClass = dojo.declare([Evented],{
+			var MyClass = declare([Evented],{
 
 			});
 			var order = [];
@@ -276,5 +285,9 @@ doh.register("tests.on",
 			button.click();
 			t.is(testValue, 3);
 		}
-	]
-);
+	]);
+
+	if(has("host-browser")){
+		doh.registerUrl("tests.on.event-focusin", require.toUrl("./event-focusin.html"), 30000);
+	}
+});

-- 
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