input.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 = { column, endColumn, endLine, endOffset, line, offset, source: this.css }
  127. if (this.file) {
  128. if (pathToFileURL) {
  129. result.input.url = pathToFileURL(this.file).toString()
  130. }
  131. result.input.file = this.file
  132. }
  133. return result
  134. }
  135. fromLineAndColumn(line, column) {
  136. let lineToIndex = getLineToIndex(this)
  137. let index = lineToIndex[line - 1]
  138. return index + column - 1
  139. }
  140. fromOffset(offset) {
  141. let lineToIndex = getLineToIndex(this)
  142. let lastLine = lineToIndex[lineToIndex.length - 1]
  143. let min = 0
  144. if (offset >= lastLine) {
  145. min = lineToIndex.length - 1
  146. } else {
  147. let max = lineToIndex.length - 2
  148. let mid
  149. while (min < max) {
  150. mid = min + ((max - min) >> 1)
  151. if (offset < lineToIndex[mid]) {
  152. max = mid - 1
  153. } else if (offset >= lineToIndex[mid + 1]) {
  154. min = mid + 1
  155. } else {
  156. min = mid
  157. break
  158. }
  159. }
  160. }
  161. return {
  162. col: offset - lineToIndex[min] + 1,
  163. line: min + 1
  164. }
  165. }
  166. mapResolve(file) {
  167. if (/^\w+:\/\//.test(file)) {
  168. return file
  169. }
  170. return resolve(this.map.consumer().sourceRoot || this.map.root || '.', file)
  171. }
  172. origin(line, column, endLine, endColumn) {
  173. if (!this.map) return false
  174. let consumer = this.map.consumer()
  175. let from = consumer.originalPositionFor({ column, line })
  176. if (!from.source) return false
  177. let to
  178. if (typeof endLine === 'number') {
  179. to = consumer.originalPositionFor({ column: endColumn, line: endLine })
  180. }
  181. let fromUrl
  182. if (isAbsolute(from.source)) {
  183. fromUrl = pathToFileURL(from.source)
  184. } else {
  185. fromUrl = new URL(
  186. from.source,
  187. this.map.consumer().sourceRoot || pathToFileURL(this.map.mapFile)
  188. )
  189. }
  190. let result = {
  191. column: from.column,
  192. endColumn: to && to.column,
  193. endLine: to && to.line,
  194. line: from.line,
  195. url: fromUrl.toString()
  196. }
  197. if (fromUrl.protocol === 'file:') {
  198. if (fileURLToPath) {
  199. result.file = fileURLToPath(fromUrl)
  200. } else {
  201. /* c8 ignore next 2 */
  202. throw new Error(`file: protocol is not available in this PostCSS build`)
  203. }
  204. }
  205. let source = consumer.sourceContentFor(from.source)
  206. if (source) result.source = source
  207. return result
  208. }
  209. toJSON() {
  210. let json = {}
  211. for (let name of ['hasBOM', 'css', 'file', 'id']) {
  212. if (this[name] != null) {
  213. json[name] = this[name]
  214. }
  215. }
  216. if (this.map) {
  217. json.map = { ...this.map }
  218. if (json.map.consumerCache) {
  219. json.map.consumerCache = undefined
  220. }
  221. }
  222. return json
  223. }
  224. }
  225. module.exports = Input
  226. Input.default = Input
  227. if (terminalHighlight && terminalHighlight.registerInput) {
  228. terminalHighlight.registerInput(Input)
  229. }