class CssSelectorConstraintValidator

Validates the CssSelector constraint.

Hierarchy

Expanded class hierarchy of CssSelectorConstraintValidator

1 file declares its use of CssSelectorConstraintValidator
CssSelectorConstraintValidatorTest.php in core/tests/Drupal/KernelTests/Core/Theme/DesignToken/CssSelectorConstraintValidatorTest.php

File

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

Namespace

Drupal\Core\Theme\Plugin\Validation\Constraint
View source
class CssSelectorConstraintValidator extends ConstraintValidator {
  
  /**
   * {@inheritdoc}
   */
  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.