trix icon indicating copy to clipboard operation
trix copied to clipboard

Internal Toolbar Permanently Vanishes & Creates Zombie `ToolbarController` During Morphing

Open codeCraft-Ritik opened this issue 1 month ago • 0 comments

Location

  • File: src/trix/elements/trix_editor_element.js
  • Lines: 461–473, 593–602

Root Cause

In commit a8b0f73e ("Make Trix compatible with morphing"), morphing support was introduced via attributeChangedCallback watching the connected attribute:

reconnect() {
  this.removeInternalToolbar()
  this.disconnectedCallback()
  this.connectedCallback()
}

removeInternalToolbar() {
  this.internalToolbar?.remove()
  this.internalToolbar = null
}
  1. When <trix-editor> is first initialized without an external toolbar, get toolbarElement() generates a unique toolbar ID (trix-toolbar-1), sets this.setAttribute("toolbar", "trix-toolbar-1"), and inserts the toolbar into the DOM.
  2. When morphing or reconnection occurs, this.removeInternalToolbar() removes this.internalToolbar from the DOM, but fails to remove the toolbar attribute from <trix-editor>.
  3. When get toolbarElement() is subsequently accessed:
    get toolbarElement() {
      if (this.hasAttribute("toolbar")) {
        return this.ownerDocument?.getElementById(this.getAttribute("toolbar"))
      } else if (this.parentNode) {
        // ... recreates toolbar ...
      }
    }
    
    Because this.hasAttribute("toolbar") remains true (pointing to "trix-toolbar-1"), it attempts getElementById("trix-toolbar-1"). Since that node was removed from the DOM, it returns null! The else if branch is never entered, and the toolbar is never recreated.
  4. Even worse, this.editorController already exists (if (!this.editorController) skips creating a new one), so this.editorController.toolbarController continues pointing to the detached, destroyed toolbar DOM node (toolbarController.element.isConnected === false). All formatting buttons vanish, and any active button reflection logic fails silently.

[!NOTE] In src/test/system/morphing_test.js, this bug was masked because the test manually executed element.removeAttribute("toolbar") before triggering reconnect:

element.toolbarElement.remove()
element.removeAttribute("toolbar") // Artificial workaround in test
element.removeAttribute("connected")

In real-world Turbo 8 / Idiomorph page morphs, no client removes the toolbar attribute manually.

Reproduction

const el = document.querySelector("trix-editor")
// Editor is connected with an internal toolbar
console.log(el.toolbarElement) // <trix-toolbar id="trix-toolbar-1">

// Trigger morphing / reconnect
el.removeAttribute("connected")
await new Promise(requestAnimationFrame)

console.log(el.toolbarElement) // null! (Permanently destroyed)
console.log(el.editorController.toolbarController.element.isConnected) // false (Zombie controller)

Proposed Patch

In src/trix/elements/trix_editor_element.js:

  removeInternalToolbar() {
    this.internalToolbar?.remove()
    this.internalToolbar = null
+   if (this.hasAttribute("toolbar") && this.getAttribute("toolbar") === `trix-toolbar-${this.trixId}`) {
+     this.removeAttribute("toolbar")
+   }
  }

And re-synchronize the ToolbarController upon reconnection:

  reconnect() {
    this.removeInternalToolbar()
    this.disconnectedCallback()
    this.connectedCallback()
+   if (this.editorController && this.toolbarElement) {
+     this.editorController.toolbarController = new ToolbarController(this.toolbarElement)
+     this.editorController.toolbarController.delegate = this.editorController
+   }
  }

codeCraft-Ritik avatar Oct 06 '26 04:10 codeCraft-Ritik