Skip to content

Commit 412a2df

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 7ef82db commit 412a2df

File tree

2 files changed

+23
-36
lines changed

2 files changed

+23
-36
lines changed

src/Property/Selector.php

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ class Selector
6969
*/
7070
private $selector;
7171

72-
/**
73-
* @var int|null
74-
*/
75-
private $specificity;
76-
7772
/**
7873
* @param string $selector
7974
*
@@ -88,14 +83,10 @@ public static function isValid($selector)
8883

8984
/**
9085
* @param string $selector
91-
* @param bool $calculateSpecificity @deprecated since V8.8.0, will be removed in V9.0.0
9286
*/
93-
public function __construct($selector, $calculateSpecificity = false)
87+
public function __construct($selector)
9488
{
9589
$this->setSelector($selector);
96-
if ($calculateSpecificity) {
97-
$this->getSpecificity();
98-
}
9990
}
10091

10192
/**
@@ -112,7 +103,6 @@ public function getSelector()
112103
public function setSelector($selector): void
113104
{
114105
$this->selector = \trim($selector);
115-
$this->specificity = null;
116106
}
117107

118108
/**
@@ -124,19 +114,16 @@ public function __toString(): string
124114
}
125115

126116
/**
127-
* @return int
117+
* @return int<0, max>
128118
*/
129-
public function getSpecificity()
119+
public function getSpecificity(): int
130120
{
131-
if ($this->specificity === null) {
132-
$a = 0;
133-
/// @todo should exclude \# as well as "#"
134-
$aMatches = null;
135-
$b = \substr_count($this->selector, '#');
136-
$c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $this->selector, $aMatches);
137-
$d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $this->selector, $aMatches);
138-
$this->specificity = ($a * 1000) + ($b * 100) + ($c * 10) + $d;
139-
}
140-
return $this->specificity;
121+
$a = 0;
122+
/// @todo should exclude \# as well as "#"
123+
$aMatches = null;
124+
$b = \substr_count($this->selector, '#');
125+
$c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $this->selector, $aMatches);
126+
$d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $this->selector, $aMatches);
127+
return ($a * 1000) + ($b * 100) + ($c * 10) + $d;
141128
}
142129
}

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)