function HTMLRestrictions::fromString

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

Parses a string of HTML restrictions into a HTMLRestrictions value object.

Parameters

string $elements_string: A string representing a list of allowed HTML elements.

Return value

\Drupal\ckeditor5\HTMLRestrictions

See also

::toFilterHtmlAllowedTagsString()

::toCKEditor5ElementsArray()

25 calls to HTMLRestrictions::fromString()
Alignment::getElementsSubset in core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Alignment.php
Returns a configured subset of the elements supported by this plugin.
CKEditor5ElementConstraintValidator::validate in core/modules/ckeditor5/src/Plugin/Validation/Constraint/CKEditor5ElementConstraintValidator.php
CKEditor5PluginDefinition::validateDrupalAspects in core/modules/ckeditor5/src/Plugin/CKEditor5PluginDefinition.php
Validates the Drupal aspects of the CKEditor 5 plugin definition.
CKEditor5PluginManager::getProvidedElements in core/modules/ckeditor5/src/Plugin/CKEditor5PluginManager.php
CKEditor5UpdateImageToolbarItemTest::getFilterHtmlRestrictions in core/modules/ckeditor5/tests/src/Functional/Update/CKEditor5UpdateImageToolbarItemTest.php
Gets the configured restrictions for the `filter_html` filter plugin.

... See full list

File

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

Class

HTMLRestrictions
Represents a set of HTML restrictions.

Namespace

Drupal\ckeditor5

Code

public static function fromString(string $elements_string) : HTMLRestrictions {
    // Preprocess wildcard tags: convert `<$text-container>` to
    // `<preprocessed-wildcard-text-container__>` and `<*>` to
    // `<preprocessed-global-attribute__>`.
    // Note: unknown wildcard tags will trigger a validation error in
    // ::validateAllowedRestrictionsPhase1().
    $replaced_wildcard_tags = [];
    $elements_string = preg_replace_callback('/<(\\$[a-z][0-9a-z\\-]*|\\*)/', function ($matches) use (&$replaced_wildcard_tags) {
        $wildcard_tag_name = $matches[1];
        $replacement = $wildcard_tag_name === '*' ? 'preprocessed-global-attribute__' : sprintf("preprocessed-wildcard-%s__", substr($wildcard_tag_name, 1));
        $replaced_wildcard_tags[$replacement] = $wildcard_tag_name;
        return "<{$replacement}";
    }, $elements_string);
    // Reuse the parsing logic from FilterHtml::getHTMLRestrictions().
    $configuration = [
        'settings' => [
            'allowed_html' => $elements_string,
        ],
    ];
    $filter = new FilterHtml($configuration, 'filter_html', [
        'provider' => 'filter',
    ]);
    $allowed_elements = $filter->getHTMLRestrictions()['allowed'];
    // Omit the broad wildcard addition that FilterHtml::getHTMLRestrictions()
    // always sets; it is specific to how FilterHTML works and irrelevant here.
    unset($allowed_elements['*']);
    // @see \Drupal\filter\Plugin\Filter\FilterHtml::getHTMLRestrictions()
    // @todo remove this in https://www.drupal.org/project/drupal/issues/3226368
    // cSpell:disable-next-line
    unset($allowed_elements['__zqh6vxfbk3cg__']);
    // Postprocess tag wildcards: convert
    // `<preprocessed-wildcard-text-container__>` to `<$text-container>`.
    foreach ($replaced_wildcard_tags as $processed => $original) {
        if (isset($allowed_elements[$processed])) {
            $allowed_elements[$original] = $allowed_elements[$processed];
            unset($allowed_elements[$processed]);
        }
    }
    // When allowing all tags on an attribute, transform FilterHtml output from
    // ['tag' => ['*'=> TRUE]] to ['tag' => TRUE]
    foreach ($allowed_elements as $element => $attributes) {
        if (is_array($attributes) && isset($attributes['*']) && $attributes['*'] === TRUE) {
            $allowed_elements[$element] = TRUE;
        }
    }
    return new self($allowed_elements);
}

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