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