mithril.js icon indicating copy to clipboard operation
mithril.js copied to clipboard

Leftover DOM elements when switching between fragment and element

Open 17dec opened this issue 1 year ago • 3 comments

Mithril.js version: v2.0.4-156-gad9aa97d

Browser and OS: Firefox on Linux.

Project:

Code

var toggle = true;
m.mount(document.body, {
    view: function() {
        return m('div', { onclick: () => toggle = !toggle },
          toggle ? 'text' : [ 'fragment', 'b' ],
          m('br') // extra element is needed to trigger the issue
         )
    }
})

Flems

Steps to Reproduce

  1. Load above code
  2. Click a few times on the <div>
  3. Observe that the 'b' element from the fragment remains in the DOM after switching back to the non-fragment version.

17dec avatar May 16 '23 08:05 17dec

hi it's because of the
.

I find the funny approach of putting a
after a simple text hahaha.

the problem in this example is that each piece of text is considered as a pseudo element node

in the first you have "text" then
in the second you have "fragment" then "b" then
when it switches for the first time it removes the first node and displays the two nodes "fragment" then "b" then when you switch for the second time it removes the first node but it leave the second because for him we replace only by a node (the first) and therefore the "b" remains.

to avoid this problem, you must surround your text with a

or other tag or you can also delete the
tag in this case it will replace all the text inside the parent

j314h avatar May 16 '23 19:05 j314h

This is indeed a bug, thanks for the report. I'll look into this.

pygy avatar Jun 15 '23 22:06 pygy

working properl. image clicked! image Second click! image

sonkwl avatar Jul 28 '23 07:07 sonkwl