• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MyIntervals / PHP-CSS-Parser / 13603528799

01 Mar 2025 09:45AM UTC coverage: 55.69% (-0.06%) from 55.753%
13603528799

Pull #1028

github

web-flow
Merge 2d604784c into 7ef82db15
Pull Request #1028: [TASK] Stop caching the selector specificity

1062 of 1907 relevant lines covered (55.69%)

12.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.47
/src/Property/Selector.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS\Property;
6

7
/**
8
 * Class representing a single CSS selector. Selectors have to be split by the comma prior to being passed into this
9
 * class.
10
 */
11
class Selector
12
{
13
    /**
14
     * regexp for specificity calculations
15
     *
16
     * @var string
17
     */
18
    private const NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX = '/
19
        (\\.[\\w]+)                   # classes
20
        |
21
        \\[(\\w+)                     # attributes
22
        |
23
        (\\:(                         # pseudo classes
24
            link|visited|active
25
            |hover|focus
26
            |lang
27
            |target
28
            |enabled|disabled|checked|indeterminate
29
            |root
30
            |nth-child|nth-last-child|nth-of-type|nth-last-of-type
31
            |first-child|last-child|first-of-type|last-of-type
32
            |only-child|only-of-type
33
            |empty|contains
34
        ))
35
        /ix';
36

37
    /**
38
     * regexp for specificity calculations
39
     *
40
     * @var string
41
     */
42
    private const ELEMENTS_AND_PSEUDO_ELEMENTS_RX = '/
43
        ((^|[\\s\\+\\>\\~]+)[\\w]+   # elements
44
        |
45
        \\:{1,2}(                    # pseudo-elements
46
            after|before|first-letter|first-line|selection
47
        ))
48
        /ix';
49

50
    /**
51
     * regexp for specificity calculations
52
     *
53
     * @var string
54
     *
55
     * @internal since 8.5.2
56
     */
57
    public const SELECTOR_VALIDATION_RX = '/
58
        ^(
59
            (?:
60
                [a-zA-Z0-9\\x{00A0}-\\x{FFFF}_^$|*="\'~\\[\\]()\\-\\s\\.:#+>]* # any sequence of valid unescaped characters
61
                (?:\\\\.)?                                                     # a single escaped character
62
                (?:([\'"]).*?(?<!\\\\)\\2)?                                    # a quoted text like [id="example"]
63
            )*
64
        )$
65
        /ux';
66

67
    /**
68
     * @var string
69
     */
70
    private $selector;
71

72
    /**
73
     * @param string $selector
74
     *
75
     * @return bool
76
     *
77
     * @internal since V8.8.0
78
     */
79
    public static function isValid($selector)
×
80
    {
81
        return \preg_match(static::SELECTOR_VALIDATION_RX, $selector);
×
82
    }
83

84
    /**
85
     * @param string $selector
86
     */
87
    public function __construct($selector)
17✔
88
    {
89
        $this->setSelector($selector);
17✔
90
    }
17✔
91

92
    /**
93
     * @return string
94
     */
95
    public function getSelector()
3✔
96
    {
97
        return $this->selector;
3✔
98
    }
99

100
    /**
101
     * @param string $selector
102
     */
103
    public function setSelector($selector): void
17✔
104
    {
105
        $this->selector = \trim($selector);
17✔
106
    }
17✔
107

108
    /**
109
     * @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
110
     */
111
    public function __toString(): string
1✔
112
    {
113
        return $this->getSelector();
1✔
114
    }
115

116
    /**
117
     * @return int<0, max>
118
     */
119
    public function getSpecificity(): int
14✔
120
    {
121
        $a = 0;
14✔
122
        /// @todo should exclude \# as well as "#"
123
        $aMatches = null;
14✔
124
        $b = \substr_count($this->selector, '#');
14✔
125
        $c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $this->selector, $aMatches);
14✔
126
        $d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $this->selector, $aMatches);
14✔
127
        return ($a * 1000) + ($b * 100) + ($c * 10) + $d;
14✔
128
    }
129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc