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

MyIntervals / PHP-CSS-Parser / 15947060957

28 Jun 2025 06:35PM UTC coverage: 57.935% (+0.03%) from 57.906%
15947060957

Pull #1294

github

web-flow
Merge 5826c8b96 into 11e634c0b
Pull Request #1294: [CLEANUP] Tidy up `DeclarationBlock::parse()`

6 of 12 new or added lines in 1 file covered. (50.0%)

1055 of 1821 relevant lines covered (57.94%)

16.67 hits per line

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

66.67
/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
9✔
39
    {
40
        $comments = [];
9✔
41
        $result = new DeclarationBlock($parserState->currentLine());
9✔
42
        try {
43
            $selectorParts = [];
9✔
44
            $stringWrapperCharacter = null;
9✔
45
            do {
46
                $selectorParts[] = $parserState->consume(1)
9✔
47
                    . $parserState->consumeUntil(['{', '}', '\'', '"'], false, false, $comments);
9✔
48
                $nextCharacter = $parserState->peek();
9✔
49
                switch ($nextCharacter) {
9✔
50
                    case '\'':
9✔
51
                        // The fallthrough is intentional.
52
                    case '"':
9✔
NEW
53
                        if (!\is_string($stringWrapperCharacter)) {
×
NEW
54
                            $stringWrapperCharacter = $nextCharacter;
×
NEW
55
                        } elseif ($stringWrapperCharacter === $nextCharacter) {
×
NEW
56
                            if (\substr(\end($selectorParts), -1) !== '\\') {
×
NEW
57
                                $stringWrapperCharacter = null;
×
58
                            }
59
                        }
NEW
60
                        break;
×
61
                }
62
            } while (!\in_array($nextCharacter, ['{', '}'], true) || \is_string($stringWrapperCharacter));
9✔
63
            $result->setSelectors(\implode('', $selectorParts), $list);
9✔
64
            if ($parserState->comes('{')) {
6✔
65
                $parserState->consume(1);
6✔
66
            }
67
        } catch (UnexpectedTokenException $e) {
3✔
68
            if ($parserState->getSettings()->usesLenientParsing()) {
3✔
69
                if (!$parserState->comes('}')) {
3✔
70
                    $parserState->consumeUntil('}', false, true);
1✔
71
                }
72
                return null;
3✔
73
            } else {
74
                throw $e;
×
75
            }
76
        }
77
        $result->setComments($comments);
6✔
78
        RuleSet::parseRuleSet($parserState, $result);
6✔
79
        return $result;
6✔
80
    }
81

82
    /**
83
     * @param array<Selector|string>|string $selectors
84
     *
85
     * @throws UnexpectedTokenException
86
     */
87
    public function setSelectors($selectors, ?CSSList $list = null): void
10✔
88
    {
89
        if (\is_array($selectors)) {
10✔
90
            $this->selectors = $selectors;
1✔
91
        } else {
92
            $this->selectors = \explode(',', $selectors);
9✔
93
        }
94
        foreach ($this->selectors as $key => $selector) {
10✔
95
            if (!($selector instanceof Selector)) {
10✔
96
                if ($list === null || !($list instanceof KeyFrame)) {
9✔
97
                    if (!Selector::isValid($selector)) {
9✔
98
                        throw new UnexpectedTokenException(
3✔
99
                            "Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.",
3✔
100
                            $selectors,
101
                            'custom'
3✔
102
                        );
103
                    }
104
                    $this->selectors[$key] = new Selector($selector);
6✔
105
                } else {
106
                    if (!KeyframeSelector::isValid($selector)) {
×
107
                        throw new UnexpectedTokenException(
×
108
                            "Selector did not match '" . KeyframeSelector::SELECTOR_VALIDATION_RX . "'.",
×
109
                            $selector,
110
                            'custom'
×
111
                        );
112
                    }
113
                    $this->selectors[$key] = new KeyframeSelector($selector);
×
114
                }
115
            }
116
        }
117
    }
7✔
118

119
    /**
120
     * Remove one of the selectors of the block.
121
     *
122
     * @param Selector|string $selectorToRemove
123
     */
124
    public function removeSelector($selectorToRemove): bool
×
125
    {
126
        if ($selectorToRemove instanceof Selector) {
×
127
            $selectorToRemove = $selectorToRemove->getSelector();
×
128
        }
129
        foreach ($this->selectors as $key => $selector) {
×
130
            if ($selector->getSelector() === $selectorToRemove) {
×
131
                unset($this->selectors[$key]);
×
132
                return true;
×
133
            }
134
        }
135
        return false;
×
136
    }
137

138
    /**
139
     * @return array<Selector>
140
     */
141
    public function getSelectors(): array
×
142
    {
143
        return $this->selectors;
×
144
    }
145

146
    /**
147
     * @return non-empty-string
148
     *
149
     * @throws OutputException
150
     */
151
    public function render(OutputFormat $outputFormat): string
6✔
152
    {
153
        $formatter = $outputFormat->getFormatter();
6✔
154
        $result = $formatter->comments($this);
6✔
155
        if (\count($this->selectors) === 0) {
6✔
156
            // If all the selectors have been removed, this declaration block becomes invalid
157
            throw new OutputException(
×
158
                'Attempt to print declaration block with missing selector',
×
159
                $this->getLineNumber()
×
160
            );
161
        }
162
        $result .= $outputFormat->getContentBeforeDeclarationBlock();
6✔
163
        $result .= $formatter->implode(
6✔
164
            $formatter->spaceBeforeSelectorSeparator() . ',' . $formatter->spaceAfterSelectorSeparator(),
6✔
165
            $this->selectors
6✔
166
        );
167
        $result .= $outputFormat->getContentAfterDeclarationBlockSelectors();
6✔
168
        $result .= $formatter->spaceBeforeOpeningBrace() . '{';
6✔
169
        $result .= $this->renderRules($outputFormat);
6✔
170
        $result .= '}';
6✔
171
        $result .= $outputFormat->getContentAfterDeclarationBlock();
6✔
172

173
        return $result;
6✔
174
    }
175
}
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