trix
trix copied to clipboard
Internal Toolbar Permanently Vanishes & Creates Zombie `ToolbarController` During Morphing
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
}
- When
<trix-editor>is first initialized without an external toolbar,get toolbarElement()generates a unique toolbar ID (trix-toolbar-1), setsthis.setAttribute("toolbar", "trix-toolbar-1"), and inserts the toolbar into the DOM. - When morphing or reconnection occurs,
this.removeInternalToolbar()removesthis.internalToolbarfrom the DOM, but fails to remove thetoolbarattribute from<trix-editor>. - When
get toolbarElement()is subsequently accessed:
Becauseget toolbarElement() { if (this.hasAttribute("toolbar")) { return this.ownerDocument?.getElementById(this.getAttribute("toolbar")) } else if (this.parentNode) { // ... recreates toolbar ... } }this.hasAttribute("toolbar")remainstrue(pointing to"trix-toolbar-1"), it attemptsgetElementById("trix-toolbar-1"). Since that node was removed from the DOM, it returnsnull! Theelse ifbranch is never entered, and the toolbar is never recreated. - Even worse,
this.editorControlleralready exists (if (!this.editorController)skips creating a new one), sothis.editorController.toolbarControllercontinues 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 executedelement.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
toolbarattribute 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
+ }
}