forked from mlocati/postcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCssSyntaxError.php
More file actions
233 lines (206 loc) · 6.03 KB
/
CssSyntaxError.php
File metadata and controls
233 lines (206 loc) · 6.03 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
namespace PostCSS\Exception;
use PostCSS\Plugin\PluginInterface;
use PostCSS\Terminal;
/**
* The CSS parser throws this error for broken CSS.
*
* Custom parsers can throw this error for broken custom syntax using Node->error method.
*
* PostCSS will use the input source map to detect the original error location.
* If you wrote a Sass file, compiled it to CSS and then parsed it with PostCSS, PostCSS will show the original position in the Sass file.
*
* If you need the position in the PostCSS input (e.g., to debug the previous compiler), use `$error->input['file']`.
*
* @link https://github.com/postcss/postcss/blob/master/lib/css-syntax-error.es6
*/
class CssSyntaxError extends Exception
{
/**
* @var string
*/
protected $postcssReason;
/**
* @var int|null
*/
protected $postcssLine;
/**
* @var int|null
*/
protected $postcssColumn;
/**
* @var string
*/
protected $postcssSource;
/**
* @var string
*/
protected $postcssFile;
/**
* @var PluginInterface|string|null
*/
protected $postcssPlugin;
/**
* @var array|null
*/
public $input = null;
/**
* @param string $reason Error message
* @param int|null $line Source line of the error
* @param int|null $column Source column of the error
* @param string $source Source code of the broken file
* @param string $file Absolute path to the broken file
* @param PluginInterface|string|null $plugin PostCSS name, if error came from plugin
*/
public function __construct($reason, $line = null, $column = null, $source = '', $file = '', $plugin = null)
{
parent::__construct('');
$this->postcssReason = (string) $reason;
$line = ($line || $line === 0 || $line === '0') ? (int) $line : null;
$column = ($column || $column === 0 || $column === '0') ? (int) $column : null;
if ($line !== null && $column !== null) {
$this->postcssLine = $line;
$this->postcssColumn = $column;
} else {
$this->postcssLine = null;
$this->postcssColumn = null;
}
$this->postcssSource = (string) $source;
$this->postcssFile = (string) $file;
$this->postcssPlugin = $plugin;
$this->updateMessage();
}
/**
* @param string $message
*/
public function setMessage($message)
{
$this->message = (string) $message;
}
/**
* Updates the message.
*/
public function updateMessage()
{
$plugin = ($this->postcssPlugin instanceof PluginInterface) ? $this->postcssPlugin->getName() : (string) $this->postcssPlugin;
$message = ($plugin === '') ? '' : ($plugin.': ');
$message .= ($this->postcssFile === '') ? '<css input>' : $this->postcssFile;
if ($this->postcssLine !== null) {
$message .= ':'.$this->postcssLine.':'.$this->postcssColumn;
}
$message .= ': '.$this->postcssReason;
$this->message = $message;
}
/**
* @return string
*/
public function getPostCSSReason()
{
return $this->postcssReason;
}
/**
* @return int|null
*/
public function getPostCSSLine()
{
return $this->postcssLine;
}
/**
* @return int|null
*/
public function getPostCSSColumn()
{
return $this->postcssColumn;
}
/**
* @return string
*/
public function getPostCSSSource()
{
return $this->postcssSource;
}
/**
* @return string
*/
public function setPostCSSSource($source)
{
$this->postcssSource = (string) $source;
}
/**
* @return string
*/
public function getPostCSSFile()
{
return $this->postcssFile;
}
/**
* @return PluginInterface|string|null
*/
public function getPostCSSPlugin()
{
return $this->postcssPlugin;
}
/**
* @param PluginInterface|string|null $plugin
*/
public function setPostCSSPlugin($plugin)
{
$this->postcssPlugin = $plugin;
}
/**
* Returns a few lines of CSS source that caused the error.
*
* If the CSS has an input source map without `sourceContent`, this method will return an empty string.
*
* @param bool $color Whether arrow will be colored red by terminal color codes
*
* @return string Few lines of CSS source that caused the error
*/
public function showSourceCode($color = null)
{
$css = $this->postcssSource;
if ($css === '') {
return '';
}
if ($color) {
$css = Terminal::highlight($css);
}
$lines = preg_split('/\r?\n/', $css);
$start = max($this->postcssLine - 3, 0);
$end = min($this->postcssLine + 2, count($lines));
$maxWidth = strlen((string) $end);
$output = [];
$relevantLines = array_slice($lines, $start, $end - $start);
foreach ($relevantLines as $index => $line) {
$number = $start + 1 + $index;
$padded = substr(' '.$number, -$maxWidth);
$gutter = ' '.$padded.' | ';
if ($number === $this->postcssLine) {
$spacing = preg_replace('/\d/', ' ', $gutter).preg_replace('/[^\t]/', ' ', substr($line, 0, $this->postcssColumn - 1));
$output[] = '>'.$gutter.$line."\n ".$spacing.'^';
} else {
$output[] = ' '.$gutter.$line;
}
}
return implode("\n", $output);
}
/**
* {@inheritdoc}
*
* @see Exception::__toString()
*/
public function __toString()
{
$fqn = get_class($this);
$fqnParts = explode('\\', $fqn);
if (count($fqnParts) === 3 && $fqnParts[0] === 'PostCSS' && $fqnParts[1] === 'Exception') {
$fqn = $fqnParts[2];
}
$result = $fqn.': '.$this->message;
$code = $this->showSourceCode();
if ($code !== '') {
$result .= "\n\n".$code."\n";
}
return $result;
}
}