function HTMLRestrictions::toGeneralHtmlSupportConfig

Same name in other branches
  1. 10 core/modules/ckeditor5/src/HTMLRestrictions.php \Drupal\ckeditor5\HTMLRestrictions::toGeneralHtmlSupportConfig()
  2. 11.x core/modules/ckeditor5/src/HTMLRestrictions.php \Drupal\ckeditor5\HTMLRestrictions::toGeneralHtmlSupportConfig()

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/gene…

https://ckeditor5.github.io/docs/nightly/ckeditor5/latest/api/module_en…

File

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

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.