Problem/Motivation

We added support for markup in data-caption in #3222808: Follow-up for #3201646: markup in image captions is lost. However, we only ever tested data-caption with data that upcasts into $text. However, there are examples of valid child elements of <figcaption> that would upcast into a model element (e.g. <section> would be a valid child of <figcaption).

Steps to reproduce

  1. Create new text format that has Drupal Media + Image captions enabled and add <section> to source editable elements
  2. Create content using the new text format
  3. Embed media to the content and enable captions
  4. Add <section> to the data-caption value in source editing
  5. Return back to editing 💥

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lauriii created an issue. See original summary.

Wim Leers’s picture

Priority: Major » Minor
Related issues: +#2508421: FilterCaption hard-codes allowed tags

Due to #2508421: FilterCaption hard-codes allowed tags, I say this is Minor. Chances of this happening are tiny.

lauriii’s picture

Should we at least add test coverage as part of this issue for all of the elements that are currently hard coded as supported? I think all of the elements listed there should work since they all convert to an attribute, but it might make sense to explicitly test that to ensure it remains to be the case.

Wim Leers’s picture

That would be prudent, and a nice nice-to-have indeed 🤓

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Wim Leers’s picture

Issue tags: +Needs tests

Wim Leers’s picture

Priority: Minor » Major

A specific concrete case of this was encountered: <br> is not allowed right now in the caption, even though FilterCaption explicitly allows it. See #3309204: CKEditor 5 crashes with <br> inside of data-caption for details + failing test. Crediting @mherchel for that.

Bumping to Major because this constitutes a regression compared to CKEditor 4.

Wim Leers’s picture

Per @longwave in Slack, this is potentially related to #2441811-69: Upgrade filter system to HTML5.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

nod_’s picture

Failing test, not quite sure how to actually fix the problem.

nod_’s picture

Status: Active » Needs review
nod_’s picture

nod_’s picture

Status: Needs review » Needs work

The last submitted patch, 15: core-3276213-15.patch, failed testing. View results

nod_’s picture

Thought it was upstream issue, doesn't appear so :/

Wim Leers’s picture

Well … that's a good thing 🤓 That means we can fix it! 😜

I bet it's a bug in

    // Parse HTML from data-caption attribute and upcast it to model fragment.
    const viewFragment = editor.data.processor.toView(
      viewItem.getAttribute('data-caption'),
    );
[…]
    conversionApi.convertChildren(viewFragment, modelFragment);

    // Insert caption model nodes into the caption.
    // eslint-disable-next-line no-restricted-syntax
    for (const child of Array.from(modelFragment.getChildren())) {
      writer.append(child, caption);
    }

I wonder if it's simply the fact that some elements are not allowed to exist in those places, because that's what #3309204-5: CKEditor 5 crashes with <br> inside of data-caption's stack trace suggests:

ckeditor5.js?ri55dw:430 TypeError: Cannot read properties of undefined (reading 'name')
    at d._checkContextMatch (ckeditor5-dll.js?v=35.1.0:5:271947)
    at d.checkChild (ckeditor5-dll.js?v=35.1.0:5:268547)
    at d.<anonymous> (ckeditor5-dll.js?v=35.1.0:5:408865)
    at d.fire (ckeditor5-dll.js?v=35.1.0:5:394092)
    at <computed> [as checkChild] (ckeditor5-dll.js?v=35.1.0:5:408917)
    at d.findAllowedParent (ckeditor5-dll.js?v=35.1.0:5:270959)
    at k._splitToAllowedParent (ckeditor5-dll.js?v=35.1.0:5:84709)
    at k._safeInsert (ckeditor5-dll.js?v=35.1.0:5:84253)
    at Object.safeInsert (ckeditor5-dll.js?v=35.1.0:5:82254)
    at k.<anonymous> (ckeditor5-dll.js?v=35.1.0:5:136002)
nod_’s picture

Status: Needs work » Needs review
FileSize
38.69 KB
35.67 KB

Thanks to some help from the CKE folks we have a fix. https://github.com/ckeditor/ckeditor5/pull/12798

Status: Needs review » Needs work

The last submitted patch, 19: core-3276213-19.patch, failed testing. View results

