Replacing a rendered DOMContainer element leaves the previous element attached
Replacing a rendered DOMContainer element leaves the previous element attached
Current behavior
The DOMContainer.element setter documents that assigning a new element replaces the current element. When the container has already rendered, assigning element B and rendering again leaves both element A and element B attached to the DOM.
Destroying the container after replacement removes B but still leaves A orphaned and interactive in the document.
Expected behavior
Replacing DOMContainer.element should detach the previously rendered element. Destroying the container should not leave any element previously owned by it attached.
Public runnable reproduction
- Repository: https://github.com/mf3493297-png/pixijs-bug-reproductions
- Reproduction: https://github.com/mf3493297-png/pixijs-bug-reproductions/tree/main/dom-container-replacement
Run:
npm ci
npm startOpen http://127.0.0.1:4173/dom-container-replacement/ in Firefox. The default case prints:
REPRODUCED: {"mode":"replace-after-render","firstAttachedBefore":true,"firstAttachedAfter":true,"secondAttachedAfter":true,"attachedInputs":["first","second"]}The same page supports these neighboring controls:
?mode=replace-before-render: only B is attached.?mode=remove-after-replace: stage removal cleans both elements throughDOMPipe.?mode=destroy-after-replace: B is removed, but stale A remains attached.
All cases were verified in separate pages in headed Camoufox with no page errors.
Root cause
DOMContainer.element replaces _element immediately:
this._element = value;
this.onViewUpdate();DOMPipe tracks the DOMContainer, not the element previously attached for it. On the next postrender(), it appends the new current element when absent, but it has no reference to the old one to remove:
const element = domContainer.element;
if (!this._domElement.contains(element))
{
this._domElement.appendChild(element);
}DOMContainer.destroy() similarly removes only the current _element, so the previous rendered element survives direct destruction.
Environment
- PixiJS
devcommit:27071aa0096d8749bfdb189857c58178c30cd153 - npm package:
[email protected] - Browser: headed Camoufox / Firefox
- OS: Linux x86_64
Duplicate search
I searched open and closed issues/PRs for DOMContainer element replace, DOMContainer stale element, DOMPipe element setter, and related lifecycle terms. Existing DOMContainer issues and PRs concern scaling, positioning, and configuration; I found no report for stale elements after replacement.
If this fits the bounty program, please consider assigning a bounty label and amount before implementation work begins.
Source: pixijs/pixijs