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

Operations with me, my, and I after append on parent element result in silent failure #513

Open
MeemeeLab opened this issue Jan 25, 2024 · 0 comments

Comments

@MeemeeLab
Copy link

Operations using me, my and I after append operations on parent silently fails.

Looks like append operation is using += operation on innerHTML, which makes entire innerHTML parsed again and Element that existed before gets replaced with new Element and causing the problem.

target.innerHTML += value;

Simplified explanation:

  1. _hyperscript holds Element (me)
  2. Parent's innerHTML changes
  3. innerHTML gets parsed once again and replaced with new Element
  4. me points to Element that no longer exists

IMO, For performance reason append should not use target.innerHTML += ... but use Element.append() so that this problem will not happen and browser don't have to parse all elements once again:

let newElementString = '<div>Hello World</div>';

target.innerHTML += newElementString; // This causes performance issue and already existing Elements gets replaced with new Element

let newElement = document.createElement('div');
newElement.outerHTML = newElementString;
target.append(newElement); // This "appends" the element, browser only have to parse new element and this issue solves too

Examples:

Works:

<div id="container">
    <button _="on click remove me">
        Click
    </button>
</div>

Result: Button gets removed after click (expected)

<div id="container">
    <button _="on click set me.style.background to 'red'">
        Click
    </button>
</div>

Result: Button becomes red after click (expected)

Does not work:

<div id="container">
    <button _="on click append '<div>You clicked!</div>' to #container then remove me">
        Click
    </button>
</div>

Expected: Shows "You clicked!" and button disappears
Actual: Shows "You clicked!" but button does not disappear

<div id="container">
    <button _="on click append '<div>You clicked!</div>' to #container then set me.style.background to 'red'">
        Click
    </button>
</div>

Expected: Shows "You clicked!" and button becomes red
Actual: Shows "You clicked!" but button does not become red

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

1 participant