Duplicate Post preview loses registered event listeners for rendered bb-code preview

Xon

Well-known member
Affected version
2.3.3
While technically xf-init is executed, any event listeners are lost probably resulting in the results of xf-init being GC'ed. The problem is the use of innerHTML to copy data into the DOM, as this creates new nodes and doesn't move/copy the existing nodes.

This is noticeable for custom bb-code which has xf-init handlers, and xf-init is executed on content which is then dropped.

Code:
function toPreview (previewHtml)
{
    const previewBox = getPreviewBox()

    const apply = function (previewHtml)
    {
...

        previewBox.innerHTML = previewHtml.querySelector('.bbWrapper').innerHTML
        XF.display(previewBox)
    }

    if (typeof previewHtml == 'string')
    {
...
    }
    else
    {
...

        XF.ajax(
            'POST',
            XF.canonicalizeUrl(href),
            formData,
            data =>
            {
                XF.setupHtmlInsert(data.html, html =>
                {
                    XF.activate(html)
                    apply(html)
                })
            },
        )
    }
}

Using previewBox.replaceChildren(...previewHtml.querySelector('.bbWrapper').children) instead of previewBox.innerHTML = will work.

Use of setting innerHTML probably needs auditing to see if there are any other cases this impacts.

Note: XF.setupHtmlInsert() calls XF.Activate() on html so if replaceChildren() is called by itself then the XF.Activate() will be ineffective as there is no contents.
 
Last edited:
This was fixed for a different bug report:

 
Back
Top Bottom