Skip to content

Commit b7d1926

Browse files
committed
[TASK] Always calculate selector specificity on demand
This avoids additional state that makes it hard to compare selectors for equality. Also, this will improve performance for cases where the specificity is not needed (but will slightly worsen performance where the specificity is queried multiple times on the same selector instance). Closes #1021
1 parent f912e71 commit b7d1926

File tree

2 files changed

+22
-36
lines changed

2 files changed

+22
-36
lines changed

src/Property/Selector.php

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ class Selector implements Renderable
7272
*/
7373
private $selector;
7474

75-
/**
76-
* @var int|null
77-
*/
78-
private $specificity;
79-
8075
/**
8176
* @return bool
8277
*
@@ -87,15 +82,9 @@ public static function isValid(string $selector)
8782
return \preg_match(static::SELECTOR_VALIDATION_RX, $selector);
8883
}
8984

90-
/**
91-
* @param bool $calculateSpecificity @deprecated since V8.8.0, will be removed in V9.0.0
92-
*/
93-
public function __construct(string $selector, bool $calculateSpecificity = false)
85+
public function __construct(string $selector)
9486
{
9587
$this->setSelector($selector);
96-
if ($calculateSpecificity) {
97-
$this->getSpecificity();
98-
}
9988
}
10089

10190
public function getSelector(): string
@@ -106,7 +95,6 @@ public function getSelector(): string
10695
public function setSelector(string $selector): void
10796
{
10897
$this->selector = \trim($selector);
109-
$this->specificity = null;
11098
}
11199

112100
/**
@@ -122,16 +110,14 @@ public function __toString(): string
122110
*/
123111
public function getSpecificity(): int
124112
{
125-
if ($this->specificity === null) {
126-
$a = 0;
127-
/// @todo should exclude \# as well as "#"
128-
$aMatches = null;
129-
$b = \substr_count($this->selector, '#');
130-
$c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $this->selector, $aMatches);
131-
$d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $this->selector, $aMatches);
132-
$this->specificity = ($a * 1000) + ($b * 100) + ($c * 10) + $d;
133-
}
134-
return $this->specificity;
113+
$a = 0;
114+
// @todo should exclude \# as well as "#"
115+
$aMatches = null;
116+
$b = \substr_count($this->selector, '#');
117+
$c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $this->selector, $aMatches);
118+
$d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $this->selector, $aMatches);
119+
120+
return ($a * 1000) + ($b * 100) + ($c * 10) + $d;
135121
}
136122

137123
public function render(OutputFormat $outputFormat): string

tests/ParserTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,26 +246,26 @@ public function unicodeRangeParsing(): void
246246
public function specificity(): void
247247
{
248248
$document = self::parsedStructureForFile('specificity');
249-
self::assertEquals([new Selector('#test .help', true)], $document->getSelectorsBySpecificity('> 100'));
249+
self::assertEquals([new Selector('#test .help')], $document->getSelectorsBySpecificity('> 100'));
250250
self::assertEquals(
251-
[new Selector('#test .help', true), new Selector('#file', true)],
251+
[new Selector('#test .help'), new Selector('#file')],
252252
$document->getSelectorsBySpecificity('>= 100')
253253
);
254-
self::assertEquals([new Selector('#file', true)], $document->getSelectorsBySpecificity('=== 100'));
255-
self::assertEquals([new Selector('#file', true)], $document->getSelectorsBySpecificity('== 100'));
254+
self::assertEquals([new Selector('#file')], $document->getSelectorsBySpecificity('=== 100'));
255+
self::assertEquals([new Selector('#file')], $document->getSelectorsBySpecificity('== 100'));
256256
self::assertEquals([
257-
new Selector('#file', true),
258-
new Selector('.help:hover', true),
259-
new Selector('li.green', true),
260-
new Selector('ol li::before', true),
257+
new Selector('#file'),
258+
new Selector('.help:hover'),
259+
new Selector('li.green'),
260+
new Selector('ol li::before'),
261261
], $document->getSelectorsBySpecificity('<= 100'));
262262
self::assertEquals([
263-
new Selector('.help:hover', true),
264-
new Selector('li.green', true),
265-
new Selector('ol li::before', true),
263+
new Selector('.help:hover'),
264+
new Selector('li.green'),
265+
new Selector('ol li::before'),
266266
], $document->getSelectorsBySpecificity('< 100'));
267-
self::assertEquals([new Selector('li.green', true)], $document->getSelectorsBySpecificity('11'));
268-
self::assertEquals([new Selector('ol li::before', true)], $document->getSelectorsBySpecificity('3'));
267+
self::assertEquals([new Selector('li.green')], $document->getSelectorsBySpecificity('11'));
268+
self::assertEquals([new Selector('ol li::before')], $document->getSelectorsBySpecificity('3'));
269269
}
270270

271271
/**

0 commit comments

Comments
 (0)