1 | 'use strict'
|
2 |
|
3 | let { SourceMapConsumer, SourceMapGenerator } = require('source-map-js')
|
4 | let { fileURLToPath, pathToFileURL } = require('url')
|
5 | let { isAbsolute, resolve } = require('path')
|
6 | let { nanoid } = require('nanoid/non-secure')
|
7 |
|
8 | let terminalHighlight = require('./terminal-highlight')
|
9 | let CssSyntaxError = require('./css-syntax-error')
|
10 | let PreviousMap = require('./previous-map')
|
11 |
|
12 | let fromOffsetCache = Symbol('fromOffsetCache')
|
13 |
|
14 | let sourceMapAvailable = Boolean(SourceMapConsumer && SourceMapGenerator)
|
15 | let pathAvailable = Boolean(resolve && isAbsolute)
|
16 |
|
17 | class Input {
|
18 | constructor(css, opts = {}) {
|
19 | if (
|
20 | css === null ||
|
21 | typeof css === 'undefined' ||
|
22 | (typeof css === 'object' && !css.toString)
|
23 | ) {
|
24 | throw new Error(`PostCSS received ${css} instead of CSS string`)
|
25 | }
|
26 |
|
27 | this.css = css.toString()
|
28 |
|
29 | if (this.css[0] === '\uFEFF' || this.css[0] === '\uFFFE') {
|
30 | this.hasBOM = true
|
31 | this.css = this.css.slice(1)
|
32 | } else {
|
33 | this.hasBOM = false
|
34 | }
|
35 |
|
36 | if (opts.from) {
|
37 | if (
|
38 | !pathAvailable ||
|
39 | /^\w+:\/\
|
40 | isAbsolute(opts.from)
|
41 | ) {
|
42 | this.file = opts.from
|
43 | } else {
|
44 | this.file = resolve(opts.from)
|
45 | }
|
46 | }
|
47 |
|
48 | if (pathAvailable && sourceMapAvailable) {
|
49 | let map = new PreviousMap(this.css, opts)
|
50 | if (map.text) {
|
51 | this.map = map
|
52 | let file = map.consumer().file
|
53 | if (!this.file && file) this.file = this.mapResolve(file)
|
54 | }
|
55 | }
|
56 |
|
57 | if (!this.file) {
|
58 | this.id = '<input css ' + nanoid(6) + '>'
|
59 | }
|
60 | if (this.map) this.map.file = this.from
|
61 | }
|
62 |
|
63 | error(message, line, column, opts = {}) {
|
64 | let result, endLine, endColumn
|
65 |
|
66 | if (line && typeof line === 'object') {
|
67 | let start = line
|
68 | let end = column
|
69 | if (typeof start.offset === 'number') {
|
70 | let pos = this.fromOffset(start.offset)
|
71 | line = pos.line
|
72 | column = pos.col
|
73 | } else {
|
74 | line = start.line
|
75 | column = start.column
|
76 | }
|
77 | if (typeof end.offset === 'number') {
|
78 | let pos = this.fromOffset(end.offset)
|
79 | endLine = pos.line
|
80 | endColumn = pos.col
|
81 | } else {
|
82 | endLine = end.line
|
83 | endColumn = end.column
|
84 | }
|
85 | } else if (!column) {
|
86 | let pos = this.fromOffset(line)
|
87 | line = pos.line
|
88 | column = pos.col
|
89 | }
|
90 |
|
91 | let origin = this.origin(line, column, endLine, endColumn)
|
92 | if (origin) {
|
93 | result = new CssSyntaxError(
|
94 | message,
|
95 | origin.endLine === undefined
|
96 | ? origin.line
|
97 | : { column: origin.column, line: origin.line },
|
98 | origin.endLine === undefined
|
99 | ? origin.column
|
100 | : { column: origin.endColumn, line: origin.endLine },
|
101 | origin.source,
|
102 | origin.file,
|
103 | opts.plugin
|
104 | )
|
105 | } else {
|
106 | result = new CssSyntaxError(
|
107 | message,
|
108 | endLine === undefined ? line : { column, line },
|
109 | endLine === undefined ? column : { column: endColumn, line: endLine },
|
110 | this.css,
|
111 | this.file,
|
112 | opts.plugin
|
113 | )
|
114 | }
|
115 |
|
116 | result.input = { column, endColumn, endLine, line, source: this.css }
|
117 | if (this.file) {
|
118 | if (pathToFileURL) {
|
119 | result.input.url = pathToFileURL(this.file).toString()
|
120 | }
|
121 | result.input.file = this.file
|
122 | }
|
123 |
|
124 | return result
|
125 | }
|
126 |
|
127 | fromOffset(offset) {
|
128 | let lastLine, lineToIndex
|
129 | if (!this[fromOffsetCache]) {
|
130 | let lines = this.css.split('\n')
|
131 | lineToIndex = new Array(lines.length)
|
132 | let prevIndex = 0
|
133 |
|
134 | for (let i = 0, l = lines.length; i < l; i++) {
|
135 | lineToIndex[i] = prevIndex
|
136 | prevIndex += lines[i].length + 1
|
137 | }
|
138 |
|
139 | this[fromOffsetCache] = lineToIndex
|
140 | } else {
|
141 | lineToIndex = this[fromOffsetCache]
|
142 | }
|
143 | lastLine = lineToIndex[lineToIndex.length - 1]
|
144 |
|
145 | let min = 0
|
146 | if (offset >= lastLine) {
|
147 | min = lineToIndex.length - 1
|
148 | } else {
|
149 | let max = lineToIndex.length - 2
|
150 | let mid
|
151 | while (min < max) {
|
152 | mid = min + ((max - min) >> 1)
|
153 | if (offset < lineToIndex[mid]) {
|
154 | max = mid - 1
|
155 | } else if (offset >= lineToIndex[mid + 1]) {
|
156 | min = mid + 1
|
157 | } else {
|
158 | min = mid
|
159 | break
|
160 | }
|
161 | }
|
162 | }
|
163 | return {
|
164 | col: offset - lineToIndex[min] + 1,
|
165 | line: min + 1
|
166 | }
|
167 | }
|
168 |
|
169 | mapResolve(file) {
|
170 | if (/^\w+:\/\//.test(file)) {
|
171 | return file
|
172 | }
|
173 | return resolve(this.map.consumer().sourceRoot || this.map.root || '.', file)
|
174 | }
|
175 |
|
176 | origin(line, column, endLine, endColumn) {
|
177 | if (!this.map) return false
|
178 | let consumer = this.map.consumer()
|
179 |
|
180 | let from = consumer.originalPositionFor({ column, line })
|
181 | if (!from.source) return false
|
182 |
|
183 | let to
|
184 | if (typeof endLine === 'number') {
|
185 | to = consumer.originalPositionFor({ column: endColumn, line: endLine })
|
186 | }
|
187 |
|
188 | let fromUrl
|
189 |
|
190 | if (isAbsolute(from.source)) {
|
191 | fromUrl = pathToFileURL(from.source)
|
192 | } else {
|
193 | fromUrl = new URL(
|
194 | from.source,
|
195 | this.map.consumer().sourceRoot || pathToFileURL(this.map.mapFile)
|
196 | )
|
197 | }
|
198 |
|
199 | let result = {
|
200 | column: from.column,
|
201 | endColumn: to && to.column,
|
202 | endLine: to && to.line,
|
203 | line: from.line,
|
204 | url: fromUrl.toString()
|
205 | }
|
206 |
|
207 | if (fromUrl.protocol === 'file:') {
|
208 | if (fileURLToPath) {
|
209 | result.file = fileURLToPath(fromUrl)
|
210 | } else {
|
211 |
|
212 | throw new Error(`file: protocol is not available in this PostCSS build`)
|
213 | }
|
214 | }
|
215 |
|
216 | let source = consumer.sourceContentFor(from.source)
|
217 | if (source) result.source = source
|
218 |
|
219 | return result
|
220 | }
|
221 |
|
222 | toJSON() {
|
223 | let json = {}
|
224 | for (let name of ['hasBOM', 'css', 'file', 'id']) {
|
225 | if (this[name] != null) {
|
226 | json[name] = this[name]
|
227 | }
|
228 | }
|
229 | if (this.map) {
|
230 | json.map = { ...this.map }
|
231 | if (json.map.consumerCache) {
|
232 | json.map.consumerCache = undefined
|
233 | }
|
234 | }
|
235 | return json
|
236 | }
|
237 |
|
238 | get from() {
|
239 | return this.file || this.id
|
240 | }
|
241 | }
|
242 |
|
243 | module.exports = Input
|
244 | Input.default = Input
|
245 |
|
246 | if (terminalHighlight && terminalHighlight.registerInput) {
|
247 | terminalHighlight.registerInput(Input)
|
248 | }
|