function HTMLRestrictions::__construct

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

Constructs a set of HTML restrictions.

Parameters

array $elements: The allowed elements.

See also

\Drupal\filter\Plugin\FilterInterface::getHTMLRestrictions()

File

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

Class

HTMLRestrictions
Represents a set of HTML restrictions.

Namespace

Drupal\ckeditor5

Code

public function __construct(array $elements) {
  self::validateAllowedRestrictionsPhase1($elements);
  self::validateAllowedRestrictionsPhase2($elements);
  self::validateAllowedRestrictionsPhase3($elements);
  self::validateAllowedRestrictionsPhase4($elements);
  self::validateAllowedRestrictionsPhase5($elements);
  $this->elements = $elements;
  // Simplify based on the global attributes:
  // - `<p dir> <* dir>` must become `<p> <* dir>`
  // - `<p foo="b a"> <* foo="a b">` must become `<p> <* foo="a b">`
  // - `<p foo="a b c"> <* foo="a b">` must become `<p foo="c"> <* foo="a b">`
  // In other words: the restrictions on `<*>` remain untouched, but the
  // attributes and attribute values allowed by `<*>` should be omitted from
  // all other tags.
  // Note: `<*>` also allows specifying disallowed attributes, but no other
  // tags are allowed to do this. Consequently, simplification is only needed
  // if >=1 allowed attribute is present on `<*>`.
  if (count($elements) >= 2 && array_key_exists('*', $elements) && array_filter($elements['*'])) {
    // @see \Drupal\ckeditor5\HTMLRestrictions::validateAllowedRestrictionsPhase4()
    $globally_allowed_attribute_restrictions = array_filter($elements['*']);
    // Prepare to compare the restrictions of all tags with those on the
    // global attribute tag `<*>`.
    $original = [];
    $global = [];
    foreach ($elements as $tag => $restrictions) {
      // `<*>`'s attribute restrictions do not need to be compared.
      if ($tag === '*') {
        continue;
      }
      $original[$tag] = $restrictions;
      $global[$tag] = $globally_allowed_attribute_restrictions;
    }
    // The subset of attribute restrictions after diffing with those on `<*>`.
    $net_global_attribute_restrictions = (new self($original))->doDiff(new self($global))
      ->getAllowedElements(FALSE);
    // Update each tag's attribute restrictions to the subset.
    foreach ($elements as $tag => $restrictions) {
      // `<*>` remains untouched.
      if ($tag === '*') {
        continue;
      }
      $this->elements[$tag] = $net_global_attribute_restrictions[$tag] ?? FALSE;
    }
  }
}

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