Skip to content

[TASK] Always calculate selector specificity on demand #1028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 3 additions & 60 deletions src/Property/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sabberworm\CSS\Property;

use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\Property\Selector\SpecificityCalculator;
use Sabberworm\CSS\Renderable;

/**
Expand All @@ -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
*
Expand All @@ -72,11 +36,6 @@ class Selector implements Renderable
*/
private $selector;

/**
* @var int|null
*/
private $specificity;

/**
* @return bool
*
Expand All @@ -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
Expand All @@ -106,7 +59,6 @@ public function getSelector(): string
public function setSelector(string $selector): void
{
$this->selector = \trim($selector);
$this->specificity = null;
}

/**
Expand All @@ -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
Expand Down
26 changes: 13 additions & 13 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand Down