input.js 6.0 KB

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