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

MyIntervals / PHP-CSS-Parser / 15961006949

30 Jun 2025 12:24AM UTC coverage: 58.275% (+0.3%) from 57.935%
15961006949

Pull #1292

github

web-flow
Merge 11776b0db into 1d8b85516
Pull Request #1292: [BUGFIX] Allow comma-separated arguments in selectors

20 of 24 new or added lines in 2 files covered. (83.33%)

1 existing line in 1 file now uncovered.

1074 of 1843 relevant lines covered (58.27%)

17.2 hits per line

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

73.4
/src/RuleSet/DeclarationBlock.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS\RuleSet;
6

7
use Sabberworm\CSS\CSSList\CSSList;
8
use Sabberworm\CSS\CSSList\KeyFrame;
9
use Sabberworm\CSS\OutputFormat;
10
use Sabberworm\CSS\Parsing\OutputException;
11
use Sabberworm\CSS\Parsing\ParserState;
12
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
13
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
14
use Sabberworm\CSS\Property\KeyframeSelector;
15
use Sabberworm\CSS\Property\Selector;
16

17
/**
18
 * This class represents a `RuleSet` constrained by a `Selector`.
19
 *
20
 * It contains an array of selector objects (comma-separated in the CSS) as well as the rules to be applied to the
21
 * matching elements.
22
 *
23
 * Declaration blocks usually appear directly inside a `Document` or another `CSSList` (mostly a `MediaQuery`).
24
 */
