diff --git a/src/Property/Selector.php b/src/Property/Selector.php index ceed44a6..f1bbbcef 100644 --- a/src/Property/Selector.php +++ b/src/Property/Selector.php @@ -5,6 +5,7 @@ namespace Sabberworm\CSS\Property; use Sabberworm\CSS\OutputFormat; +use Sabberworm\CSS\Property\Selector\SpecificityCalculator; use Sabberworm\CSS\Renderable; /** @@ -13,43 +14,6 @@ */ class Selector implements Renderable { - /** - * regexp for specificity calculations - * - * @var string - */ - private const NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX = '/ - (\\.[\\w]+) # classes - | - \\[(\\w+) # attributes - | - (\\:( # pseudo classes - link|visited|active - |hover|focus - |lang - |target - |enabled|disabled|checked|indeterminate - |root - |nth-child|nth-last-child|nth-of-type|nth-last-of-type - |first-child|last-child|first-of-type|last-of-type - |only-child|only-of-type - |empty|contains - )) - /ix'; - - /** - * regexp for specificity calculations - * - * @var string - */ - private const ELEMENTS_AND_PSEUDO_ELEMENTS_RX = '/ - ((^|[\\s\\+\\>\\~]+)[\\w]+ # elements - | - \\:{1,2}( # pseudo-elements - after|before|first-letter|first-line|selection - )) - /ix'; - /** * regexp for specificity calculations * @@ -72,11 +36,6 @@ class Selector implements Renderable */ private $selector; - /** - * @var int|null - */ - private $specificity; - /** * @return bool * @@ -87,15 +46,9 @@ public static function isValid(string $selector) return \preg_match(static::SELECTOR_VALIDATION_RX, $selector); } - /** - * @param bool $calculateSpecificity @deprecated since V8.8.0, will be removed in V9.0.0 - */ - public function __construct(string $selector, bool $calculateSpecificity = false) + public function __construct(string $selector) { $this->setSelector($selector); - if ($calculateSpecificity) { - $this->getSpecificity(); - } } public function getSelector(): string @@ -106,7 +59,6 @@ public function getSelector(): string public function setSelector(string $selector): void { $this->selector = \trim($selector); - $this->specificity = null; } /** @@ -122,16 +74,7 @@ public function __toString(): string */ public function getSpecificity(): int { - if ($this->specificity === null) { - $a = 0; - /// @todo should exclude \# as well as "#" - $aMatches = null; - $b = \substr_count($this->selector, '#'); - $c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $this->selector, $aMatches); - $d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $this->selector, $aMatches); - $this->specificity = ($a * 1000) + ($b * 100) + ($c * 10) + $d; - } - return $this->specificity; + return SpecificityCalculator::calculate($this->selector); } public function render(OutputFormat $outputFormat): string diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 7a66f852..821c4338 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -246,26 +246,26 @@ public function unicodeRangeParsing(): void public function specificity(): void { $document = self::parsedStructureForFile('specificity'); - self::assertEquals([new Selector('#test .help', true)], $document->getSelectorsBySpecificity('> 100')); + self::assertEquals([new Selector('#test .help')], $document->getSelectorsBySpecificity('> 100')); self::assertEquals( - [new Selector('#test .help', true), new Selector('#file', true)], + [new Selector('#test .help'), new Selector('#file')], $document->getSelectorsBySpecificity('>= 100') ); - self::assertEquals([new Selector('#file', true)], $document->getSelectorsBySpecificity('=== 100')); - self::assertEquals([new Selector('#file', true)], $document->getSelectorsBySpecificity('== 100')); + self::assertEquals([new Selector('#file')], $document->getSelectorsBySpecificity('=== 100')); + self::assertEquals([new Selector('#file')], $document->getSelectorsBySpecificity('== 100')); self::assertEquals([ - new Selector('#file', true), - new Selector('.help:hover', true), - new Selector('li.green', true), - new Selector('ol li::before', true), + new Selector('#file'), + new Selector('.help:hover'), + new Selector('li.green'), + new Selector('ol li::before'), ], $document->getSelectorsBySpecificity('<= 100')); self::assertEquals([ - new Selector('.help:hover', true), - new Selector('li.green', true), - new Selector('ol li::before', true), + new Selector('.help:hover'), + new Selector('li.green'), + new Selector('ol li::before'), ], $document->getSelectorsBySpecificity('< 100')); - self::assertEquals([new Selector('li.green', true)], $document->getSelectorsBySpecificity('11')); - self::assertEquals([new Selector('ol li::before', true)], $document->getSelectorsBySpecificity('3')); + self::assertEquals([new Selector('li.green')], $document->getSelectorsBySpecificity('11')); + self::assertEquals([new Selector('ol li::before')], $document->getSelectorsBySpecificity('3')); } /**