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

Template contents are not replaced on merge #15

Open
caleb opened this issue Oct 30, 2023 · 5 comments · May be fixed by #49
Open

Template contents are not replaced on merge #15

caleb opened this issue Oct 30, 2023 · 5 comments · May be fixed by #49

Comments

@caleb
Copy link

caleb commented Oct 30, 2023

I'm trying to merge two nodes that contain a <template> and after the merge the template contains the contents from the old node.

Does it make sense to just replace template node's contents with the new version?

Here's a sandbox showing the issue:

https://codesandbox.io/s/immutable-tdd-mk7mtk?file=/src/index.mjs

Click the button and then use dev tools to inspect the DOM of the result panel.

@caleb caleb changed the title Template contents not replaced on merge Template contents are not replaced on merge Oct 30, 2023
@caleb
Copy link
Author

caleb commented Nov 1, 2023

For anybody who runs into this, I am using this callback and it seems to fix the problem for me:

Idiomorph.morph(e, this.templateContent, {
    callbacks: {
      afterNodeMorphed: (oldEl, newEl) => {
        if (oldEl instanceof HTMLTemplateElement && newEl instanceof HTMLTemplateElement) {
          oldEl.content.replaceChildren(...newEl.content.children);
        }
      }
    }
})

@1cg
Copy link
Contributor

1cg commented Dec 15, 2023

hey caleb, would you mind contributing a test for this so I can see what you think should work?

@caleb
Copy link
Author

caleb commented Dec 19, 2023

Sure thing, let me come up with a test.

@davegaeddert
Copy link

davegaeddert commented Feb 15, 2024

I'm running into a similar question with <template> tags on the page. Unfortunately I'm having trouble getting the tests to run... I think this is a really basic test for it? I'll keep trying but @1cg if you know of any tricks to running the tests, let me know!

it('can morph a template tag properly', function()
{
    let initial = parseHTML("<div><template>Foo</template></div>");
    let finalSrc = '<div><template>Bar</template></div>';
    let final = parseHTML(finalSrc);
    Idiomorph.morph(initial.content, final.content);
    initial.content.outerHTML.should.equal(finalSrc);
});

edit: This is what I get when I run try to run the tests:

Promise Rejection:  Error: connect ECONNREFUSED ::1:58371
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 58371
}

seanpdoyle added a commit to seanpdoyle/idiomorph that referenced this issue Apr 12, 2024
Closes [bigskysoftware#15][]

When morphing `<template>` elements, treat the [DocumentFragment][]
instances returned from the [HTMLTemplateElement.content][] properties
as `morphChildren` methods' `newParent` and `oldParent` variables.

That way, descendant nodes can be iterated across the
connected-disconnected boundary.

[bigskysoftware#15]: bigskysoftware#15
[DocumentFragment]:
[HTMLTemplateElement.content]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement/content
seanpdoyle added a commit to seanpdoyle/idiomorph that referenced this issue Apr 12, 2024
Closes [bigskysoftware#15][]

When morphing `<template>` elements, treat the [DocumentFragment][]
instances returned from the [HTMLTemplateElement.content][] properties
as `morphChildren` methods' `newParent` and `oldParent` variables.

That way, descendant nodes can be iterated across the
connected-disconnected boundary.

[bigskysoftware#15]: bigskysoftware#15
[DocumentFragment]:
[HTMLTemplateElement.content]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement/content
@seanpdoyle seanpdoyle linked a pull request Apr 12, 2024 that will close this issue
seanpdoyle added a commit to seanpdoyle/idiomorph that referenced this issue Apr 12, 2024
Closes [bigskysoftware#15][]

When morphing `<template>` elements, treat the [DocumentFragment][]
instances returned from the [HTMLTemplateElement.content][] properties
as `morphChildren` methods' `newParent` and `oldParent` variables.

That way, descendant nodes can be iterated across the
connected-disconnected boundary.

[bigskysoftware#15]: bigskysoftware#15
[DocumentFragment]: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
[HTMLTemplateElement.content]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement/content
@seanpdoyle
Copy link
Contributor

I've opened #49 to try and resolve this issue.

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 a pull request may close this issue.

4 participants