25
class DeclarationBlock extends RuleSet
26
{
27
    /**
28
     * @var array<Selector|string>
29
     */
30
    private $selectors = [];
31

32
    /**
33
     * @throws UnexpectedTokenException
34
     * @throws UnexpectedEOFException
35
     *
36
     * @internal since V8.8.0
37
     */
38
    public static function parse(ParserState $parserState, ?CSSList $list = null): ?DeclarationBlock
39✔
39
    {
40
        $comments = [];
39✔
41
        $result = new DeclarationBlock($parserState->currentLine());
39✔
42
        try {
43
            $selectors = [];
39✔
44
            $selectorParts = [];
39✔
45
            $stringWrapperCharacter = null;
39✔
46
            $functionNestingLevel = 0;
39✔
47
            do {
48
                $selectorParts[] = $parserState->consume(1)
39✔
49
                    . $parserState->consumeUntil(['{', '}', '\'', '"', '(', ')', ','], false, false, $comments);
39✔
50
                $nextCharacter = $parserState->peek();
39✔
51
                switch ($nextCharacter) {
39✔
52
                    case '\'':
39✔
53
                        // The fallthrough is intentional.
54
                    case '"':
39✔
55
                        if (!\is_string($stringWrapperCharacter)) {
×
56
                            $stringWrapperCharacter = $nextCharacter;
×
57
                        } elseif ($stringWrapperCharacter === $nextCharacter) {
×
58
                            if (\substr(\end($selectorParts), -1) !== '\\') {
×
59
                                $stringWrapperCharacter = null;
×
60
                            }
61
                        }
62
                        break;
×
63
                    case '(':
39✔
64
                        if (!isset($stringWrapperCharacter)) {
18✔
65
                            ++$functionNestingLevel;
18✔
66
                        }
67
                        break;
18✔
68
                    case ')':
39✔
69
                        if (!isset($stringWrapperCharacter)) {
18✔
70
                            if ($functionNestingLevel <= 0) {
18✔
NEW
71
                                throw new UnexpectedTokenException('anything but', ')');
×
72
                            }
73
                            --$functionNestingLevel;
18✔
74
                        }
75
                        break;
18✔
76
                    case ',':
39✔
77
                        if (!isset($stringWrapperCharacter) && $functionNestingLevel === 0) {
26✔
78
                            $selectors[] = \implode('', $selectorParts);
25✔
79
                            $selectorParts = [];
25✔
80
                            $parserState->consume(1);
25✔
81
                        }
82
                        break;
26✔
83
                }
84
            } while (!\in_array($nextCharacter, ['{', '}'], true) || \is_string($stringWrapperCharacter));
39✔
85
            $selectors[] = \implode('', $selectorParts); // add final or only selector
39✔
86
            $result->setSelectors($selectors, $list);
39✔
87
            if ($parserState->comes('{')) {
36✔
88
                $parserState->consume(1);
36✔
89
            }
90
        } catch (UnexpectedTokenException $e) {
3✔
91
            if ($parserState->getSettings()->usesLenientParsing()) {
3✔
92
                if (!$parserState->comes('}')) {
3✔
93
                    $parserState->consumeUntil('}', false, true);
1✔
94
                }
95
                return null;
3✔
96
            } else {
97
                throw $e;
×
98
            }
99
        }
100
        $result->setComments($comments);
36✔
101
        RuleSet::parseRuleSet($parserState, $result);
36✔
102
        return $result;
36✔
103
    }
104

105
    /**
106
     * @param array<Selector|string>|string $selectors
107
     *
108
     * @throws UnexpectedTokenException
109
     */
110
    public function setSelectors($selectors, ?CSSList $list = null): void
40✔
111
    {
112
        if (\is_array($selectors)) {
40✔
113
            $this->selectors = $selectors;
40✔
114
        } else {
UNCOV
115
            $this->selectors = \explode(',', $selectors);
×
116
        }
117
        foreach ($this->selectors as $key => $selector) {
40✔
118
            if (!($selector instanceof Selector)) {
40✔
119
                if ($list === null || !($list instanceof KeyFrame)) {
39✔
120
                    if (!Selector::isValid($selector)) {
39✔
121
                        throw new UnexpectedTokenException(
3✔
122
                            "Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.",
3✔
123
                            $selector,
124
                            'custom'
3✔
125
                        );
126
                    }
127
                    $this->selectors[$key] = new Selector($selector);
36✔
128
                } else {
129
                    if (!KeyframeSelector::isValid($selector)) {
×
130
                        throw new UnexpectedTokenException(
×
131
                            "Selector did not match '" . KeyframeSelector::SELECTOR_VALIDATION_RX . "'.",
×
132
                            $selector,
133
                            'custom'
×
134
                        );
135
                    }
136
                    $this->selectors[$key] = new KeyframeSelector($selector);
×
137
                }
138
            }
139
        }
140
    }
37✔
141

142
    /**
143
     * Remove one of the selectors of the block.
144
     *
145
     * @param Selector|string $selectorToRemove
146
     */
147
    public function removeSelector($selectorToRemove): bool
×
148
    {
149
        if ($selectorToRemove instanceof Selector) {
×
150
            $selectorToRemove = $selectorToRemove->getSelector();
×
151
        }
152
        foreach ($this->selectors as $key => $selector) {
×
153
            if ($selector->getSelector() === $selectorToRemove) {
×
154
                unset($this->selectors[$key]);
×
155
                return true;
×
156
            }
157
        }
158
        return false;
×
159
    }
160

161
    /**
162
     * @return array<Selector>
163
     */
164
    public function getSelectors(): array
30✔
165
    {
166
        return $this->selectors;
30✔
167
    }
168

169
    /**
170
     * @return non-empty-string
171
     *
172
     * @throws OutputException
173
     */
174
    public function render(OutputFormat $outputFormat): string
6✔
175
    {
176
        $formatter = $outputFormat->getFormatter();
6✔
177
        $result = $formatter->comments($this);
6✔
178
        if (\count($this->selectors) === 0) {
6✔
179
            // If all the selectors have been removed, this declaration block becomes invalid
180
            throw new OutputException(
×
181
                'Attempt to print declaration block with missing selector',
×
182
                $this->getLineNumber()
×
183
            );
184
        }
185
        $result .= $outputFormat->getContentBeforeDeclarationBlock();
6✔
186
        $result .= $formatter->implode(
6✔
187
            $formatter->spaceBeforeSelectorSeparator() . ',' . $formatter->spaceAfterSelectorSeparator(),
6✔
188
            $this->selectors
6✔
189
        );
190
        $result .= $outputFormat->getContentAfterDeclarationBlockSelectors();
6✔
191
        $result .= $formatter->spaceBeforeOpeningBrace() . '{';
6✔
192
        $result .= $this->renderRules($outputFormat);
6✔
193
        $result .= '}';
6✔
194
        $result .= $outputFormat->getContentAfterDeclarationBlock();
6✔
195

196
        return $result;
6✔
197
    }
198
}
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

© 2025 Coveralls, Inc