function StyleSensibleElementConstraintValidator::validate
Same name in other branches
- 10 core/modules/ckeditor5/src/Plugin/Validation/Constraint/StyleSensibleElementConstraintValidator.php \Drupal\ckeditor5\Plugin\Validation\Constraint\StyleSensibleElementConstraintValidator::validate()
- 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 35
Class
- StyleSensibleElementConstraintValidator
- Styles can only be specified for HTML5 tags and extra classes.
Namespace
Drupal\ckeditor5\Plugin\Validation\ConstraintCode
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();
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.