function HTMLRestrictions::toCKEditor5ElementsArray

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

Transforms into the CKEditor 5 package metadata "elements" representation.

Return value

string[] A list of strings, with each string expressing an allowed element, structured in the way expected by the CKEditor 5 package metadata.

See also

https://ckeditor.com/docs/ckeditor5/latest/framework/guides/contributin…

File

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

Class

HTMLRestrictions
Represents a set of HTML restrictions.

Namespace

Drupal\ckeditor5

Code

public function toCKEditor5ElementsArray() : array {
    $readable = [];
    foreach ($this->elements as $tag => $attributes) {
        $attribute_string = '';
        if (is_array($attributes)) {
            foreach ($attributes as $attribute_name => $attribute_values) {
                if (is_array($attribute_values)) {
                    $attribute_values_string = implode(' ', array_keys($attribute_values));
                    $attribute_string .= "{$attribute_name}=\"{$attribute_values_string}\" ";
                }
                else {
                    // Special case: the global attribute `*` HTML tag.
                    // @see https://html.spec.whatwg.org/multipage/dom.html#global-attributes
                    // @see validateAllowedRestrictionsPhase2()
                    // @see validateAllowedRestrictionsPhase4()
                    if ($attribute_values === FALSE) {
                        assert($tag === '*');
                        continue;
                    }
                    $attribute_string .= "{$attribute_name} ";
                }
            }
        }
        // Special case: the global attribute `*` HTML tag.
        // @see https://html.spec.whatwg.org/multipage/dom.html#global-attributes
        // @see validateAllowedRestrictionsPhase2()
        // @see validateAllowedRestrictionsPhase4()
        if ($tag === '*' && empty(array_filter($attributes))) {
            continue;
        }
        $joined = '<' . $tag . (!empty($attribute_string) ? ' ' . trim($attribute_string) : '') . '>';
        array_push($readable, $joined);
    }
    assert(Inspector::assertAllStrings($readable));
    return $readable;
}

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