8000 Fix processing of template nodes by trescenzi · Pull Request #10 · alexpetros/triptych · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix processing of template nodes #10

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

8000
Merged
merged 1 commit into from
May 14, 2025

Conversation

trescenzi
Copy link
Contributor

The contents of template elements aren't technically the children of that template element. So template.querySelectorAll('button[action]') would always return an empty nodelist, however template.content.querySelectorAll('button[action]') would return buttons that are part of the template's contents.

I discovered this when working with two buttons like:

<button target="_this" action="/feeds/subscribe/1" method="PUT">Subscribe</button>
<button target="_this" action="/feeds/subscribe/1" method="DELETE">Unsubscribe</button>

Where the respective actions return the opposite button. Only the first click worked.

Looking into it a bit deeper in the spec I found that in fact the content model of the template node is nothing:

It is also possible, as a result of DOM manipulation, for a template element to contain Text nodes and element nodes; however, having any is a violation of the template element's content model, since its content model is defined as nothing.

A little example you could run in a browser console:

const template = document.createElement('template');
template.innerHTML = '<button action="POST" target="_this">Click Me!</button';
console.log(template.querySelectorAll('button[action]'));
console.log(template.content.querySelectorAll('button[action]'));

I added a test and confirmed that it passes with this change and fails without it.

Happy to do this a different way if you think there's a better solution. Just noticed this and wanted to contribute back. Love the proposal and it's working great for a small site that doesn't need everything that htmx brings.

Templates don't have children, instead the `content` is where the actual
dom nodes that they contain are. In order to be able to insert nodes that
have been processed the content needs to be processed not the template node
itself.
@alexpetros
Copy link
Owner

Hey, cool! You are actually the first person to tell me they're using this in production.

You're 100% right, the fix is excellent, and so is the corresponding test. Thanks so much for contributing ☺️

@alexpetros alexpetros merged commit 6383913 into alexpetros:main May 14, 2025
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.

2 participants
0