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

Commit

Permalink
Merge pull request #102 from Financial-Times/issue/57
Browse files Browse the repository at this point in the history
Issue/57
  • Loading branch information
notlee authored Sep 18, 2019
2 parents 40c5bfa + 5bf4608 commit 377e20d
Show file tree
Hide file tree
Showing 2 changed files with 70 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 @@ -410,7 +410,16 @@ function matchesTag(tagName, element) {
*/
function matchesRoot(selector, element) {
/*jshint validthis:true*/
if (this.rootElement === window) return element === document;
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
61 changes: 60 additions & 1 deletion test/tests/delegateTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*jshint laxbreak:true*/

/*global buster, Delegate*/
var assert = buster.referee.assert;
var refute = buster.referee.refute;

var setupHelper = {};

Expand Down Expand Up @@ -169,7 +171,7 @@ buster.testCase('Delegate', {
setupHelper.fireMouseEvent(element, 'mouseover');

assert.calledOnce(spy);

delegate.off();
},
'Class name selectors are supported' : function() {
Expand Down Expand Up @@ -629,6 +631,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();
},

'Custom events are supported': function() {
var delegate = new Delegate(document.body);
Expand Down

0 comments on commit 377e20d

Please sign in to comment.