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

MyIntervals / PHP-CSS-Parser / 12967903681

25 Jan 2025 07:43PM UTC coverage: 42.138%. Remained the same
12967903681

Pull #805

github

web-flow
Merge 1c5a672ee into 24f44c2ad
Pull Request #805: [TASK] Use native `void` return type declarations

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

5 existing lines in 3 files now uncovered.

871 of 2067 relevant lines covered (42.14%)

6.56 hits per line

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

0.0
/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 $sSelector;
71

72
    /**
73
     * @var int|null
74
     */
75
    private $iSpecificity;
76

77
    /**
78
     * @param string $sSelector
79
     *
80
     * @return bool
81
     */
82
    public static function isValid($sSelector)
×
83
    {
84
        return \preg_match(static::SELECTOR_VALIDATION_RX, $sSelector);
×
85
    }
86

87
    /**
88
     * @param string $sSelector
89
     * @param bool $bCalculateSpecificity
90
     */
91
    public function __construct($sSelector, $bCalculateSpecificity = false)
×
92
    {
93
        $this->setSelector($sSelector);
×
94
        if ($bCalculateSpecificity) {
×
95
            $this->getSpecificity();
×
96
        }
97
    }
×
98

99
    /**
100
     * @return string
101
     */
102
    public function getSelector()
×
103
    {
104
        return $this->sSelector;
×
105
    }
106

107
    /**
108
     * @param string $sSelector
109
     */
UNCOV
110
    public function setSelector($sSelector): void
×
111
    {
112
        $this->sSelector = \trim($sSelector);
×
113
        $this->iSpecificity = null;
×
114
    }
×
115

116
    public function __toString(): string
×
117
    {
118
        return $this->getSelector();
×
119
    }
120

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