source: node_modules/postcss/lib/input.js@ 7deb3e2

Last change on this file since 7deb3e2 was 57e58a3, checked in by ste08 <sjovanoska@…>, 4 months ago

Initial commit

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