function HTMLRestrictions::applyOperation

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

Applies an operation (difference/intersection/union) with wildcard support.

Parameters

\Drupal\ckeditor5\HTMLRestrictions $a: The first operand.

\Drupal\ckeditor5\HTMLRestrictions $b: The second operand.

string $operation_method_name: The name of the private method on this class to use as the operation.

Return value

\Drupal\ckeditor5\HTMLRestrictions The result of the operation.

2 calls to HTMLRestrictions::applyOperation()
HTMLRestrictions::diff in core/modules/ckeditor5/src/HTMLRestrictions.php
Computes difference of two HTML restrictions, with wildcard support.
HTMLRestrictions::intersect in core/modules/ckeditor5/src/HTMLRestrictions.php
Computes intersection of two HTML restrictions, with wildcard support.

File

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

Class

HTMLRestrictions
Represents a set of HTML restrictions.

Namespace

Drupal\ckeditor5

Code

private static function applyOperation(HTMLRestrictions $a, HTMLRestrictions $b, string $operation_method_name) : HTMLRestrictions {
    // 1. Operation applied to wildcard tags that exist in both operands.
    // For example: <$text-container id> in both operands.
    $a_wildcard = $a->getWildcardSubset();
    $b_wildcard = $b->getWildcardSubset();
    $wildcard_op_result = $a_wildcard->{$operation_method_name}($b_wildcard);
    // Early return if both operands contain only wildcard tags.
    if (count($a_wildcard->elements) === count($a->elements) && count($b_wildcard->elements) === count($b->elements)) {
        return $wildcard_op_result;
    }
    // 2. Operation applied with wildcard tags resolved into concrete tags.
    // For example: <p class="text-align-center"> in the first operand and
    // <$text-container class="text-align-center"> in the second
    // operand.
    $a_concrete = self::resolveWildcards($a);
    $b_concrete = self::resolveWildcards($b);
    $concrete_op_result = $a_concrete->{$operation_method_name}($b_concrete);
    // Using the PHP array union operator is safe because the two operation
    // result arrays ensure there is no overlap between the array keys.
    // @codingStandardsIgnoreStart
    assert(Inspector::assertAll(function ($t) {
        return self::isWildcardTag($t);
    }, array_keys($wildcard_op_result->elements)));
    assert(Inspector::assertAll(function ($t) {
        return !self::isWildcardTag($t);
    }, array_keys($concrete_op_result->elements)));
    // @codingStandardsIgnoreEnd
    return new self($concrete_op_result->elements + $wildcard_op_result->elements);
}

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