Skip to content

Commit 8570e6b

Browse files
committed
Use the new utility class
1 parent 264cfd2 commit 8570e6b

File tree

1 file changed

+2
-45
lines changed

1 file changed

+2
-45
lines changed

src/Property/Selector.php

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Sabberworm\CSS\Property;
66

77
use Sabberworm\CSS\OutputFormat;
8+
use Sabberworm\CSS\Property\Selector\SpecificityCalculator;
89
use Sabberworm\CSS\Renderable;
910

1011
/**
@@ -13,43 +14,6 @@
1314
*/
1415
class Selector implements Renderable
1516
{
16-
/**
17-
* regexp for specificity calculations
18-
*
19-
* @var string
20-
*/
21-
private const NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX = '/
22-
(\\.[\\w]+) # classes
23-
|
24-
\\[(\\w+) # attributes
25-
|
26-
(\\:( # pseudo classes
27-
link|visited|active
28-
|hover|focus
29-
|lang
30-
|target
31-
|enabled|disabled|checked|indeterminate
32-
|root
33-
|nth-child|nth-last-child|nth-of-type|nth-last-of-type
34-
|first-child|last-child|first-of-type|last-of-type
35-
|only-child|only-of-type
36-
|empty|contains
37-
))
38-
/ix';
39-
40-
/**
41-
* regexp for specificity calculations
42-
*
43-
* @var string
44-
*/
45-
private const ELEMENTS_AND_PSEUDO_ELEMENTS_RX = '/
46-
((^|[\\s\\+\\>\\~]+)[\\w]+ # elements
47-
|
48-
\\:{1,2}( # pseudo-elements
49-
after|before|first-letter|first-line|selection
50-
))
51-
/ix';
52-
5317
/**
5418
* regexp for specificity calculations
5519
*
@@ -110,14 +74,7 @@ public function __toString(): string
11074
*/
11175
public function getSpecificity(): int
11276
{
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;
77+
return SpecificityCalculator::calculate($this->selector);
12178
}
12279

12380
public function render(OutputFormat $outputFormat): string

0 commit comments

Comments
 (0)