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

Fix: Fire events on elements with mousetrap class in Shadow Dom #487

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 8 additions & 9 deletions mousetrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,15 +973,6 @@
Mousetrap.prototype.stopCallback = function(e, element) {
var self = this;

// if the element has the class "mousetrap" then no need to stop
if ((' ' + element.className + ' ').indexOf(' mousetrap ') > -1) {
return false;
}

if (_belongsTo(element, self.target)) {
return false;
}

// Events originating from a shadow DOM are re-targetted and `e.target` is the shadow host,
// not the initial event target in the shadow tree. Note that not all events cross the
// shadow boundary.
Expand All @@ -995,7 +986,15 @@
element = initialEventTarget;
}
}

// if the element has the class "mousetrap" then no need to stop
if ((' ' + element.className + ' ').indexOf(' mousetrap ') > -1) {
return false;
}

if (_belongsTo(element, self.target)) {
return false;
}
// stop for input, select, and textarea
return element.tagName == 'INPUT' || element.tagName == 'SELECT' || element.tagName == 'TEXTAREA' || element.isContentEditable;
};
Expand Down
22 changes: 22 additions & 0 deletions tests/test.mousetrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ describe('Mousetrap.bind', function () {
expect(spy.callCount).to.equal(0, 'callback should not have fired');
});

it("z key does fire when inside an input element with mousetrap class in an open shadow dom", function () {
var spy = sinon.spy();
var shadowHost = document.createElement("div");
var shadowRoot = shadowHost.attachShadow({ mode: "open" });
document.body.appendChild(shadowHost);

var inputElement = document.createElement("input");
inputElement.className = 'mousetrap';
shadowRoot.appendChild(inputElement);
expect(shadowHost.shadowRoot).to.equal(
shadowRoot,
"shadow root accessible"
);

Mousetrap.bind("z", spy);
KeyEvent.simulate("Z".charCodeAt(0), 90, [], inputElement, 1, {
shadowHost: shadowHost,
});
document.body.removeChild(shadowHost);
expect(spy.callCount).to.equal(1, "callback should have fired once");
});

it('z key does fire when inside an input element in a closed shadow dom', function() {
var spy = sinon.spy();

Expand Down