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

MyIntervals / PHP-CSS-Parser / 16157880991

09 Jul 2025 01:09AM UTC coverage: 58.482% (+0.5%) from 57.935%
16157880991

Pull #1292

github

web-flow
Merge 873085a66 into 6dec68e38
Pull Request #1292: [BUGFIX] Allow comma-separated arguments in selectors

26 of 27 new or added lines in 1 file covered. (96.3%)

1 existing line in 1 file now uncovered.

1079 of 1845 relevant lines covered (58.48%)

17.28 hits per line

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

74.75
/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
            $consumedNextCharacter = false;
39✔
48
            static $stopCharacters = ['{', '}', '\'', '"', '(', ')', ','];
39✔
49
            do {
50
                if (!$consumedNextCharacter) {
39✔
51
                    $selectorParts[] = $parserState->consume(1);
39✔
52
                }
53
                $selectorParts[] = $parserState->consumeUntil($stopCharacters, false, false, $comments);
39✔
54
                $nextCharacter = $parserState->peek();
39✔
55
                $consumedNextCharacter = false;
39✔
56
                switch ($nextCharacter) {
39✔
57
                    case '\'':
39✔
58
                        // The fallthrough is intentional.
59
                    case '"':
39✔
60
                        if (!\is_string($stringWrapperCharacter)) {
×
61
                            $stringWrapperCharacter = $nextCharacter;
×
62
                        } elseif ($stringWrapperCharacter === $nextCharacter) {
×
63
                            if (\substr(\end($selectorParts), -1) !== '\\') {
×
64
                                $stringWrapperCharacter = null;
×
65
                            }
66
                        }
67
                        break;
×
68
                    case '(':
39✔
69
                        if (!isset($stringWrapperCharacter)) {
18✔
70
                            ++$functionNestingLevel;
18✔
71
                        }
72
                        break;
18✔
73
                    case ')':
39✔
74
                        if (!isset($stringWrapperCharacter)) {
18✔
75
                            if ($functionNestingLevel <= 0) {
18✔
NEW
76
                                throw new UnexpectedTokenException('anything but', ')');
×
77
                            }
78
                            --$functionNestingLevel;
18✔
79
                        }
80
                        break;
18✔
81
                    case ',':
39✔
82
                        if (!isset($stringWrapperCharacter) && $functionNestingLevel === 0) {
26✔
83
                            $selectors[] = \implode('', $selectorParts);
25✔
84
                            $selectorParts = [];
25✔
85
                            $parserState->consume(1);
25✔
86
                            $consumedNextCharacter = true;
25✔
87
                        }
88
                        break;
26✔
89
                }
90
            } while (!\in_array($nextCharacter, ['{', '}'], true) || \is_string($stringWrapperCharacter));
39✔
91
            $selectors[] = \implode('', $selectorParts); // add final or only selector
39✔
92
            $result->setSelectors($selectors, $list);
39✔
93
            if ($parserState->comes('{')) {
36✔
94
                $parserState->consume(1);
36✔
95
            }
96
        } catch (UnexpectedTokenException $e) {
3✔
97
            if ($parserState->getSettings()->usesLenientParsing()) {
3✔
98
                if (!$parserState->comes('}')) {
3✔
99
                    $parserState->consumeUntil('}', false, true);
1✔
100
                }
101
                return null;
3✔
102
            } else {
103
                throw $e;
×
104
            }
105
        }
106
        $result->setComments($comments);
36✔
107
        RuleSet::parseRuleSet($parserState, $result);
36✔
108
        return $result;
36✔
109
    }
110

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

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

167
    /**
168
     * @return array<Selector>
169
     */
170
    public function getSelectors(): array
30✔
171
    {
172
        return $this->selectors;
30✔
173
    }
174

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

202
        return $result;
6✔
203
    }
204
}
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