Inserted HTML template goes wrong

Created on 20 November 2023, about 1 year ago
Updated 16 February 2024, 9 months ago

Problem/Motivation

I have set up the following two HTML templates in ckeditor5_template.json

[
    {
        "title": "Heading2",
        "icon": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path d=\"M8 16h8v2H8zm0-4h8v2H8zm6-10H6c-1.1 0-2 .9-2 2v16c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zm4 18H6V4h7v5h5v11z\"/></svg>",
        "description": "Insert heading2 tags for this site.",
        "html": "<div class=\"o-title__border\"><h2 class=\"o-title__xl\">Heading2</h2></div>"
    },
    {
        "title": "Heading3",
        "icon": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path d=\"M8 16h8v2H8zm0-4h8v2H8zm6-10H6c-1.1 0-2 .9-2 2v16c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zm4 18H6V4h7v5h5v11z\"/></svg>",
        "description": "Insert heading3 tags for this site.",
        "html": "<div class=\"o-title__border2\"><h3 class=\"o-title__l\">Headingr3</h3></div>"
    }
]

After that, I inserted the HTML templates in the order of "Heading2", "Heading3" on CKEditor5, and found the following in the Heading2 div HTML for Heading3 was encapsulated in the Heading2 div as shown below.

<div class="o-title__border">
    <h2 class="o-title__xl">
        Heading2
    </h2>
    <div class="o-title__border2">
        <h3 class="o-title__l">
            Headingr3
        </h3>
    </div>
</div>

Steps to reproduce

Proposed resolution

I want the tags to be inserted in the order specified, as shown below.

<div class="o-title__border"><h2 class="o-title__xl">Heading2</h2></div>
<div class="o-title__border2"><h3 class="o-title__l">Headingr3</h3></div>

Remaining tasks

User interface changes

API changes

Data model changes

💬 Support request
Status

Closed: works as designed

Version

1.0

Component

Miscellaneous

Created by

🇯🇵Japan kentaono

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @kentaono
  • 🇯🇵Japan kentaono

    Hello all,
    I have tried pasting the above two HTML codes in CKEditor5 source mode and had no problems pasting them manually.

  • 🇩🇪Germany dbielke1986

    Can confirm this behavior - we will take a look at it.
    Thanks for reporting!

  • Status changed to Closed: works as designed about 1 year ago
  • 🇩🇪Germany dbielke1986

    The reason for this is a standard behavior of the CKEditor. If you insert the heading, you remain in the current DIV.

    You can reproduce this by simply switching to a new line after inserting the first heading and continuing to write there. Here you remain in the current DIV. The following template is therefore also inserted within the first DIV.

    You can solve this by adapting your templates as follows (add the "<p>&nbsp;</p>" after the template-div):

    [
        {
            "title": "Heading2",
            "icon": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path d=\"M8 16h8v2H8zm0-4h8v2H8zm6-10H6c-1. 1 0-2 .9-2 2v16c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zm4 18H6V4h7v5h5v11z\"/></svg>",
            "description": "Insert heading2 tags for this site.",
            "html": "<div class=\"o-title__border\"><h2 class=\"o-title__xl\">Heading2</h2></div><strong><p>&nbsp;</p></strong>"
        },
        {
            "title": "Heading3",
            "icon": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\"><path d=\"M8 16h8v2H8zm0-4h8v2H8zm6-10H6c-1. 1 0-2 .9-2 2v16c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zm4 18H6V4h7v5h5v11z\"/></svg>",
            "description": "Insert heading3 tags for this site.",
            "html": "<div class=\"o-title__border2\"><h3 class=\"o-title__l\">Headingr3</h3></div><strong><p>&nbsp;</p></strong>"
        } 
    ] 
  • 🇯🇵Japan kentaono

    Hello Daniel,

    Thanks for the reply.
    I followed your advice and wrote the template and was able to insert the tags as intended.

  • 🇩🇪Germany dbielke1986

    This "trapped cursor" thing is an issue with CKEditor 5 in general, as reported here in core: [GHS] Allow the cursor to leave divs and other HTML elements at document start or end (3344462) 🐛 [GHS] Allow the cursor to leave s and other HTML elements at document start or end Postponed: needs info and upstream on CKEditor's github: [GHS] It is not possible to leave DIV element #10220 back in 2021 with sadly little movement. It happens whenever you try and use a <div> (or simmilar) in CKEditor 5.

Production build 0.71.5 2024