-
Notifications
You must be signed in to change notification settings - Fork 98
Add test for templateElement.content.childNodes[0].replaceWith #279
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
Conversation
| let template2 = document2.documentElement; | ||
| assert(template2.innerHTML, '<img id="el-14" class="has-svg" src="./favicon.svg" inline="">'); | ||
|
|
||
| template2.content.childNodes[0].replaceWith(document.createElement('br')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you save content here,
const content = template2.content
content.toString() will give the correct content.
template2.content.innerHTML still won't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, live nodes are not a thing in here ... and won't be because that's not a goal of this project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clarify: I don't know if content returns an array of its nodes but if that's the case, that's gonna be the case in here. For everything else, JSDOM got you covered. This project is not a replacement for it, it just enables the read-only things a page would land, everything else really need a good use case for it or it won't happen. Do you want to dig into the DocumentFragment hole without affecting performance? Please do, if that's the case, I'll be watching 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what should I do instead?
the idea is to swap out <a/> with <c/> without affecting anything else.
<template class="container">
<a/>
<b/>
</template>
the original code is like this:
for (const el of document.querySelector('.container').querySelectorAll('a')) {
el.replaceWith(document.createElement('c'))
}the behavior is different depending on if .container is a <template> or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found the solution. The problem is not about live nodes.
template.querySelectorAll works.
template.content.querySelectorAll doesn't. template.content clones each node and puts them in an array. Calling .replaceWith on new nodes doesn't work. Using template.childNodes could work as well.
Anyway, thanks for the trippy trip down the DOM road!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, templte.content does that and it's not a LIVE NodeList so you got your answer ... that won't land in here, it's no purpose whatsoever, so if you solved, remove the failing-but-not-really test and let's merge your changes?
On HTMLTemplateElement.contentThe following snippet from MDN implies that I tried it in browser. It works as expected. deno-dom works as expected.
I don't think it is possible to do that, given the current implementation seems a bit buggy. If I understand currently, templateElement.content;
templateElement.childNodes.append(document.createElement('div'))
templateElement.content; // this would be stale |
|
Not sure what to do with this, actually. browser DOM says if there's a way to direct "adding elements" to the DocumentFragment in the first place... this is complicated. @WebReflection can we do getter setter function proxy here? is it too complicated for linkedom? |
|
It’s not complicated, rather about SSR performance where you never want to compute things more than once. May I ask your use case here? If it’s to test please read the non-goals section in the README. If it’s about something else please share, thanks. P.S. I’m really focusing on other things these days and I won’t be able to help much but if it’s a quick thing I will try to help |
|
The use case. It's about something else. It's already solved. basically, in linkedom, i don't even know if that's the standard DOM usage. maybe it won't work in browser, but that's ok. lume a static site generator. the code only runs in Deno, which has no DOM API on its own. we (plan to) replace another DOM-in-JS library with linkedom to make the SSG go faster.
You can help at any time 👍 . At your own pace. |
added a test. the test should not fail.