[6a3a178] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | let { SourceMapConsumer, SourceMapGenerator } = require('source-map-js')
|
---|
| 4 | let { fileURLToPath, pathToFileURL } = require('url')
|
---|
| 5 | let { resolve, isAbsolute } = 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+:\/\//.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
|
---|
| 107 | if (!column) {
|
---|
| 108 | let pos = this.fromOffset(line)
|
---|
| 109 | line = pos.line
|
---|
| 110 | column = pos.col
|
---|
| 111 | }
|
---|
| 112 | let origin = this.origin(line, column)
|
---|
| 113 | if (origin) {
|
---|
| 114 | result = new CssSyntaxError(
|
---|
| 115 | message,
|
---|
| 116 | origin.line,
|
---|
| 117 | origin.column,
|
---|
| 118 | origin.source,
|
---|
| 119 | origin.file,
|
---|
| 120 | opts.plugin
|
---|
| 121 | )
|
---|
| 122 | } else {
|
---|
| 123 | result = new CssSyntaxError(
|
---|
| 124 | message,
|
---|
| 125 | line,
|
---|
| 126 | column,
|
---|
| 127 | this.css,
|
---|
| 128 | this.file,
|
---|
| 129 | opts.plugin
|
---|
| 130 | )
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | result.input = { line, column, source: this.css }
|
---|
| 134 | if (this.file) {
|
---|
| 135 | if (pathToFileURL) {
|
---|
| 136 | result.input.url = pathToFileURL(this.file).toString()
|
---|
| 137 | }
|
---|
| 138 | result.input.file = this.file
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | return result
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | origin(line, column) {
|
---|
| 145 | if (!this.map) return false
|
---|
| 146 | let consumer = this.map.consumer()
|
---|
| 147 |
|
---|
| 148 | let from = consumer.originalPositionFor({ line, column })
|
---|
| 149 | if (!from.source) return false
|
---|
| 150 |
|
---|
| 151 | let fromUrl
|
---|
| 152 |
|
---|
| 153 | if (isAbsolute(from.source)) {
|
---|
| 154 | fromUrl = pathToFileURL(from.source)
|
---|
| 155 | } else {
|
---|
| 156 | fromUrl = new URL(
|
---|
| 157 | from.source,
|
---|
| 158 | this.map.consumer().sourceRoot || pathToFileURL(this.map.mapFile)
|
---|
| 159 | )
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | let result = {
|
---|
| 163 | url: fromUrl.toString(),
|
---|
| 164 | line: from.line,
|
---|
| 165 | column: from.column
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | if (fromUrl.protocol === 'file:') {
|
---|
| 169 | if (fileURLToPath) {
|
---|
| 170 | result.file = fileURLToPath(fromUrl)
|
---|
| 171 | } else {
|
---|
| 172 | // istanbul ignore next
|
---|
| 173 | throw new Error(`file: protocol is not available in this PostCSS build`)
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | let source = consumer.sourceContentFor(from.source)
|
---|
| 178 | if (source) result.source = source
|
---|
| 179 |
|
---|
| 180 | return result
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | mapResolve(file) {
|
---|
| 184 | if (/^\w+:\/\//.test(file)) {
|
---|
| 185 | return file
|
---|
| 186 | }
|
---|
| 187 | return resolve(this.map.consumer().sourceRoot || this.map.root || '.', file)
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | get from() {
|
---|
| 191 | return this.file || this.id
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | toJSON() {
|
---|
| 195 | let json = {}
|
---|
| 196 | for (let name of ['hasBOM', 'css', 'file', 'id']) {
|
---|
| 197 | if (this[name] != null) {
|
---|
| 198 | json[name] = this[name]
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 | if (this.map) {
|
---|
| 202 | json.map = { ...this.map }
|
---|
| 203 | if (json.map.consumerCache) {
|
---|
| 204 | json.map.consumerCache = undefined
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | return json
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | module.exports = Input
|
---|
| 212 | Input.default = Input
|
---|
| 213 |
|
---|
| 214 | if (terminalHighlight && terminalHighlight.registerInput) {
|
---|
| 215 | terminalHighlight.registerInput(Input)
|
---|
| 216 | }
|
---|