Example: https://perchance.org/3o1sbun1w3#edit
Seems like what’s returned at the end of the square brackets is somehow added, instead of setting what replaces the square brackets. Potentially, it’s cleared, then the square brackets are processed, then the result of the square brackets is added?
Thanks for the test. That’s what I guessed was happening, yeah… cleared first, processed, then appended. Most likely none of this will change, as it’s kinda core and changing it could blow things up. But, for science…
I guess first the expectation/spec should be defined of what “should” happen. This is (or was) my expectation:
(There are of course other ways of defining things. I just don’t think it’s actually defined by the dev anywhere, but more cludged together–as is the style of perchance. 😉)
So the tricky part here is, in step 3, what happens if the spot no longer exists by the time the function is processed the first time? My expectation:
If you rewrite the innerHTML/innerText, the old text nodes are disconnected from the DOM. So then you can check if the old ones still have a parent (or the same parent perhaps). If not, don’t do the replace step.
Similarly for an update, you could keep a reference to each top-level element created by the code block. Then in the replace step remove as many of them as are still within that same parent, and add the new stuff in at that point–“replacing” it with the result.
I would also expect that if the entire contents of an element is square brackets, the entire contents would be lost on an update. So that would be an easier edge case to process in any case.
I just updated with a test5, which has a more complex case. The result is pretty wild! 😂 I don’t know how anyone would’ve guessed that is the expected behaviour; seems pretty complex. 🫠