UNPKG

6.19 kBJavaScriptView Raw
1'use strict'
2
3let { SourceMapConsumer, SourceMapGenerator } = require('source-map-js')
4let { fileURLToPath, pathToFileURL } = require('url')
5let { isAbsolute, resolve } = 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 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 get from() {
128 return this.file || this.id
129 }
130
131 fromOffset(offset) {
132 let lastLine, lineToIndex
133 if (!this[fromOffsetCache]) {
134 let lines = this.css.split('\n')
135 lineToIndex = new Array(lines.length)
136 let prevIndex = 0
137
138 for (let i = 0, l = lines.length; i < l; i++) {
139 lineToIndex[i] = prevIndex
140 prevIndex += lines[i].length + 1
141 }
142
143 this[fromOffsetCache] = lineToIndex
144 } else {
145 lineToIndex = this[fromOffsetCache]
146 }
147 lastLine = lineToIndex[lineToIndex.length - 1]
148
149 let min = 0
150 if (offset >= lastLine) {
151 min = lineToIndex.length - 1
152 } else {
153 let max = lineToIndex.length - 2
154 let mid
155 while (min < max) {
156 mid = min + ((max - min) >> 1)
157 if (offset < lineToIndex[mid]) {
158 max = mid - 1
159 } else if (offset >= lineToIndex[mid + 1]) {
160 min = mid + 1
161 } else {
162 min = mid
163 break
164 }
165 }
166 }
167 return {
168 col: offset - lineToIndex[min] + 1,
169 line: min + 1
170 }
171 }
172
173 mapResolve(file) {
174 if (/^\w+:\/\//.test(file)) {
175 return file
176 }
177 return resolve(this.map.consumer().sourceRoot || this.map.root || '.', file)
178 }
179
180 origin(line, column, endLine, endColumn) {
181 if (!this.map) return false
182 let consumer = this.map.consumer()
183
184 let from = consumer.originalPositionFor({ column, line })
185 if (!from.source) return false
186
187 let to
188 if (typeof endLine === 'number') {
189 to = consumer.originalPositionFor({ column: endColumn, line: endLine })
190 }
191
192 let fromUrl
193
194 if (isAbsolute(from.source)) {
195 fromUrl = pathToFileURL(from.source)
196 } else {
197 fromUrl = new URL(
198 from.source,
199 this.map.consumer().sourceRoot || pathToFileURL(this.map.mapFile)
200 )
201 }
202
203 let result = {
204 column: from.column,
205 endColumn: to && to.column,
206 endLine: to && to.line,
207 line: from.line,
208 url: fromUrl.toString()
209 }
210
211 if (fromUrl.protocol === 'file:') {
212 if (fileURLToPath) {
213 result.file = fileURLToPath(fromUrl)
214 } else {
215 /* c8 ignore next 2 */
216 throw new Error(`file: protocol is not available in this PostCSS build`)
217 }
218 }
219
220 let source = consumer.sourceContentFor(from.source)
221 if (source) result.source = source
222
223 return result
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}