|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CssLint\CharLinter; |
| 6 | + |
| 7 | +use CssLint\LintContext; |
| 8 | +use CssLint\LintContextName; |
| 9 | +use CssLint\Properties; |
| 10 | + |
| 11 | +class PropertyCharLinter implements CharLinter |
| 12 | +{ |
| 13 | + public function __construct( |
| 14 | + protected readonly Properties $cssLintProperties, |
| 15 | + ) {} |
| 16 | + |
| 17 | + /** |
| 18 | + * Performs lint for a given char, check property part |
| 19 | + * @return boolean|null : true if the process should continue, else false, null if this char is not about selector |
| 20 | + */ |
| 21 | + public function lintChar(string $charValue, LintContext $lintContext): ?bool |
| 22 | + { |
| 23 | + if (is_bool($lintPropertyNameChar = $this->lintPropertyNameChar($charValue, $lintContext))) { |
| 24 | + return $lintPropertyNameChar; |
| 25 | + } |
| 26 | + |
| 27 | + if (is_bool($lintPropertyContentChar = $this->lintPropertyContentChar($charValue, $lintContext))) { |
| 28 | + return $lintPropertyContentChar; |
| 29 | + } |
| 30 | + |
| 31 | + return null; |
| 32 | + } |
| 33 | + /** |
| 34 | + * Performs lint for a given char, check property name part |
| 35 | + * @return bool|null : true if the process should continue, else false, null if this char is not a property name |
| 36 | + */ |
| 37 | + protected function lintPropertyNameChar(string $charValue, LintContext $lintContext): ?bool |
| 38 | + { |
| 39 | + if (!$lintContext->assertCurrentContext(LintContextName::CONTEXT_PROPERTY_NAME)) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + if ($charValue === ':') { |
| 44 | + $propertyName = trim($lintContext->getCurrentContent()); |
| 45 | + |
| 46 | + // Ignore CSS variables (names starting with --) |
| 47 | + if (str_starts_with($propertyName, '--')) { |
| 48 | + $lintContext->setCurrentContext(LintContextName::CONTEXT_PROPERTY_CONTENT); |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + // Check if property name exists |
| 53 | + if (!$this->cssLintProperties->propertyExists($propertyName)) { |
| 54 | + $lintContext->addError('Unknown CSS property "' . $propertyName . '"'); |
| 55 | + } |
| 56 | + |
| 57 | + $lintContext->setCurrentContext(LintContextName::CONTEXT_PROPERTY_CONTENT); |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + $lintContext->appendCurrentContent($charValue); |
| 62 | + |
| 63 | + if ($charValue === ' ') { |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + if (in_array(preg_match('/[-a-zA-Z0-9]+/', $charValue), [0, false], true)) { |
| 68 | + $lintContext->addError('Unexpected property name token "' . $charValue . '"'); |
| 69 | + } |
| 70 | + |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Performs lint for a given char, check property content part |
| 76 | + * @return bool|null : true if the process should continue, else false, null if this char is not a property content |
| 77 | + */ |
| 78 | + protected function lintPropertyContentChar(string $charValue, LintContext $lintContext): ?bool |
| 79 | + { |
| 80 | + if (!$lintContext->assertCurrentContext(LintContextName::CONTEXT_PROPERTY_CONTENT)) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + $lintContext->appendCurrentContent($charValue); |
| 85 | + |
| 86 | + // End of the property content |
| 87 | + if ($charValue === ';') { |
| 88 | + // Check if the ";" is not quoted |
| 89 | + $contextContent = $lintContext->getCurrentContent(); |
| 90 | + if ((substr_count($contextContent, '"') & 1) === 0 && (substr_count($contextContent, "'") & 1) === 0) { |
| 91 | + $lintContext->setCurrentContext(LintContextName::CONTEXT_SELECTOR_CONTENT); |
| 92 | + } |
| 93 | + |
| 94 | + if (trim($contextContent) !== '' && trim($contextContent) !== '0') { |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + $lintContext->addError('Property cannot be empty'); |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + // No property content validation |
| 103 | + return true; |
| 104 | + } |
| 105 | +} |
0 commit comments