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