-
Notifications
You must be signed in to change notification settings - Fork 75
/
StringExpression.php
182 lines (149 loc) · 4.44 KB
/
StringExpression.php
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* SCSSPHP
*
* @copyright 2012-2020 Leaf Corcoran
*
* @license http://opensource.org/licenses/MIT MIT
*
* @link http://scssphp.github.io/scssphp
*/
namespace ScssPhp\ScssPhp\Ast\Sass\Expression;
use ScssPhp\ScssPhp\Ast\Sass\Expression;
use ScssPhp\ScssPhp\Ast\Sass\Interpolation;
use ScssPhp\ScssPhp\Parser\InterpolationBuffer;
use ScssPhp\ScssPhp\SourceSpan\FileSpan;
use ScssPhp\ScssPhp\Util\Character;
use ScssPhp\ScssPhp\Visitor\ExpressionVisitor;
/**
* A string literal.
*
* @internal
*/
final class StringExpression implements Expression
{
/**
* @var Interpolation
* @readonly
*/
private $text;
/**
* @var bool
* @readonly
*/
private $quotes;
public function __construct(Interpolation $text, bool $quotes = false)
{
$this->text = $text;
$this->quotes = $quotes;
}
/**
* Returns a string expression with no interpolation.
*/
public static function plain(string $text, FileSpan $span, bool $quotes = false): self
{
return new self(new Interpolation([$text], $span), $quotes);
}
/**
* Returns Sass source for a quoted string that, when evaluated, will have
* $text as its contents.
*/
public static function quoteText(string $text): string
{
$quote = self::bestQuote([$text]);
$buffer = $quote;
$buffer .= self::quoteInnerText($text, $quote, true);
$buffer .= $quote;
return $buffer;
}
public function getText(): Interpolation
{
return $this->text;
}
public function hasQuotes(): bool
{
return $this->quotes;
}
public function getSpan(): FileSpan
{
return $this->text->getSpan();
}
public function accept(ExpressionVisitor $visitor)
{
return $visitor->visitStringExpression($this);
}
public function asInterpolation(bool $static = false, string $quote = null): Interpolation
{
if (!$this->quotes) {
return $this->text;
}
$quote = $quote ?? self::bestQuote($this->text->getContents());
$buffer = new InterpolationBuffer();
$buffer->write($quote);
foreach ($this->text->getContents() as $value) {
if ($value instanceof Expression) {
$buffer->add($value);
} else {
$buffer->write(self::quoteInnerText($value, $quote, $static));
}
}
$buffer->write($quote);
return $buffer->buildInterpolation($this->text->getSpan());
}
private static function quoteInnerText(string $value, string $quote, bool $static = false): string
{
$buffer = '';
$length = \strlen($value);
for ($i = 0; $i < $length; $i++) {
$char = $value[$i];
if (Character::isNewline($char)) {
$buffer .= '\\a';
if ($i !== $length - 1) {
$next = $value[$i + 1];
if (Character::isWhitespace($next) || Character::isHex($next)) {
$buffer .= ' ';
}
}
} else {
if ($char === $quote || $char === '\\' || ($static && $char === '#' && $i < $length - 1 && $value[$i + 1] === '{')) {
$buffer .= '\\';
}
if (\ord($char) < 0x80) {
$buffer .= $char;
} else {
if (!preg_match('/./usA', $value, $m, 0, $i)) {
throw new \UnexpectedValueException('Invalid UTF-8 char');
}
$buffer .= $m[0];
$i += \strlen($m[0]) - 1; // skip over the extra bytes that have been processed.
}
}
}
return $buffer;
}
/**
* @param array<string|Expression> $parts
*
* @return string
*/
private static function bestQuote(array $parts): string
{
$containsDoubleQuote = false;
foreach ($parts as $part) {
if (!\is_string($part)) {
continue;
}
if (false !== strpos($part, "'")) {
return '"';
}
if (false !== strpos($part, '"')) {
$containsDoubleQuote = true;
}
}
return $containsDoubleQuote ? "'": '"';
}
public function __toString(): string
{
return (string) $this->asInterpolation();
}
}