UNPKG

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