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. 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. this.document = this.css
  29. if (opts.document) this.document = opts.document.toString()
  30. if (opts.from) {
  31. if (
  32. !pathAvailable ||
  33. /^\w+:\/\//.test(opts.from) ||
  34. isAbsolute(opts.from)
  35. ) {
  36. this.file = opts.from
  37. } else {
  38. this.file = resolve(opts.from)
  39. }
  40. }
  41. if (pathAvailable && sourceMapAvailable) {
  42. let map = new PreviousMap(this.css, opts)
  43. if (map.text) {
  44. this.map = map
  45. let file = map.consumer().file
  46. if (!this.file && file) this.file = this.mapResolve(file)
  47. }
  48. }
  49. if (!this.file) {
  50. this.id = '<input css ' + nanoid(6) + '>'
  51. }
  52. if (this.map) this.map.file = this.from
  53. }
  54. error(message, line, column, opts = {}) {
  55. let endColumn, endLine, result
  56. if (line && typeof line === 'object') {
  57. let start = line
  58. let end = column
  59. if (typeof start.offset === 'number') {
  60. let pos = this.fromOffset(start.offset)
  61. line = pos.line
  62. column = pos.col
  63. } else {
  64. line = start.line
  65. column = start.column
  66. }
  67. if (typeof end.offset === 'number') {
  68. let pos = this.fromOffset(end.offset)
  69. endLine = pos.line
  70. endColumn = pos.col
  71. } else {
  72. endLine = end.line
  73. endColumn = end.column
  74. }
  75. } else if (!column) {
  76. let pos = this.fromOffset(line)
  77. line = pos.line
  78. column = pos.col
  79. }
  80. let origin = this.origin(line, column, endLine, endColumn)
  81. if (origin) {
  82. result = new CssSyntaxError(
  83. message,
  84. origin.endLine === undefined
  85. ? origin.line
  86. : { column: origin.column, line: origin.line },
  87. origin.endLine === undefined
  88. ? origin.column
  89. : { column: origin.endColumn, line: origin.endLine },
  90. origin.source,
  91. origin.file,
  92. opts.plugin
  93. )
  94. } else {
  95. result = new CssSyntaxError(
  96. message,
  97. endLine === undefined ? line : { column, line },
  98. endLine === undefined ? column : { column: endColumn, line: endLine },
  99. this.css,
  100. this.file,
  101. opts.plugin
  102. )
  103. }
  104. result.input = { column, endColumn, endLine, line, source: this.css }
  105. if (this.file) {
  106. if (pathToFileURL) {
  107. result.input.url = pathToFileURL(this.file).toString()
  108. }
  109. result.input.file = this.file
  110. }
  111. return result
  112. }
  113. fromOffset(offset) {
  114. let lastLine, lineToIndex
  115. if (!this[fromOffsetCache]) {
  116. let lines = this.css.split('\n')
  117. lineToIndex = new Array(lines.length)
  118. let prevIndex = 0
  119. for (let i = 0, l = lines.length; i < l; i++) {
  120. lineToIndex[i] = prevIndex
  121. prevIndex += lines[i].length + 1
  122. }
  123. this[fromOffsetCache] = lineToIndex
  124. } else {
  125. lineToIndex = this[fromOffsetCache]
  126. }
  127. lastLine = lineToIndex[lineToIndex.length - 1]
  128. let min = 0
  129. if (offset >= lastLine) {
  130. min = lineToIndex.length - 1
  131. } else {
  132. let max = lineToIndex.length - 2
  133. let mid
  134. while (min < max) {
  135. mid = min + ((max - min) >> 1)
  136. if (offset < lineToIndex[mid]) {
  137. max = mid - 1
  138. } else if (offset >= lineToIndex[mid + 1]) {
  139. min = mid + 1
  140. } else {
  141. min = mid
  142. break
  143. }
  144. }
  145. }
  146. return {
  147. col: offset - lineToIndex[min] + 1,
  148. line: min + 1
  149. }
  150. }
  151. mapResolve(file) {
  152. if (/^\w+:\/\//.test(file)) {
  153. return file
  154. }
  155. return resolve(this.map.consumer().sourceRoot || this.map.root || '.', file)
  156. }
  157. origin(line, column, endLine, endColumn) {
  158. if (!this.map) return false
  159. let consumer = this.map.consumer()
  160. let from = consumer.originalPositionFor({ column, line })
  161. if (!from.source) return false
  162. let to
  163. if (typeof endLine === 'number') {
  164. to = consumer.originalPositionFor({ column: endColumn, line: endLine })
  165. }
  166. let fromUrl
  167. if (isAbsolute(from.source)) {
  168. fromUrl = pathToFileURL(from.source)
  169. } else {
  170. fromUrl = new URL(
  171. from.source,
  172. this.map.consumer().sourceRoot || pathToFileURL(this.map.mapFile)
  173. )
  174. }
  175. let result = {
  176. column: from.column,
  177. endColumn: to && to.column,
  178. endLine: to && to.line,
  179. line: from.line,
  180. url: fromUrl.toString()
  181. }
  182. if (fromUrl.protocol === 'file:') {
  183. if (fileURLToPath) {
  184. result.file = fileURLToPath(fromUrl)
  185. } else {
  186. /* c8 ignore next 2 */
  187. throw new Error(`file: protocol is not available in this PostCSS build`)
  188. }
  189. }
  190. let source = consumer.sourceContentFor(from.source)
  191. if (source) result.source = source
  192. return result
  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. get from() {
  210. return this.file || this.id
  211. }
  212. }
  213. module.exports = Input
  214. Input.default = Input
  215. if (terminalHighlight && terminalHighlight.registerInput) {
  216. terminalHighlight.registerInput(Input)
  217. }