[upstream] CKEditor 5 Restructures And Removes Inline HTML Tags

Created on 20 March 2024, 3 months ago
Updated 8 May 2024, about 2 months ago

Problem/Motivation

CKEditor 5 changes the HTML Structure almost immediately. This doesn't affect the pre-existing HTML structure of different pages until and unless we open those respective nodes in /edit mode.

Steps to reproduce

  1. Setup a D10 Site.
  2. Enable CKEDitor 5
  3. Configure any text format to use "CKEditor 5" as the text editor in /admin/config/content/formats
  4. Input the following in "Source" -
    <div class="social-media">
    <span>Share</span> 
    <span class="icon">
    <a href="#" target="_blank" rel="noopener">
    <em class="fa-fw fa-twitter fab">&nbsp;</em>
    </a>
    </span>
    </div>
  5. The structure gets changed into -
    <div class="social-media">
    <span>Share</span>&nbsp;
    <a href="#" target="_blank" rel="noopener">
    <em class="fa-fw fa-twitter fab">
    <span class="icon">&nbsp;</span>
    </em>
    </a>
    </div>

Proposed resolution

Make sure that the HTML Structure doesn't get changed.

🐛 Bug report
Status

Needs work

Version

10.1

Component
CKEditor 5 

Last updated about 3 hours ago

Created by

🇮🇳India ishanghosh27

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

  • Issue created by @ishanghosh27
  • Status changed to Postponed: needs info 3 months ago
  • 🇮🇳India ishanghosh27

    Hi Wim, I am giving another example of the HTML DOM Restructuring which is not how I need it to be -
    Input -
    <div class="container"> <span class="icon"><a href="#" target="_blank"><em>SOMETHING</em></a></span></div>
    Output -
    <div class="container"><a href="#" target="_blank"><em><span class="icon">SOMETHING</span></em></a></div>
    The main issue with this kind of restructuring is that it affects all the existing nodes the moment they are opened up in "/edit" and re-saved. I am currently backtracking on what happens when I click on the "Source" button. Maybe some .js file gets called which in turn filters out and restructures the DOM(?).

  • 🇧🇪Belgium Wim Leers Ghent 🇧🇪🇪🇺

    I can see how that's disruptive. But that sure looks like some pretty questionable HTML 😅 That makes this difficult to describe and report. Can you still reproduce this without the <em> too? Try to find the smallest possible pattern, and then verify that it works with multiple tag combinations. That'd help report this upstream, and would result in a higher priority upstream.

  • 🇮🇳India ishanghosh27

    @Wim, I removed <em> from my DOM -

    <div class="container">
    <span>
    <a href="#">Test</a>
    </span>
    </div>

    It is getting changed into -

    <div class="container">
        <a href="#"><span>Test</span></a><span>&nbsp;</span>
    </div>

    However, upon further testing, this is not the only instance where the DOM is getting changed. I am checking on a few more instances of HTML structure, where the DOM is getting changed. Will keep everything updated here.

  • 🇧🇪Belgium Wim Leers Ghent 🇧🇪🇪🇺

    Thanks!

  • Status changed to Needs work 3 months ago
  • 🇮🇳India ishanghosh27

    Tested for these cases -
    Input -
    <a href="#">&nbsp;</a>
    Output - Entire Thing Got Removed. However, if I enter <a href="#">Lorem Ipsum</a>, it works.
    Another test case -
    Input -

    <a aria-label="Lorem Ipsum" class="lorem-ipsum-class" href="#" rel="noopener" target="_blank">
      <svg fill="none" height="18" viewbox="0 0 16 18" width="16" xmlns="http://www.w3.org/2000/svg">
        <path d="M1 2.6554C1 1.48814 2.27454 0.768165 3.27427 1.37068L13.8017 7.71531C14.7693 8.29848 14.7693 9.70157 13.8017 10.2847L3.27427 16.6294C2.27454 17.2319 1 16.5119 1 15.3447V2.6554Z"
          stroke="#14142B" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></path>
      </svg>
      Watch
    </a>

    Output -

    <a class="lorem-ipsum-class" href="#" aria-label="Lorem Ipsum" rel="noopener" target="_blank"><svg fill="none" height="18" viewBox="0 0 16 18" width="16" xmlns="http://www.w3.org/2000/svg">
        <path d="M1 2.6554C1 1.48814 2.27454 0.768165 3.27427 1.37068L13.8017 7.71531C14.7693 8.29848 14.7693 9.70157 13.8017 10.2847L3.27427 16.6294C2.27454 17.2319 1 16.5119 1 15.3447V2.6554Z" stroke="#14142B" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></path>
      </svg></a>
    <p>
        <a class="lorem-ipsum-class" href="#" aria-label="Lorem Ipsum" rel="noopener" target="_blank">&nbsp;Watch</a>
    </p>

    Here, the anchor tag that contains the main <svg><path></path></svg> is being copied after the main anchor tag, and put inside a paragraph (<p></p>).

  • 🇦🇹Austria mvonfrie

    That is related to the HTML normalization "feature" of CKeditor 5. See https://github.com/ckeditor/ckeditor5/issues/16203 for more examples.

Production build 0.69.0 2024