function CssSelectorConstraintValidator::validate

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Theme/Plugin/Validation/Constraint/CssSelectorConstraintValidator.php \Drupal\Core\Theme\Plugin\Validation\Constraint\CssSelectorConstraintValidator::validate()

File

core/lib/Drupal/Core/Theme/Plugin/Validation/Constraint/CssSelectorConstraintValidator.php, line 20

Class

CssSelectorConstraintValidator
Validates the CssSelector constraint.

Namespace

Drupal\Core\Theme\Plugin\Validation\Constraint

Code

public function validate(mixed $value, Constraint $constraint) : void {
  assert($constraint instanceof CssSelectorConstraint);
  if (!is_string($value)) {
    throw new UnexpectedValueException($value, 'string');
  }
  // In case it is a key that is tested.
  $testedValue = str_replace(DesignTokenInterface::DOT_CONVERSION_CHARACTER, '.', $value);
  // The goal of this regex is not validate the correctness of a CSS selector
  // but to check the string can only be a CSS selector, correct or not, by
  // prohibiting a full selector + property + value combination.
  if (preg_match('/[{};]/', $testedValue)) {
    $this->context
      ->addViolation($constraint->message, [
      '@scope' => $value,
    ]);
  }
}

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