[upstream] [GHS] CKEditor 5 stripping <h2> tags (and more) if inside <div>

Created on 23 May 2023, over 1 year ago
Updated 10 May 2024, 7 months ago

I'm not sure if this is the right place to report this - CKEditor 5 is stripping specific H2 tags and more. I've imported a bunch of content using feeds and the first code sample works fine. The second code sample is what CKEditor 5 does with it.

CKEditor breaks it; much of the required HTML to make the accordion work is stripped out entirely. This is without restricting nor correcting HTML; the only way I can make changes is to work in plain text.

I used to run into this same sort of thing with earlier versions of Drupal; I think I had to use TinyMCE to keep the HTML intact. But there seems to be no option other than CKEditor 5 for Drupal, though.

This is a major issue, IMO - at least it is for me.

Any suggestions (other than working in plain text) greatly appreciated - thanks in advance...

<div class="field-group-bootstrap_accordion-wrapper accordion" id="group-articles">
	<div class="accordion-item">
		<h2 class="accordion-header" id="heading_field_contributions">
			<div class="accordion-button active" id="accordion-field-contributions" data-group="group_articles" data-bs-toggle="collapse" data-controls="field_contributions" aria-expanded="true" data-bs-target="#field-contributions" aria-controls="field-contributions">[Header text]</div>
		</h2>
		<div id="field-contributions" class="accordion-collapse collapse" aria-labelledby="heading_field_contributions" data-bs-parent="#group-articles">
			<div class="accordion-body">
				<div class="clearfix text-formatted field field--name-field-contributions field--type-text-long field--label-hidden field__item">
					<ul>
						<li><a href="[link]">[link text]</a></li>
					</ul>
				</div>
			</div>
		</div>
	</div>
</div>

Below is what happens in CKEditor 5. Note the missing HTML.

<div class="field-group-bootstrap_accordion-wrapper accordion" id="group-articles">
    <div class="accordion-item">
        <div class="accordion-header" id="heading_field_contributions">
            [Header text]
        </div>
        <div class="accordion-collapse collapse " id="field-contributions" aria-labelledby="heading_field_contributions" data-bs-parent="#group-articles">
            <div class="accordion-body">
                <div class="clearfix text-formatted field field--name-field-contributions field--type-text-long field--label-hidden field__item">
                    <ul>
                        <li>
                            <a href=[link]>[link text]</a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
🐛 Bug report
Status

Postponed

Version

11.0 🔥

Component
CKEditor 5 

Last updated about 5 hours ago

Created by

🇺🇸United States bcobin

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

Comments & Activities

  • Issue created by @bcobin
  • 🇦🇺Australia tinarey

    Looks like that's a ckeditor bug, it happens on the demo page for the source input https://ckeditor.com/ckeditor-5/demo/html-support/
    If you select h2 via the heading dropdown, it stays.

  • 🇦🇺Australia tinarey

    Looks like this might be the same: https://www.drupal.org/project/drupal/issues/3349893 🐛 [upstream] [GHS] CKEditor 5 removes s that wrap HTML elements not natively supported by CKEditor 5 Fixed
    (added this issue to the CKEditor report).

  • 🇦🇺Australia fenstrat Australia

    This sounds like a duplicate of 🐛 [upstream] [GHS] CKEditor 5 removes s that wrap HTML elements not natively supported by CKEditor 5 Fixed , however it seems a bit different. That's because in 3349893 it's about an inline element (i.e. an <a> tag) wrapping a block element (i.e. a <div> tag). Whereas here the wrapping h2 is a block element. Not sure if that affects this issue here, but thought it was worth noting.

  • Status changed to Postponed: needs info over 1 year ago
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Can you please upload the output of drush config:export of the relevant text editor + format? Thanks!

  • 🇺🇸United States bcobin

    OK - here ya go. Note that this is a pretty much plain vanilla install - I'm only at the import/organize stage. If you use the HTML I've provided in a vanilla site, I'm pretty sure you'd get the same result.

    Thanks for the quick response!

  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    You provided everything 😅

    I just needed these two files! Looks like you have only a single text format/editor: . Is that right?

  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Your text format is configured to not allow <div> tags 😅

    So of course it's getting stripped. CKEditor 5 is configured to match the HTML restrictions, which means no <div> tags.

    Can you please share your text editor + format YAML config exports (like the ones I posted in #8) from before you upgraded from CKEditor 4 to 5? 🙏 We have VERY detailed test coverage that proves this should not be possible: if <div> was allowed before, it should still be allowed.

  • 🇺🇸United States bcobin

    Thanks, Will - this isn't an upgrade, though. It's a straight D10 install.

    The old site I'm exporting from (D7) used Field Collection, which I now see is available (if patched) for D10. I do recall, however, that in the past, I've had to use TinyMCE to preserve certain tags. Thanks for the <div> info, though - that explains a lot!

  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    In #7, you only provided an export of Basic HTML, no other text formats!

    Try using Full HTML. Then your markup should work. Or did you already try that?

    That being said, there are still edge cases of this kind of arbitrary HTML that do not work correctly in CKEditor 5. In the next 10.1 release (after the current first beta), CKEditor 5 will be on version 38, which has additional fixes for this, which may address this. See 📌 Update CKEditor 5 to 38.0.0 Fixed .

    Oh and like @TinaRey said: if this is reproducible on the official CKEditor 5 demo site with GHS (General HTML Support) enabled, then this definitely would be an upstream bug! This is definitely a different bug though: <a> are very special (because they may need to wrap inline or block elements, and both elements supported natively by CKEditor 5 and custom ones that are supported thanks to GHS).

    What we need here is to distill this down to the minimal test case that triggers the problem. Once we have that, we should report it as an upstream bug.

    Tentatively bumping priority and tagging , because based on @TinaRey's comment, that is now very likely.)

  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    I suspect the triggering condition is that this is markup inside of a <div>. It'd be great if that could be confirmed 😊🙏

  • 🇺🇸United States bcobin

    Try using Full HTML. Then your markup should work. Or did you already try that?

    Oh, I most definitely did; in fact, there's no other way I would have tried it. I have no idea why my export didn't show HTML, though. In any event, if you try the HTML I posted on a D 10 install, I'd be surprised if you didn't get the same result.

    In the meantime, I've decided to use a different method to do what I was trying to do, so I'm off the dime here. Thanks for getting back and keep up the good work!

  • 🇦🇺Australia tinarey

    I could replicate the issue in a pretty fresh Drupal 10 install with full_html, other formats with several ways of adding tags, ids, classes etc in the allowed list etc and at some point noticed that the h2 doesn't get stripped if it's applied via UI; only via source. That's when I went to their demo page for the "source" button which says it allows all sorts of dangerous html. The test case is as minimal as

    <h2>
        <div>Test</div>
    </h2>
    

    I left a comment in the upstream issue https://github.com/ckeditor/ckeditor5/issues/13803#issuecomment-1558319636

  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Asked @witeksocha to confirm — thanks @TinaRey!

  • Status changed to Postponed about 1 year ago
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Was confirmed at https://github.com/ckeditor/ckeditor5/issues/14249. (I forgot to follow up here.)

  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
  • 🇺🇸United States luke.leber Pennsylvania

    Looking at the upstream issue, there was a valid point made that <h2><div>content</div></h2> isn't legal html.

    That may help explain why this hasn't been addressed yet. Given how long this has been open, might it be an option for folks suffering from this to consider migrating to markup like <h2><span>content</span></h2> instead? I'm not convinced that cksource will make divs in headings work as a product feature.

Production build 0.71.5 2024