|
5 | 5 | namespace Sabberworm\CSS\Property;
|
6 | 6 |
|
7 | 7 | use Sabberworm\CSS\OutputFormat;
|
| 8 | +use Sabberworm\CSS\Property\Selector\SpecificityCalculator; |
8 | 9 | use Sabberworm\CSS\Renderable;
|
9 | 10 |
|
10 | 11 | /**
|
|
13 | 14 | */
|
14 | 15 | class Selector implements Renderable
|
15 | 16 | {
|
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 |
| - |
53 | 17 | /**
|
54 | 18 | * regexp for specificity calculations
|
55 | 19 | *
|
@@ -110,14 +74,7 @@ public function __toString(): string
|
110 | 74 | */
|
111 | 75 | public function getSpecificity(): int
|
112 | 76 | {
|
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); |
121 | 78 | }
|
122 | 79 |
|
123 | 80 | public function render(OutputFormat $outputFormat): string
|
|
0 commit comments