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

htmx event listeners set on elements inside shadow DOM return right away #718

Closed
gotcha opened this issue Dec 15, 2021 · 2 comments
Closed

Comments

@gotcha
Copy link
Contributor

gotcha commented Dec 15, 2021

var eventListener = function (evt) {
    if (!bodyContains(elt)) {
        eltToListenOn.removeEventListener(triggerSpec.trigger, eventListener);
        return;
    }

If I understand properly the code above, this checks if a node is part of body to avoid to run event listener if it is not the case anymore.

However, if elements from inside a shadow DOM are processed with code like

htmx.process(elt.shadowRoot)

bodyContains(elt) at the begin of eventListener always returns false because elt is part of shadow DOM.

I propose the following fix to take care of shadow DOM nodes.

function bodyContains(elt) {
    if (elt.getRootNode() instanceof ShadowRoot) {
        return getDocument().body.contains(elt.getRootNode().host);
    } else {
        return getDocument().body.contains(elt);
    }
}

Opinions ?

@1cg
Copy link
Contributor

1cg commented Jan 16, 2022

Thank you for the change. Can you write a test that exercises this functionality?

@1cg 1cg closed this as completed Jan 16, 2022
@gotcha
Copy link
Contributor Author

gotcha commented Jan 16, 2022

@cg Tests are why I started the CI PR. I'll do my best to add one.

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

No branches or pull requests

2 participants