From 6bc642bbe0eeec930c7c0835448d3d83840f7ece Mon Sep 17 00:00:00 2001 From: Robin Skoglund Date: Sat, 28 Mar 2015 23:01:50 +0100 Subject: [PATCH 1/4] Add test for not firing in textarea --- tests/test.mousetrap.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test.mousetrap.js b/tests/test.mousetrap.js index fdd24f7a..5dbd7915 100644 --- a/tests/test.mousetrap.js +++ b/tests/test.mousetrap.js @@ -70,6 +70,16 @@ describe('Mousetrap.bind', function() { } }); + it('z key does not fire when inside an input element', function() { + var textarea = document.querySelector('textarea'); + var spy = sinon.spy(); + + Mousetrap.bind('z', spy); + KeyEvent.simulate('Z'.charCodeAt(0), 90, [], textarea); + + expect(spy.callCount).to.equal(0, 'callback should not have fired'); + }); + it('keyup events should fire', function() { var spy = sinon.spy(); From 37dd0e00b3e33d8ab4e69025b822513f5b66297e Mon Sep 17 00:00:00 2001 From: Robin Skoglund Date: Sat, 28 Mar 2015 23:05:26 +0100 Subject: [PATCH 2/4] Add test for not firing in input in shadow DOM --- tests/test.mousetrap.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test.mousetrap.js b/tests/test.mousetrap.js index 5dbd7915..1352c28e 100644 --- a/tests/test.mousetrap.js +++ b/tests/test.mousetrap.js @@ -80,6 +80,25 @@ describe('Mousetrap.bind', function() { expect(spy.callCount).to.equal(0, 'callback should not have fired'); }); + it('z key does not fire when inside an input element in a shadow dom', function() { + var shadow = document.createElement('div'); + shadow.style.display = 'none'; + shadow.createShadowRoot(); + document.body.appendChild(shadow); + + var input = document.createElement('input'); + shadow.shadowRoot.appendChild(input); + + var spy = sinon.spy(); + + Mousetrap.bind('z', spy); + KeyEvent.simulate('Z'.charCodeAt(0), 90, [], input); + + document.body.removeChild(shadow); + + expect(spy.callCount).to.equal(0, 'callback should not have fired'); + }); + it('keyup events should fire', function() { var spy = sinon.spy(); From 49e9adf9a918f4dc74b0b20f18853227f6dcc703 Mon Sep 17 00:00:00 2001 From: Robin Skoglund Date: Sat, 28 Mar 2015 23:33:39 +0100 Subject: [PATCH 3/4] Use event path if available to support shadow DOM Use `event.path` (if it's available) to get the target element, so the `stopCallback` function supports events occuring in a shadow DOM. Fixes #245 --- mousetrap.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mousetrap.js b/mousetrap.js index 1baeadcb..1c030f83 100644 --- a/mousetrap.js +++ b/mousetrap.js @@ -410,7 +410,7 @@ } function _belongsTo(element, ancestor) { - if (element === document) { + if (element === document || element === null) { return false; } @@ -598,7 +598,8 @@ function _fireCallback(callback, e, combo, sequence) { // if this event should not happen stop here - if (self.stopCallback(e, e.target || e.srcElement, combo, sequence)) { + var element = typeof e.path == 'object' && e.path.constructor == Array ? e.path[0] : e.target || e.srcElement; + if (self.stopCallback(e, element, combo, sequence)) { return; } From ca9c0857a94e4db5aff3e8e3f56be0eebb3f1687 Mon Sep 17 00:00:00 2001 From: Robin Skoglund Date: Mon, 30 Mar 2015 09:17:59 +0200 Subject: [PATCH 4/4] Use strict equality comparison --- mousetrap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mousetrap.js b/mousetrap.js index 1c030f83..dda9364e 100644 --- a/mousetrap.js +++ b/mousetrap.js @@ -598,7 +598,7 @@ function _fireCallback(callback, e, combo, sequence) { // if this event should not happen stop here - var element = typeof e.path == 'object' && e.path.constructor == Array ? e.path[0] : e.target || e.srcElement; + var element = typeof e.path === 'object' && e.path.constructor === Array ? e.path[0] : e.target || e.srcElement; if (self.stopCallback(e, element, combo, sequence)) { return; }