Wim Leers’s picture

That was fast! :O

nod_’s picture

Status: Needs work » Needs review
FileSize
118.08 KB
79.71 KB

only updated the image plugin, not the drupal media one.

Added test for the image plugin + update code for the drupal media plugin

Status: Needs review » Needs work

The last submitted patch, 22: core-3276213-20.patch, failed testing. View results

nod_’s picture

Wim Leers’s picture

Queued 10.0, 9.5 and 9.4 tests of #24.

  1. +++ b/core/modules/ckeditor5/js/ckeditor5_plugins/drupalImage/src/drupalimageediting.js
    @@ -478,20 +478,13 @@ function viewImageToModelImage(editor) {
    -      const modelFragment = writer.createDocumentFragment();
    ...
    -      conversionApi.convertChildren(viewFragment, modelFragment);
    ...
    +      conversionApi.convertChildren(viewFragment, caption);
    

    So … was the problem that there were two distinct document fragments, and nodes cannot be moved from one document fragment to another?

  2. +++ b/core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaLibraryTest.php
    @@ -300,4 +301,43 @@ public function testAlt() {
    +  public function testCaptionMarkup() {
    

    🤔 What is the purpose of this new test?

    Why isn't \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testEditableCaption() adequate?

    The Media Library plugin is just for inserting, the Media plugin is the one that provides this functionality. So this new test is IMHO at the very least in the wrong test class 😅

nod_’s picture

1. So I don't have 100% grasp on the cke5 code, but I think transforming text into nodes behaves differently based on the context in which it happens. Context was an empty document fragment before, now it's the caption element. Somehow somewhere it's the right thing to do.

2. no idea 🤷 :D

Updated the test location.

Wim Leers’s picture

Status: Needs review » Needs work
+++ b/core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php
@@ -627,6 +627,31 @@ public function testEditableCaption() {
+    $caption_markup = 'Test<br>caption';

I'd rather just add a <br> to the existing \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testEditableCaption() test … 😅 These tests are relatively slow, no need for repeating 99% of the test, if we can test both aspects at the same time!

nod_’s picture

It wasn't straightforward so went this way to keep it green will try again later today

nod_’s picture

Status: Needs work » Needs review
FileSize
119.07 KB
3.19 KB

Easier after some sleep

Wim Leers’s picture

Status: Needs review » Needs work

Great! Could you also upload a failing test-only patch, now that the test coverage is just updating an existing test? 🙏

nod_’s picture

Status: Needs work » Needs review
FileSize
4.49 KB

Failing test

Status: Needs review » Needs work

The last submitted patch, 31: core-3276213-31.patch, failed testing. View results

nod_’s picture

Status: Needs work » Needs review
Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +rc target

Perfect! 👏

1) Drupal\Tests\ckeditor5\FunctionalJavascript\ImageTest::testImageCaption
Failed asserting that a NULL is not empty.

/var/www/html/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:122
/var/www/html/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:55
/var/www/html/core/modules/ckeditor5/tests/src/FunctionalJavascript/ImageTestBase.php:580
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:728

which is the first assertion after setting a data-caption containing a <br>, hence #31's failure proves that the green test result for #29 can be trusted! 👍

nod_’s picture

rerolled

crediting wim for review and lauriii for actually finding the fix

nod_’s picture

  • alexpott committed 8b1819b on 10.1.x
    Issue #3276213 by nod_, Wim Leers, lauriii, mherchel, longwave: Uncaught...

  • alexpott committed 10544d8 on 10.0.x
    Issue #3276213 by nod_, Wim Leers, lauriii, mherchel, longwave: Uncaught...
alexpott’s picture

Version: 10.1.x-dev » 9.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 8b1819bf3b to 10.1.x and 10544d8d07 to 10.0.x. Thanks!
Committed and pushed afc43ea00f to 9.5.x and c62e7d1c80 to 9.4.x. Thanks!

  • alexpott committed afc43ea on 9.5.x
    Issue #3276213 by nod_, Wim Leers, lauriii, mherchel, longwave: Uncaught...

  • alexpott committed c62e7d1 on 9.4.x
    Issue #3276213 by nod_, Wim Leers, lauriii, mherchel, longwave: Uncaught...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.