function HTMLRestrictions::findElementsOverridingGloballyDisallowedAttributes

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

Finds elements overriding globally disallowed attributes.

Parameters

array $elements: The allowed elements.

Return value

null|array NULL if no conflict is found, an array containing otherwise, containing:

  • the globally disallowed attribute restrictions
  • the elements overriding globally disallowed attributes
2 calls to HTMLRestrictions::findElementsOverridingGloballyDisallowedAttributes()
HTMLRestrictions::fromObjectWithHtmlRestrictions in core/modules/ckeditor5/src/HTMLRestrictions.php
Constructs a set of HTML restrictions matching the given object.
HTMLRestrictions::validateAllowedRestrictionsPhase5 in core/modules/ckeditor5/src/HTMLRestrictions.php
Validates allowed elements — phase 5: disallowed attribute overrides.

File

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

Class

HTMLRestrictions
Represents a set of HTML restrictions.

Namespace

Drupal\ckeditor5

Code

private static function findElementsOverridingGloballyDisallowedAttributes(array $elements) : ?array {
    // Find the globally disallowed attributes.
    // For example: `['*' => ['style' => FALSE, 'foo' => TRUE, 'bar' => FALSE]`
    // has two globally disallowed attributes, the code below will extract
    // `['*' => ['style' => FALSE, 'bar' => FALSE']]`.
    $globally_disallowed_attribute_restrictions = !array_key_exists('*', $elements) ? [] : array_filter($elements['*'], function ($global_attribute_restrictions) : bool {
        return $global_attribute_restrictions === FALSE;
    });
    if (empty($globally_disallowed_attribute_restrictions)) {
        // No conflict possible.
        return NULL;
    }
    // The elements which could potentially have a conflicting override.
    $elements_with_attribute_level_restrictions = array_filter($elements, function ($attribute_restrictions, string $attribute_name) : bool {
        return is_array($attribute_restrictions) && $attribute_name !== '*';
    }, ARRAY_FILTER_USE_BOTH);
    if (empty($elements_with_attribute_level_restrictions)) {
        // No conflict possible.
        return NULL;
    }
    // Construct a HTMLRestrictions object containing just the elements that are
    // potentially overriding globally disallowed attributes.
    // For example: `['p' => ['style' => TRUE]]`.
    $potentially_overriding = new self($elements_with_attribute_level_restrictions);
    // Construct a HTMLRestrictions object that contains the globally disallowed
    // attribute restrictions, but pretends they are allowed. This allows using
    // ::intersect() to detect a conflict.
    $conflicting_restrictions = new self(array_fill_keys(array_keys($elements_with_attribute_level_restrictions), array_fill_keys(array_keys($globally_disallowed_attribute_restrictions), TRUE)));
    // When there is a non-empty intersection at the attribute level, an
    // override of a globally disallowed attribute was found.
    $conflict = $potentially_overriding->intersect($conflicting_restrictions);
    $elements_overriding_globally_disallowed_attributes = array_filter($conflict->getAllowedElements());
    // No conflict found.
    if (empty($elements_overriding_globally_disallowed_attributes)) {
        return NULL;
    }
    return [
        $globally_disallowed_attribute_restrictions,
        $elements_overriding_globally_disallowed_attributes,
    ];
}

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