CKEDITOR.tools.convertToPx breaks with Hotwire Turbo / Turbolinks, et al #5158
Description
Type of report
CKEDITOR.tools.convertToPx
does not properly support frameworks that replace HTML body but don't reload Javascript libraries, such as Hotwire Turbo.
More accurately, anything that clears or replaces the DOM body will break this CKEDITOR.tools.convertToPx
Provide detailed reproduction steps (if any)
This surfaces when using the autogrow plugin, but anything that relies on editor.resize()
will trigger this behavior. Instantiate an editor, clear the DOM body, create new HTML for an editor and instantiate it, then call resize on it.
Expected result
The editor should resize to the size specified.
Actual result
The editor resizes to a zero height.
Other details
This is because CKEDITOR.tools.convertToPx
declares a variable calculator
which it uses to memoize an element appended to the body used for the calculation.
But if anything clears the DOM body, this "calculator" element no longer exists, even though the variable still points to the disembodied element. This causes the incorrect calculation that—at least in Chrome—returns zero.
The fix is to also check calculator.$.isConnected
to see whether it is connected to the DOM:
Lines 978 to 983 in c0dbd14
if ( !calculator ) {
calculator = CKEDITOR.dom.element.createFromHtml( '<div style="position:absolute;left:-9999px;' +
'top:-9999px;margin:0px;padding:0px;border:0px;"' +
'></div>', CKEDITOR.document );
CKEDITOR.document.getBody().append( calculator );
}
else
{
if ( !calculator.$.isConnected ) {
// Re-attach to DOM if it got disconnected.
CKEDITOR.document.getBody().append( calculator );
}
}
Activity