function HTMLRestrictions::validateAllowedRestrictionsPhase1

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

Validates allowed elements — phase 1: shape of keys.

Confirms each of the top-level array keys:

  • Is a string
  • Does not contain leading or trailing whitespace
  • Is a tag name, not a tag, e.g. `div` not `<div>`
  • Is a valid HTML tag name (or the global attribute `*` tag).

Parameters

array $elements: The allowed elements.

Throws

\InvalidArgumentException

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

File

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

Class

HTMLRestrictions
Represents a set of HTML restrictions.

Namespace

Drupal\ckeditor5

Code

private static function validateAllowedRestrictionsPhase1(array $elements) : void {
    if (!is_array($elements) || !Inspector::assertAllStrings(array_keys($elements))) {
        throw new \InvalidArgumentException('An array of key-value pairs must be provided, with HTML tag names as keys.');
    }
    foreach (array_keys($elements) as $html_tag_name) {
        if (trim($html_tag_name) !== $html_tag_name) {
            throw new \InvalidArgumentException(sprintf('The "%s" HTML tag contains trailing or leading whitespace.', $html_tag_name));
        }
        if ($html_tag_name[0] === '<' || $html_tag_name[-1] === '>') {
            throw new \InvalidArgumentException(sprintf('"%s" is not a HTML tag name, it is an actual HTML tag. Omit the angular brackets.', $html_tag_name));
        }
        if (self::isWildcardTag($html_tag_name)) {
            continue;
        }
        // Special case: the global attribute `*` HTML tag.
        // @see https://html.spec.whatwg.org/multipage/dom.html#global-attributes
        // @see validateAllowedRestrictionsPhase2()
        // @see validateAllowedRestrictionsPhase4()
        // @see validateAllowedRestrictionsPhase5()
        if ($html_tag_name === '*') {
            continue;
        }
        // HTML elements must have a valid tag name.
        // @see https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-name
        // @see https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
        if (!preg_match('/^[a-z][0-9a-z\\-]*$/', strtolower($html_tag_name))) {
            throw new \InvalidArgumentException(sprintf('"%s" is not a valid HTML tag name.', $html_tag_name));
        }
    }
}

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