function HTMLRestrictions::merge

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

Computes set union of two HTML restrictions, with wildcard support.

Parameters

\Drupal\ckeditor5\HTMLRestrictions $other: The HTML restrictions to compare to.

Return value

\Drupal\ckeditor5\HTMLRestrictions Returns a new HTML restrictions value object with all the elements that are either allowed in $this or in $other.

File

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

Class

HTMLRestrictions
Represents a set of HTML restrictions.

Namespace

Drupal\ckeditor5

Code

public function merge(HTMLRestrictions $other) : HTMLRestrictions {
    $union = self::mergeAllowedElementsLevel($this->elements, $other->elements);
    // Special case: wildcard attributes, and the ability to define restrictions
    // for all concrete attributes matching them using:
    // - prefix wildcard, f.e. `*-foo`
    // - infix wildcard, f.e. `*-entity-*`
    // - suffix wildcard, f.e. `data-*`, to match `data-foo`, `data-bar`, etc.
    foreach ($union as $tag => $tag_config) {
        // If there are no per-attribute restrictions for this tag, then no
        // wildcard attribute postprocessing is needed.
        if (!is_array($tag_config)) {
            continue;
        }
        $wildcard_attributes = array_filter(array_keys($tag_config), [
            __CLASS__,
            'isWildcardAttributeName',
        ]);
        foreach ($wildcard_attributes as $wildcard_attribute_name) {
            $regex = self::getRegExForWildCardAttributeName($wildcard_attribute_name);
            foreach ($tag_config as $html_tag_attribute_name => $html_tag_attribute_restrictions) {
                // The wildcard attribute restriction itself must be kept.
                if ($html_tag_attribute_name === $wildcard_attribute_name) {
                    continue;
                }
                // If a concrete attribute restriction (f.e. `data-foo`, `data-bar`,
                // etc.) exists whose attribute value restrictions are the same as the
                // wildcard attribute value restrictions (f.e. `data-*`), we must
                // explicitly drop the concrete attribute restriction in favor of the
                // wildcard one.
                if ($html_tag_attribute_restrictions === $tag_config[$wildcard_attribute_name] && preg_match($regex, $html_tag_attribute_name) === 1) {
                    unset($tag_config[$html_tag_attribute_name]);
                }
            }
            $union[$tag] = $tag_config;
        }
    }
    return new self($union);
}

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