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

Can't handle document fragments #53

Open
mihaiav opened this issue May 11, 2024 · 1 comment
Open

Can't handle document fragments #53

mihaiav opened this issue May 11, 2024 · 1 comment

Comments

@mihaiav
Copy link

mihaiav commented May 11, 2024

I'm trying to morph a document fragment but it seems that for some reasons this fails. I also wrote a test. Am I doing something wrong. The test represents the way my "framework" works(i.e. using document fragments to encapsulate one or more nodes)

  1. Core morphing tests
    can handle document fragments:
    TypeError: Cannot read properties of null (reading 'replaceChild')
    at morphOldNodeTo (file:///Users/x/go/src/github.com/AVML/idiomorph/src/idiomorph.js:135:39)
    at morphNormalizedContent (file:///Users/x/go/src/github.com/AVML/idiomorph/src/idiomorph.js:91:35)
    at Object.morph (file:///Users/x/go/src/github.com/AVML/idiomorph/src/idiomorph.js:53:20)
    at Context. (core.js:465:19)

// test code
#54


   it('can handle document fragments', function()
     {
         let tpl = document.createElement("template");
         tpl.innerHTML = "<div><button>Foo</button><div>";
         let initial = document.adoptNode(tpl.content);
         let initialFirstChild = initial.firstChild;

         tpl = document.createElement("template");
         tpl.innerHTML = "<p>x</p><p>y</p>"
         let replaceWith =  document.adoptNode(tpl.content);
         Idiomorph.morph(initialFirstChild, replaceWith, {});
         initial.outerHTML.should.equal(`<p>x</p><p>y</p>`);
     });

@MichaelWest22
Copy link
Collaborator

  it('can handle document fragments', function()
  {
      let tpl = document.createElement("template");
      tpl.innerHTML = "<div><button>Foo</button><div>";
      let initial = document.adoptNode(tpl.content);
      let initialFirstChild = initial.firstChild;

      tpl = document.createElement("template");
      tpl.innerHTML = "<p>x</p><p>y</p>"
      let replaceWith =  document.adoptNode(tpl.content);
      Idiomorph.morph(initialFirstChild, replaceWith, {});
      let initalOuterHTML = '';
      [].forEach.call(initial.children, function(el) { 
        initalOuterHTML += el.outerHTML;
      });
      initalOuterHTML.should.equal(`<p>x</p><p>y</p>`);
  });

Found your test had an issue where you can't call outerHTML on a fragment which I fixed above.

Testing this on version 0.3.0 and was able to reproduce your problem. But when trying on the current release 0.4.0 it no longer produces the problem and morphs with no errors. however it seems to morph only the first x node into the inital fragment. But when testing on the current release we are about it do it seems to work fine! So hopefully when this releases you should be able to retest.

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