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

MyIntervals / PHP-CSS-Parser / 13603540116

01 Mar 2025 09:46AM UTC coverage: 55.753%. Remained the same
13603540116

Pull #1044

github

web-flow
Merge 411bd9ea8 into 7ef82db15
Pull Request #1044: [TASK] Add more native type declarations for `Selector`

4 of 5 new or added lines in 1 file covered. (80.0%)

1066 of 1912 relevant lines covered (55.75%)

12.33 hits per line

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

87.5
/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
     * @var int|null
74
     */
75
    private $specificity;
76

77
    /**
78
     * @return bool
79
     *
80
     * @internal since V8.8.0
81
     */
NEW
82
    public static function isValid(string $selector)
×
83
    {
84
        return \preg_match(static::SELECTOR_VALIDATION_RX, $selector);
×
85
    }
86

87
    /**
88
     * @param bool $calculateSpecificity @deprecated since V8.8.0, will be removed in V9.0.0
89
     */
90
    public function __construct(string $selector, bool $calculateSpecificity = false)
17✔
91
    {
92
        $this->setSelector($selector);
17✔
93
        if ($calculateSpecificity) {
17✔
94
            $this->getSpecificity();
×
95
        }
96
    }
17✔
97

98
    public function getSelector(): string
3✔
99
    {
100
        return $this->selector;
3✔
101
    }
102

103
    public function setSelector(string $selector): void
17✔
104
    {
105
        $this->selector = \trim($selector);
17✔
106
        $this->specificity = null;
17✔
107
    }
17✔
108

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

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