function StyleSensibleElementConstraintValidator::validate

Same name in other branches
  1. 9 core/modules/ckeditor5/src/Plugin/Validation/Constraint/StyleSensibleElementConstraintValidator.php \Drupal\ckeditor5\Plugin\Validation\Constraint\StyleSensibleElementConstraintValidator::validate()
  2. 11.x core/modules/ckeditor5/src/Plugin/Validation/Constraint/StyleSensibleElementConstraintValidator.php \Drupal\ckeditor5\Plugin\Validation\Constraint\StyleSensibleElementConstraintValidator::validate()

Throws

\Symfony\Component\Validator\Exception\UnexpectedTypeException Thrown when the given constraint is not supported by this validator.

File

core/modules/ckeditor5/src/Plugin/Validation/Constraint/StyleSensibleElementConstraintValidator.php, line 70

Class

StyleSensibleElementConstraintValidator
Styles can only be specified for HTML5 tags and extra classes.

Namespace

Drupal\ckeditor5\Plugin\Validation\Constraint

Code

public function validate($element, Constraint $constraint) {
    if (!$constraint instanceof StyleSensibleElementConstraint) {
        throw new UnexpectedTypeException($constraint, StyleSensibleElementConstraint::class);
    }
    // The preceding constraints (in this case: CKEditor5Element) must be valid.
    if ($this->hasViolationsForPrecedingConstraints($constraint)) {
        return;
    }
    $text_editor = $this->createTextEditorObjectFromContext();
    // The single tag for which a style is specified, which we are checking now.
    $style_element = HTMLRestrictions::fromString($element);
    assert(count($style_element->getAllowedElements()) === 1);
    [
        $tag,
        $classes,
    ] = Style::getTagAndClasses($style_element);
    // Ensure the tag is in the range supported by the Style plugin.
    $superset = HTMLRestrictions::fromString('<$any-html5-element class>');
    $supported_range = $superset->merge($style_element->extractPlainTagsSubset());
    if (!$style_element->diff($supported_range)
        ->allowsNothing()) {
        $this->context
            ->buildViolation($constraint->nonHtml5TagMessage)
            ->setParameter('@tag', sprintf("<%s>", $tag))
            ->addViolation();
        return;
    }
    // Get the list of tags enabled by every plugin other than Style.
    $other_enabled_plugins = $this->getOtherEnabledPlugins($text_editor, 'ckeditor5_style');
    $enableable_disabled_plugins = $this->getEnableableDisabledPlugins($text_editor);
    $other_enabled_plugin_elements = new HTMLRestrictions($this->pluginManager
        ->getProvidedElements(array_keys($other_enabled_plugins), $text_editor, FALSE));
    $disabled_plugin_elements = new HTMLRestrictions($this->pluginManager
        ->getProvidedElements(array_keys($enableable_disabled_plugins), $text_editor, FALSE));
    // Next, validate that the classes specified for this style are not
    // supported by an enabled plugin.
    if (self::intersectionWithClasses($style_element, $other_enabled_plugin_elements)) {
        $this->context
            ->buildViolation($constraint->conflictingEnabledPluginMessage)
            ->setParameter('@tag', sprintf("<%s>", $tag))
            ->setParameter('@classes', implode(", ", $classes))
            ->setParameter('%plugin', $this->findStyleConflictingPluginLabel($style_element))
            ->addViolation();
    }
    elseif (self::intersectionWithClasses($style_element, $disabled_plugin_elements)) {
        $this->context
            ->buildViolation($constraint->conflictingDisabledPluginMessage)
            ->setParameter('@tag', sprintf("<%s>", $tag))
            ->setParameter('@classes', implode(", ", $classes))
            ->setParameter('%plugin', $this->findStyleConflictingPluginLabel($style_element))
            ->addViolation();
    }
    // Finally, while the configuration is technically valid if this point was
    // reached, there are some known compatibility issues. Inform the user that
    // for that reason, this configuration must be considered invalid.
    $unsupported = $style_element->intersect(HTMLRestrictions::fromString(implode(' ', static::KNOWN_UNSUPPORTED_TAGS)));
    if (!$unsupported->allowsNothing()) {
        $this->context
            ->buildViolation($constraint->unsupportedTagMessage)
            ->setParameter('@tag', sprintf("<%s>", $tag))
            ->addViolation();
    }
}

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