Skip to content

removeCode() leaves the code block's contents loose on the root #501

Description

@panphora

Toggling a code block off can leave its text directly on the editor root instead of wrapping it in a block. If the caret is then inside that text, block commands silently do nothing because getStartBlockOfRange cannot find a block and returns early.

For example:

editor.setHTML("<p>one</p><p>two</p>");
// caret in the first paragraph
editor.toggleCode();  // <pre>one</pre><p>two</p>
editor.toggleCode();  // one<p>two</p>

After the second toggleCode(), "one" is a bare text node on the editor root.

removeCode() already calls fixContainer on the <pre> before replacing it with its children, which seems intended to wrap those children in blocks. But fixContainer returns immediately because of this guard in MergeSplit.ts:

/^(?:TABLE|TBODY|TR|TH|TD|P)/

Because the regex has no end anchor, PRE matches the P alternative. PICTURE and PROGRESS match as well.

Changing it to:

/^(?:TABLE|TBODY|TR|TH|TD|P)$/

looks safe to me. P still matches exactly, so the existing parsing behavior should be unchanged, while <pre> is allowed through fixContainer.

Seen in 2.4.8 and current master.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions