From 4f0eaa76c0fa14ff39c79daeb5f9dd7fa45e4885 Mon Sep 17 00:00:00 2001 From: Matt Andrews Date: Wed, 5 Nov 2014 01:06:32 +0000 Subject: [PATCH 1/2] Treat high level things as the same --- lib/delegate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/delegate.js b/lib/delegate.js index df37477..9485e62 100644 --- a/lib/delegate.js +++ b/lib/delegate.js @@ -397,7 +397,7 @@ function matchesTag(tagName, element) { */ function matchesRoot(selector, element) { /*jshint validthis:true*/ - if (this.rootElement === window) return element === document; + if (this.rootElement === window) return element === document || element === document.body.parentElement; return this.rootElement === element; } From 585cb913a5e2454e7671eaf50f2c7a45b1a589ea Mon Sep 17 00:00:00 2001 From: Larry Davis Date: Thu, 6 Nov 2014 08:33:50 -0800 Subject: [PATCH 2/2] Adjust matchesRoot to support events dispateched on the window itself, fixes #57 --- lib/delegate.js | 11 ++++++- test/tests/delegateTest.js | 62 +++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/lib/delegate.js b/lib/delegate.js index 9485e62..550d4d4 100644 --- a/lib/delegate.js +++ b/lib/delegate.js @@ -397,7 +397,16 @@ function matchesTag(tagName, element) { */ function matchesRoot(selector, element) { /*jshint validthis:true*/ - if (this.rootElement === window) return element === document || element === document.body.parentElement; + if (this.rootElement === window) { + return ( + // Match the outer document (dispatched from document) + element === document || + // The element (dispatched from document.body or document.documentElement) + element === document.documentElement || + // Or the window itself (dispatched from window) + element === window + ); + } return this.rootElement === element; } diff --git a/test/tests/delegateTest.js b/test/tests/delegateTest.js index 6a7ed14..27c4c38 100644 --- a/test/tests/delegateTest.js +++ b/test/tests/delegateTest.js @@ -1,6 +1,9 @@ /*jshint laxbreak:true*/ -/*global buster, assert, refute, Delegate*/ +/*global buster, Delegate*/ + +var assert = buster.referee.assert; +var refute = buster.referee.refute; var setupHelper = {}; @@ -593,6 +596,63 @@ buster.testCase('Delegate', { assert.calledOnce(captureSpy); assert.calledOnce(bubbleSpy); }, + 'Delegate instances on window catch events when bubbled from the body' : function() { + var delegate, spy; + + delegate = new Delegate(window); + spy = this.spy(); + + delegate.on('click', spy); + + setupHelper.fireMouseEvent(document.body, 'click'); + + assert.calledOnce(spy); + + delegate.off(); + }, + 'Delegate instances on window catch events when bubbled from the document' : function() { + var delegate, spy; + + delegate = new Delegate(window); + spy = this.spy(); + + delegate.on('click', spy); + + setupHelper.fireMouseEvent(document, 'click'); + + // assert.calledOnce(spy); // "Invalid Argument" in IE 9 + assert(spy.callCount === 1); // Workaround for Buster.js IE 9 error + + delegate.off(); + }, + 'Delegate instances on window catch events when bubbled from the element' : function() { + var delegate, spy; + + delegate = new Delegate(window); + spy = this.spy(); + + delegate.on('click', spy); + + setupHelper.fireMouseEvent(document.documentElement, 'click'); + + assert.calledOnce(spy); + + delegate.off(); + }, + 'Delegate instances on window cause events when dispatched directly on window' : function() { + var delegate, spy; + + delegate = new Delegate(window); + spy = this.spy(); + + delegate.on('click', spy); + + setupHelper.fireMouseEvent(window, 'click'); + + assert.calledOnce(spy); + + delegate.off(); + }, 'tearDown': function() { setupHelper.tearDown();