Debian Codegraph @

vendor/sabberworm/php-css-parser/src/Value/CSSString.php

raw
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 use Sabberworm\CSS\ShortClassNameProvider;
13
14 use function Safe\preg_match;
15
16 /**
17 * This class is a wrapper for quoted strings to distinguish them from keywords.
18 *
19 * `CSSString`s always output with double quotes.
20 */
21 class CSSString extends PrimitiveValue
22 {
23 use ShortClassNameProvider;
24
25 /**
26 * @var string
27 */
28 private $string;
29
30 /**
31 * @param int<1, max>|null $lineNumber
32 */
33 public function __construct(string $string, ?int $lineNumber = null)
34 {
35 $this->string = $string;
36 parent::__construct($lineNumber);
37 }
38
39 /**
40 * @throws SourceException
41 * @throws UnexpectedEOFException
42 * @throws UnexpectedTokenException
43 *
44 * @internal since V8.8.0
45 */
46 public static function parse(ParserState $parserState): CSSString
47 {
48 $begin = $parserState->peek();
49 $quote = null;
50 if ($begin === "'") {
51 $quote = "'";
52 } elseif ($begin === '"') {
53 $quote = '"';
54 }
55 if ($quote !== null) {
56 $parserState->consume($quote);
57 }
58 $result = '';
59 $content = null;
60 if ($quote === null) {
61 // Unquoted strings end in whitespace or with braces, brackets, parentheses
62 while (preg_match('/[\\s{}()<>\\[\\]]/isu', $parserState->peek()) === 0) {
63 $result .= $parserState->parseCharacter(false);
64 }
65 } else {
66 while (!$parserState->comes($quote)) {
67 $content = $parserState->parseCharacter(false);
68 if ($content === null) {
69 throw new SourceException(
70 "Non-well-formed quoted string {$parserState->peek(3)}",
71 $parserState->currentLine()
72 );
73 }
74 $result .= $content;
75 }
76 $parserState->consume($quote);
77 }
78 return new CSSString($result, $parserState->currentLine());
79 }
80
81 public function setString(string $string): void
82 {
83 $this->string = $string;
84 }
85
86 public function getString(): string
87 {
88 return $this->string;
89 }
90
91 /**
92 * @return non-empty-string
93 */
94 public function render(OutputFormat $outputFormat): string
95 {
96 return $outputFormat->getStringQuotingType()
97 . $this->escape($this->string, $outputFormat)
98 . $outputFormat->getStringQuotingType();
99 }
100
101 /**
102 * @return array<string, bool|int|float|string|array<mixed>|null>
103 *
104 * @internal
105 */
106 public function getArrayRepresentation(): array
107 {
108 return [
109 'class' => $this->getShortClassName(),
110 // We're using the term "contents" here to make the difference to the class more clear.
111 'contents' => $this->string,
112 ];
113 }
114
115 private function escape(string $string, OutputFormat $outputFormat): string
116 {
117 $charactersToEscape = '\\';
118 $charactersToEscape .= ($outputFormat->getStringQuotingType() === '"' ? '"' : "'");
119 $withEscapedQuotes = \addcslashes($string, $charactersToEscape);
120
121 $withNewlineEncoded = \str_replace("\n", '\\A', $withEscapedQuotes);
122
123 return $withNewlineEncoded;
124 }
125 }