Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fire events if you're inside a textfield/input in a shadow DOM #254

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions mousetrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@
}

function _belongsTo(element, ancestor) {
if (element === document) {
if (element === document || element === null) {
return false;
}

Expand Down Expand Up @@ -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;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/test.mousetrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,35 @@ 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('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();

Expand Down