input.js 6.6 KB

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