function HTMLRestrictions::validateAllowedRestrictionsPhase2

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

Validates allowed elements — phase 2: shape of values.

Parameters

array $elements: The allowed elements.

Throws

\InvalidArgumentException

1 call to HTMLRestrictions::validateAllowedRestrictionsPhase2()
HTMLRestrictions::__construct in core/modules/ckeditor5/src/HTMLRestrictions.php
Constructs a set of HTML restrictions.

File

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

Class

HTMLRestrictions
Represents a set of HTML restrictions.

Namespace

Drupal\ckeditor5

Code

private static function validateAllowedRestrictionsPhase2(array $elements) : void {
    foreach ($elements as $html_tag_name => $html_tag_restrictions) {
        // The global attribute `*` HTML tag is a special case: it allows
        // specifying specific attributes that are allowed on all tags (f.e.
        // `lang`) or disallowed on all tags (f.e. `style`) as translations and
        // security are concerns orthogonal to the configured HTML restrictions
        // of a text format.
        // @see https://html.spec.whatwg.org/multipage/dom.html#global-attributes
        // @see validateAllowedRestrictionsPhase4()
        // @see validateAllowedRestrictionsPhase5()
        if ($html_tag_name === '*' && !is_array($html_tag_restrictions)) {
            throw new \InvalidArgumentException(sprintf('The value for the special "*" global attribute HTML tag must be an array of attribute restrictions.'));
        }
        // The value must be either a boolean (FALSE means no attributes are
        // allowed, TRUE means all attributes are allowed), or an array of allowed
        // The value must be either:
        // - An array of allowed attribute names OR
        // - A boolean (where FALSE means no attributes are allowed, and TRUE
        //   means all attributes are allowed).
        if (is_bool($html_tag_restrictions)) {
            continue;
        }
        if (!is_array($html_tag_restrictions)) {
            throw new \InvalidArgumentException(sprintf('The value for the "%s" HTML tag is neither a boolean nor an array of attribute restrictions.', $html_tag_name));
        }
        if ($html_tag_restrictions === []) {
            throw new \InvalidArgumentException(sprintf('The value for the "%s" HTML tag is an empty array. This is not permitted, specify FALSE instead to indicate no attributes are allowed. Otherwise, list allowed attributes.', $html_tag_name));
        }
    }
}

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