CKEditor5 removes empty P tags

Created on 30 April 2025, 19 days ago

Problem/Motivation

CKEditor4 allowed empty HTML blocks, such as the following, entered in source mode:

<p class="my-class">
  &nbsp;
</p>

CKEditor5 removes this entire block, include when updating from CKEditor4, which causes data loss.

A couple of things to consider:

  • Is the above valid HTML?
  • Even if it isn't, should it be possible to have a way to add it anyway? For example, when using JS or other code to process it before displaying it to the end user.

I've had a look through related closed and open issues but couldn't find any addressing this specific case. In some issues, people with a related problem were encouraged to open a new issue.

Steps to reproduce

  1. Set Full HTML text format to use CKEditor5
  2. Remove any restrictions and limits (not strictly necessary though)
  3. Click the Source button and dd the above HTML
  4. Toggle source off and on, the HTML will disappear

There may not be much we can do here as it appears to be an upstream issue(/feature?).

It can also be reproduced on the demo page: https://ckeditor.com/docs/ckeditor5/45.0.0/examples/builds-custom/full-f...

In which case, is a workaround within Drupal possible?

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

10.4

Component

ckeditor5.module

Created by

🇦🇺Australia imclean Tasmania

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

Comments & Activities

  • Issue created by @imclean
  • 🇦🇺Australia imclean Tasmania
  • We were just running into this same issue, but with anchor tags (that use aria, javascript, and css).

    Using the CKEditor demo page:

    <ul>
        <li>
            example text
        </li>
        <li>
            <a href="https://www.example.com">&nbsp;</a>&nbsp;<a href="https://www.drupal.org">&nbsp;</a>
        </li>
    </ul>
    

    ...will remove the non-breaking space from the first link and turn it into a regular space when going back and forth in source. The HTML is kept, but the first non-breaking space is lost.

    Using the same code, but without the first li that had visible text:

    <ul>
        <li>
            <a href="https://www.example.com">&nbsp;</a>&nbsp;<a href="https://www.drupal.org">&nbsp;</a>
        </li>
    </ul>
    

    ...shows two spaces when you get out of source, but if you go back into source, all of the HTML is gone.

    I'm seeing lots of variations of how it handles the non-breaking spaces, but in general it seems very broken and dependent on other elements within the code.

  • 🇳🇿New Zealand quietone

    If this problem was discovered on a version of Drupal that is not 11.x, add that information in the issue summary and leave the version at 11.x. In Drupal core changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies .

  • 🇦🇺Australia imclean Tasmania
Production build 0.71.5 2024