Error message The current CKEditor 5 build requires the following elements and attributes

Created on 12 June 2023, over 1 year ago
Updated 21 May 2024, 7 months ago

Problem/Motivation

When trying to save a CKEditor5 text format with the filter provided by this module enabled an error like this occurs:

The current CKEditor 5 build requires the following elements and attributes:
<br> <p> <h2> <h3> <h4> <h5> <h6> <strong> <em> <pre> <code class="language-*"> <s> <sub> <sup> <blockquote> <a href> <ul> <ol reversed start> <li> <hr> <table> <tr> <td rowspan colspan> <th rowspan colspan> <thead> <tbody> <tfoot> <caption> <img src alt height width data-entity-uuid data-entity-type data-caption data-align>
The following elements are missing:
<br> <p> <h2> <h3> <h4> <h5> <h6> <strong> <em> <pre> <code class="language-*"> <s> <sub> <sup> <blockquote> <a href> <ul> <ol reversed start> <li> <hr> <table> <tr> <td rowspan colspan> <th rowspan colspan> <thead> <tbody> <tfoot> <caption> <img src alt height width data-entity-uuid data-entity-type data-caption data-align>

Steps to reproduce

  1. Install a fresh Drupal 10 site with the standard profile
  2. Install this module
  3. Edit the "Full HTML" text format
  4. Tick "htmLawed HTML filter and purifier"
  5. Attempt to save
  6. You will see the error

This was mentioned on TypeError: Argument 1 passed to Drupal\ckeditor5\HTMLRestrictions::__construct() must be of the type array, object given 🐛 TypeError: Argument 1 passed to Drupal\ckeditor5\HTMLRestrictions::__construct() must be of the type array, object given Closed: works as designed but it was not solved by the fix from the linked core issue.

More details about why this is happening:

It seems like this is happening in Drupal\ckeditor5\Plugin\Validation\Constraint\FundamentalCompatibilityConstraintValidator::checkHtmlRestrictionsMatch().

Basically the filter is loaded here because it is of type FilterInterface::TYPE_HTML_RESTRICTOR:

    $html_restrictor_filters = static::getFiltersInFormatOfType(
      $text_editor->getFilterFormat(),
      FilterInterface::TYPE_HTML_RESTRICTOR
    );

however, since the filter doesn't actually override the getHTMLRestrictions() method it just returns false, which means that here:

$allowed = HTMLRestrictions::fromFilterPluginInstance($filter);
$diff_allowed = $allowed->diff($provided);
$diff_elements = $provided->diff($allowed);

$allowed is set to empty and this results in $diff_elements including all elements that the format provides. A validation violation is added here as a result:

      if (!$diff_elements->allowsNothing()) {
        $this->context->buildViolation($constraint->missingElementsMessage)
          ->setParameter('@list', implode(' ', $provided->toCKEditor5ElementsArray()))
          ->setParameter('@diff', implode(' ', $diff_elements->toCKEditor5ElementsArray()))
          ->atPath("filters.$filter_plugin_id")
          ->addViolation();
      }

This causes the format configuration form to display the error when saving, likely because it thinks the filter forbids all elements from being saved.

Proposed resolution

As I see it there are two ways to fix this: change this filter's type (e.g. to Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE instead of Drupal\filter\Plugin\FilterInterface::TYPE_HTML_RESTRICTOR) or implement getHtmlRestrictions() so the filter plugin accurately reports the tags and attributes it supports. The latter is probably more "correct", but it seems to be quite difficult to do this in a way that will be compatible with other HTML restricting filters like filter_html.

So I'm going to attach an MR that switches the type instead. However, please consider that alternative before merging this in because I'm not sure that's the most appropriate solution.

🐛 Bug report
Status

Closed: won't fix

Version

4.1

Component

Code

Created by

🇨🇦Canada dylan donkersgoed London, Ontario

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

Comments & Activities

Production build 0.71.5 2024