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

MyIntervals / PHP-CSS-Parser / 16112982700

07 Jul 2025 09:18AM UTC coverage: 57.935%. Remained the same
16112982700

Pull #1307

github

web-flow
Merge 81b4533d5 into 674cfa00c
Pull Request #1307: [DOCS] Temporarily drop some markers from the class diagram

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

25.0
/src/Value/CSSString.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS\Value;
6

7
use Sabberworm\CSS\OutputFormat;
8
use Sabberworm\CSS\Parsing\ParserState;
9
use Sabberworm\CSS\Parsing\SourceException;
10
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
11
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
12

13
/**
14
 * This class is a wrapper for quoted strings to distinguish them from keywords.
15
 *
16
 * `CSSString`s always output with double quotes.
17
 */
18
class CSSString extends PrimitiveValue
19
{
20
    /**
21
     * @var string
22
     */
23
    private $string;
24

25
    /**
26
     * @param int<1, max>|null $lineNumber
27
     */
28
    public function __construct(string $string, ?int $lineNumber = null)
6✔
29
    {
30
        $this->string = $string;
6✔
31
        parent::__construct($lineNumber);
6✔
32
    }
6✔
33

34
    /**
35
     * @throws SourceException
36
     * @throws UnexpectedEOFException
37
     * @throws UnexpectedTokenException
38
     *
39
     * @internal since V8.8.0
40
     */
41
    public static function parse(ParserState $parserState): CSSString
×
42
    {
43
        $begin = $parserState->peek();
×
44
        $quote = null;
×
45
        if ($begin === "'") {
×
46
            $quote = "'";
×
47
        } elseif ($begin === '"') {
×
48
            $quote = '"';
×
49
        }
50
        if ($quote !== null) {
×
51
            $parserState->consume($quote);
×
52
        }
53
        $result = '';
×
54
        $content = null;
×
55
        if ($quote === null) {
×
56
            // Unquoted strings end in whitespace or with braces, brackets, parentheses
57
            while (\preg_match('/[\\s{}()<>\\[\\]]/isu', $parserState->peek()) !== 1) {
×
58
                $result .= $parserState->parseCharacter(false);
×
59
            }
60
        } else {
61
            while (!$parserState->comes($quote)) {
×
62
                $content = $parserState->parseCharacter(false);
×
63
                if ($content === null) {
×
64
                    throw new SourceException(
×
65
                        "Non-well-formed quoted string {$parserState->peek(3)}",
×
66
                        $parserState->currentLine()
×
67
                    );
68
                }
69
                $result .= $content;
×
70
            }
71
            $parserState->consume($quote);
×
72
        }
73
        return new CSSString($result, $parserState->currentLine());
×
74
    }
75

76
    public function setString(string $string): void
1✔
77
    {
78
        $this->string = $string;
1✔
79
    }
1✔
80

81
    public function getString(): string
2✔
82
    {
83
        return $this->string;
2✔
84
    }
85

86
    /**
87
     * @return non-empty-string
88
     */
89
    public function render(OutputFormat $outputFormat): string
×
90
    {
91
        $string = \addslashes($this->string);
×
92
        $string = \str_replace("\n", '\\A', $string);
×
93
        return $outputFormat->getStringQuotingType() . $string . $outputFormat->getStringQuotingType();
×
94
    }
95
}
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