forked from mlocati/postcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.php
More file actions
187 lines (174 loc) · 5.29 KB
/
Input.php
File metadata and controls
187 lines (174 loc) · 5.29 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
<?php
namespace PostCSS;
/**
* Represents the source CSS.
*
* @link https://github.com/postcss/postcss/blob/master/lib/input.es6
*
* @example
* $root = \PostCSS\Parser::parse($css, ['from' => $file]);
* $input = $root->source->input;
*
* @property string $from The CSS source identifier. Contains Input->file if the user set the `from` option, or Input->id if they did not
*
* @example
* $root = \PostCSS\Parser::parse($css, ['from' => 'a.css']);
* $root-source->input->from //=> "/home/ai/a.css"
* $root = \PostCSS\Parser::parse($css);
* $root-source->input->from //=> "<input css 1>"
*/
class Input
{
protected static $sequence = 0;
/**
* @var string
*/
public $css;
/**
* @var string|null
*/
public $file = null;
/**
* @var PreviousMap|null
*/
public $map = null;
/**
* @var string|null
*/
public $id = null;
/**
* @param string $css Input CSS source
* @param array $opts Options (see Processor->process ). It may also contain: {
*
* @var string $from The absolute path to the CSS source file.
* }
*/
public function __construct($css, array $opts = [])
{
if (is_resource($css)) {
$s = [];
while (!feof($css)) {
$chunk = @stream_get_contents($css, 8192);
if ($chunk === false) {
throw new \Exception('Failed to read from stream');
}
$s[] = $chunk;
}
$css = implode('', $s);
} else {
$css = (string) $css;
}
if (isset($css[2]) && $css[0] === "\xEF" && $css[1] === "\xBB" && $css[2] === "\xBF") {
$css = substr($css, 3);
}
$this->css = $css;
if (isset($opts['from']) && $opts['from']) {
if (preg_match('/^\w+:\/\//', $opts['from'])) {
$this->file = $opts['from'];
} else {
$this->file = Path\NodeJS::resolve($opts['from']);
}
}
$map = new PreviousMap($this->css, $opts);
if (isset($map->text)) {
$this->map = $map;
$file = $map->consumer()->file;
if ($this->file === null && $file !== null && $file !== '') {
$this->file = $this->mapResolve($file);
}
}
if ($this->file === null) {
++self::$sequence;
$this->id = '<input css '.self::$sequence.'>';
}
if ($this->map !== null) {
$this->map->file = $this->from;
}
}
/**
* Build a CssSyntaxError error.
*
* @param string $message
* @param int $line
* @param int $column
* @param array $opts
*
* @return Exception\CssSyntaxError
*/
public function error($message, $line, $column, array $opts = [])
{
$origin = $this->origin($line, $column);
if ($origin !== null) {
$result = new Exception\CssSyntaxError($message, $origin['line'], $origin['column'], $origin['source'], $origin['file'], isset($opts['plugin']) ? $opts['plugin'] : '');
} else {
$result = new Exception\CssSyntaxError($message, $line, $column, $this->css, $this->file, isset($opts['plugin']) ? $opts['plugin'] : '');
}
$result->input = [
'line' => $line,
'column' => $column,
'source' => $this->css,
];
if ($this->file !== null) {
$result->input['file'] = $this->file;
}
return $result;
}
/**
* Reads the input source map and returns a symbol position in the input source (e.g., in a Sass file that was compiled to CSS before being passed to PostCSS).
*
* @param int $line Line in input CSS
* @param int $column Column in input CSS
*
* @return null|array {
*
* @var string $file Path to file
* @var int $line Source line in file
* @var int $column Source column in file
* @var string $source Contents source (empty if not available).
* }
*/
public function origin($line, $column)
{
if ($this->map === null) {
return null;
}
$consumer = $this->map->consumer();
$from = $consumer->originalPositionFor(['line' => $line, 'column' => $column]);
if ($from === null || $from['source'] === '') {
return null;
}
$result = [
'file' => $this->mapResolve($from['source']),
'line' => $from['line'],
'column' => $from['column'],
'source' => '',
];
$source = $consumer->sourceContentFor($from['source']);
if ($source !== null) {
$result['source'] = $source;
}
return $result;
}
/**
* @param string $file
*
* @return string
*/
protected function mapResolve($file)
{
if (preg_match('/^\w+:\/\//', $file)) {
return $file;
} else {
return Path\NodeJS::resolve($this->map->consumer()->sourceRoot ?: '.', $file);
}
}
public function __get($name)
{
switch ($name) {
case 'from':
return $this->file ?: $this->id;
default:
throw new Exception\UndefinedProperty($this, $name);
}
}
}