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

MyIntervals / PHP-CSS-Parser / 13595546406

28 Feb 2025 07:58PM UTC coverage: 55.322% (+0.04%) from 55.282%
13595546406

Pull #1028

github

web-flow
Merge b16dc4502 into 19ffd076e
Pull Request #1028: [TASK] Stop caching the selector specificity

0 of 7 new or added lines in 1 file covered. (0.0%)

7 existing lines in 1 file now uncovered.

1055 of 1907 relevant lines covered (55.32%)

12.23 hits per line

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

52.63
/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
    public static function isValid($selector)
×
78
    {
79
        return \preg_match(static::SELECTOR_VALIDATION_RX, $selector);
×
80
    }
81

82
    /**
83
     * @param string $selector
84
     * @param bool $calculateSpecificity @deprecated since V8.8.0, will be removed in V9.0.0
85
     */
86
    public function __construct($selector, $calculateSpecificity = false)
3✔
87
    {
88
        $this->setSelector($selector);
3✔
89
    }
3✔
90

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

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

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

115
    /**
116
     * @return int<0, max>
117
     */
NEW
UNCOV
118
    public function getSpecificity(): int
×
119
    {
NEW
UNCOV
120
        $a = 0;
×
121
        /// @todo should exclude \# as well as "#"
NEW
UNCOV
122
        $aMatches = null;
×
NEW
UNCOV
123
        $b = \substr_count($this->selector, '#');
×
NEW
UNCOV
124
        $c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $this->selector, $aMatches);
×
NEW
UNCOV
125
        $d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $this->selector, $aMatches);
×
NEW
UNCOV
126
        return ($a * 1000) + ($b * 100) + ($c * 10) + $d;
×
127
    }
128
}
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