function HTMLRestrictions::fromObjectWithHtmlRestrictions

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

Constructs a set of HTML restrictions matching the given object.

Note: there is no interface for the ::getHTMLRestrictions() method that both text filter plugins and the text format configuration entity type implement. To avoid duplicating this logic, this private helper method exists: to simplify the two public static methods that each accept one of those two interfaces.

Parameters

\Drupal\filter\Plugin\FilterInterface|\Drupal\filter\FilterFormatInterface $object: A text format or filter plugin instance to construct a HTML restrictions object for.

Return value

\Drupal\ckeditor5\HTMLRestrictions

See also

::fromFilterPluginInstance()

::fromTextFormat()

2 calls to HTMLRestrictions::fromObjectWithHtmlRestrictions()
HTMLRestrictions::fromFilterPluginInstance in core/modules/ckeditor5/src/HTMLRestrictions.php
Constructs a set of HTML restrictions matching the given text format.
HTMLRestrictions::fromTextFormat in core/modules/ckeditor5/src/HTMLRestrictions.php
Constructs a set of HTML restrictions matching the given text format.

File

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

Class

HTMLRestrictions
Represents a set of HTML restrictions.

Namespace

Drupal\ckeditor5

Code

private static function fromObjectWithHtmlRestrictions(object $object) : HTMLRestrictions {
    if (!method_exists($object, 'getHTMLRestrictions')) {
        throw new \InvalidArgumentException();
    }
    $restrictions = $object->getHTMLRestrictions();
    if ($restrictions === FALSE || $restrictions === []) {
        return self::unrestricted();
    }
    // When allowing all tags on an attribute, transform FilterHtml output from
    // ['tag' => ['*'=> TRUE]] to ['tag' => TRUE]
    $allowed = $restrictions['allowed'];
    foreach ($allowed as $element => $attributes) {
        if (is_array($attributes) && isset($attributes['*']) && $attributes['*'] === TRUE) {
            $allowed[$element] = TRUE;
        }
    }
    // FilterHtml accepts configuration for `allowed_html` that it will not
    // actually apply. In other words: it allows for meaningless configuration.
    // HTMLRestrictions strictly forbids tags overriding globally disallowed
    // attributes; it considers these conflicting statements. Since FilterHtml
    // will not apply these anyway, remove them from $allowed prior to
    // constructing a HTMLRestrictions object:
    // - `<tag style foo>` will become `<tag foo>` since the `style` attribute
    //   is globally disallowed by FilterHtml
    // - `<tag bar on*>` will become `<tag bar>` since the `on*` attribute is
    //   globally disallowed by FilterHtml
    // - `<tag ontouch baz>` will become `<tag baz>` since the `on*` attribute
    //   is globally disallowed by FilterHtml
    // @see ::validateAllowedRestrictionsPhase5()
    // @see \Drupal\filter\Plugin\Filter\FilterHtml::process()
    // @see \Drupal\filter\Plugin\Filter\FilterHtml::getHTMLRestrictions()
    $conflict = self::findElementsOverridingGloballyDisallowedAttributes($allowed);
    if ($conflict) {
        [
            ,
            $elements_overriding_globally_disallowed_attributes,
        ] = $conflict;
        foreach ($elements_overriding_globally_disallowed_attributes as $element => $attributes) {
            foreach (array_keys($attributes) as $attribute_name) {
                unset($allowed[$element][$attribute_name]);
            }
            if ($allowed[$element] === []) {
                $allowed[$element] = FALSE;
            }
        }
    }
    return new self($allowed);
}

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