You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
6.0 KiB

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