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.

251 lines
6.1 KiB

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