-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 for hx-swab-oob within web components #2846
Conversation
I added type annotations for the rootNode parameter. I also realize now this probably does not correctly handle the special |
I don't know much about web components, but since those are mentioned in the docs, I would say: please do look into that! Also, you'll want to rebase against |
I did in fact look into the global target and realized the implementation was a bit off. The correct rootNode is the As for the host target, I don't think it's currently implemented on the dev branch and I don't want to implement it because I'm not 100% sure what it's supposed to do. I am not sure whether it's supposed to refer to the shadow root node or whether it should be used to search the parent's DOM in a shadow root within shadow root situation. But I think searching htmx.js for |
Yeah quick testing shows host does not currently work so maybe it needs to be removed from the documentation? Also testing of the related 'root' selector that was added with web components support and this seems not be be working or usable any more. This one seems not to be documented but it looks like it may have worked initially but then broke with JSDoc changes in #2336 because getTarget() was wrapped in asElement() and shadow roots are NOT elements! Not sure if a swap targeting the shadow root even made sense when it is not a normal element? When testing with overrides to let the shadow root be the target it just produced errors for me trying to set settling classes on the shadow root when classes not supported here. } else if (selector === 'root') {
return [getRootNode(elt, !!global)]
} else if (selector === 'host') {
return [getRootNode(elt, !!global).host]
} else if (selector.indexOf('global ') === 0) {
return querySelectorAllExt(elt, selector.slice(7), true) Adding these two lines in I think is all that would be needed to support 'host' targeting if this was a useful feature. It seemed to work well targeting host with a outerHTML swap or a beforebegin/afterend but inner swaps do not work it seems with web component hosts. Otherwise documentation should be updated instead to remove reference to host. Note that the workaround to not having 'host' targeting working would be to just add some ID to the web component and then hx-target="global #someID" |
If it's not implemented at all, then I'd say you should indeed not care about it for this PR which is about oob swap, just to keep things tight and ease the reviewer's reading.
Woops, that was me... apologies for that! Help is very very welcome when it comes to web components as we don't use those ourselves, if you feel like diving in and opening PRs to handle these cases/features, it'd be great! We also probably need more web component tests as my breaking-PR revealed 😅 . Again, to ease 1cg's job that reviews all core PRs before merging them, I'd strongly suggest, if you feel like working on those, opening separate PRs as much as you can, to have a minimal amount of code to review at a given time. |
I won't implement the host target for this pull request. As far as I can tell, these commits fulfill the objectives of the pull request. However, if it is preferred to revert changes to the api.js tests and fall back to |
Looking at the code, I see that |
Previous a commit made swapOptions.contextElement a required field. This could have harmful ramifications for extensions and users, so instead, the old behavior of assuming document as a root will be used if the contextElement is not provided.
If not provided, it will fall back to using document as rootNode. jsdocs have been updated for oobSwap and findAndSwapElements accordingly.
Description
I added a rootNode parameter to functions pertaining to oob swaps. This is so that oob swaps can respect shadowRoots.
Testing
I added a test in test/attributes/hx-swap-oob.js. It creates a web component and two divs with the same id, one inside the shadow root and one outside. The response targets the div and asserts that the one in the web component was swapped and the one outside was not. Note that I ignored the linter and used a template literal. There is no way to unregister a web component, so every test involving one must have a unique web component name, and building html with string concatenation is much less readable.
Checklist
master
for website changes,dev
forsource changes)
approved via an issue
npm run test
) and verified that it succeeded