You are here

public function HTMLRestrictions::toGeneralHtmlSupportConfig in Drupal 10

Transforms into the CKEditor 5 GHS configuration representation.

Return value

string[] An array of allowed elements, structured in the manner expected by the CKEditor 5 htmlSupport plugin constructor.

See also

https://ckeditor5.github.io/docs/nightly/ckeditor5/latest/features/gener...

https://ckeditor5.github.io/docs/nightly/ckeditor5/latest/api/module_eng...

File

core/modules/ckeditor5/src/HTMLRestrictions.php, line 1098

Class

HTMLRestrictions
Represents a set of HTML restrictions.

Namespace

Drupal\ckeditor5

Code

public function toGeneralHtmlSupportConfig() : array {
  $allowed = [];

  // Resolve any remaining wildcards based on Drupal's assumptions on
  // wildcards to ensure all HTML tags that Drupal thinks are supported are
  // truly supported by CKEditor 5.
  $elements = self::resolveWildcards($this)
    ->getAllowedElements();
  foreach ($elements as $tag => $attributes) {
    $to_allow = [
      'name' => $tag,
    ];
    assert($attributes === FALSE || is_array($attributes));
    if (is_array($attributes)) {
      foreach ($attributes as $name => $value) {

        // Convert the `'hreflang' => ['en' => TRUE, 'fr' => TRUE]` structure
        // that this class expects to the `['en', 'fr']` structure that the
        // GHS functionality in CKEditor 5 expects.
        if (is_array($value)) {

          // Ensure that all values are strings, this is necessary since PHP
          // transforms the "1" string into 1 the number when it is used as
          // an array key.
          $value = array_map('strval', array_keys($value));
        }

        // Drupal never allows style attributes due to security concerns.
        // @see \Drupal\Component\Utility\Xss
        if ($name === 'style') {
          continue;
        }

        // Special case: the global attribute `*` HTML tag.
        // @see https://html.spec.whatwg.org/multipage/dom.html#global-attributes
        // @see validateAllowedRestrictionsPhase2()
        // @see validateAllowedRestrictionsPhase4()
        assert($value === TRUE || Inspector::assertAllStrings($value) || $tag === '*' && $value === FALSE);

        // If a single attribute value is allowed, it must be TRUE (see the
        // assertion above). Otherwise, it must be an array of strings (see
        // the assertion above), which lists all allowed attribute values. To
        // be able to configure GHS to a range of values, we need to use a
        // regular expression.
        $allowed_attribute_value = is_array($value) ? [
          'regexp' => [
            'pattern' => '/^(' . implode('|', str_replace('*', '.*', $value)) . ')$/',
          ],
        ] : $value;
        if ($name === 'class') {
          $to_allow['classes'] = $allowed_attribute_value;
          continue;
        }

        // Most attribute restrictions specify a concrete attribute name. When
        // the attribute name contains a partial wildcard, more complex syntax
        // is needed.
        $to_allow['attributes'][] = [
          'key' => strpos($name, '*') === FALSE ? $name : [
            'regexp' => [
              'pattern' => self::getRegExForWildCardAttributeName($name),
            ],
          ],
          'value' => $allowed_attribute_value,
        ];
      }
    }
    $allowed[] = $to_allow;
  }
  return $allowed;
}