You are here

public static function HTMLRestrictions::fromString in Drupal 10

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()

23 calls to HTMLRestrictions::fromString()
Alignment::getElementsSubset in core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Alignment.php
CKEditor5ElementConstraintValidator::validate in core/modules/ckeditor5/src/Plugin/Validation/Constraint/CKEditor5ElementConstraintValidator.php
CKEditor5PluginDefinition::isCreatableElement in core/modules/ckeditor5/src/Plugin/CKEditor5PluginDefinition.php
Checks if the element is a plain tag, meaning the plugin can create it.
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
Gets all supported elements for the given plugins and text editor.

... See full list

File

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

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