Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
Adjust matchesRoot to support events dispateched on the window itself,
Browse files Browse the repository at this point in the history
…fixes #57
  • Loading branch information
lazd committed Nov 6, 2014
1 parent 4f0eaa7 commit 585cb91
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <html> element (dispatched from document.body or document.documentElement)
element === document.documentElement ||
// Or the window itself (dispatched from window)
element === window
);
}
return this.rootElement === element;
}

Expand Down
62 changes: 61 additions & 1 deletion test/tests/delegateTest.js
Original file line number Diff line number Diff line change
@@ -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 = {};

Expand Down Expand Up @@ -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 <html> 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();
Expand Down

0 comments on commit 585cb91

Please sign in to comment.