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

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

wants to merge 4 commits into from

Conversation

robinsk
Copy link

@robinsk robinsk commented Mar 28, 2015

Events occuring in a shadow DOM are retargeted to look like they come from the host element. Use event.path rather than event.target to get the actual element in which the event occured.

This pull request fixes #245.

robinsk added 3 commits March 28, 2015 23:01
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
@@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the typeof check and constructor check can you please update to use ===

@ccampbell
Copy link
Owner

Also I just checked and the second test now no longer runs via phantom js

TypeError: 'undefined' is not a function (evaluating 'shadow.createShadowRoot()')

Is this just a matter of updating mocha/phantomjs or is this a known thing?

@robinsk
Copy link
Author

robinsk commented Mar 30, 2015

That's a bit weird. I was sure I had that test running fine in both browser and phantom.

I know the polymer guys use mocha for testing, so I guess it'll work if just the test libs are upgraded. I'll look into it.

@vitorgalvao
Copy link

What currently is preventing this from being fixed?

@kleinfreund
Copy link
Contributor

kleinfreund commented Nov 26, 2018

It would be amazing if this could be either merged or the underlying issue fixed.

I tried monkey-patching mousetrap.js by adding the following just before the final return statement in Mousetrap.prototype.stopCallback:

// Events originating from a shadow DOM are re-targetted; thus, `e.target` is not always
// the initial event source. If the first element in the event’s composed path is not
// the target, it has a different initial event source than `element`.
if ('composedPath' in e && typeof e.composedPath === 'function') {
    var initialEventTarget = e.composedPath()[0];
    if (initialEventTarget !== e.target) {
        element = initialEventTarget;
    }
}

This seems to work. Should I submit a pull request?

@kleinfreund
Copy link
Contributor

Is this just a matter of updating mocha/phantomjs or is this a known thing?

@ccampbell I’m trying to write a test for my solution posted above and I get same error:

TypeError: 'undefined' is not a function (evaluating 'shadowHost.attachShadow({ mode: 'open' })')

Mocha version 1.9.0 is used by these tests. That version is very outdated and doesn’t know about Element.prototype.attachShadow (or the createShadowRoot() method from above). My test is passing when opening the tests/mousetrap.html file.

@kleinfreund
Copy link
Contributor

This issue was addressed in #445 and can be closed.

@ccampbell ccampbell closed this Mar 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stop events occuring in input fields when using a shadow DOM
4 participants