input.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. 'use strict'
  2. let { SourceMapConsumer, SourceMapGenerator } = require('source-map-js')
  3. let { fileURLToPath, pathToFileURL } = require('url')
  4. let { isAbsolute, resolve } = require('path')
  5. let { nanoid } = require('nanoid/non-secure')
  6. let terminalHighlight = require('./terminal-highlight')
  7. let CssSyntaxError = require('./css-syntax-error')
  8. let PreviousMap = require('./previous-map')
  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 result, endLine, endColumn
  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. get from() {
  112. return this.file || this.id
  113. }
  114. fromOffset(offset) {
  115. let lastLine, lineToIndex
  116. if (!this[fromOffsetCache]) {
  117. let lines = this.css.split('\n')
  118. lineToIndex = new Array(lines.length)
  119. let prevIndex = 0
  120. for (let i = 0, l = lines.length; i < l; i++) {
  121. lineToIndex[i] = prevIndex
  122. prevIndex += lines[i].length + 1
  123. }
  124. this[fromOffsetCache] = lineToIndex
  125. } else {
  126. lineToIndex = this[fromOffsetCache]
  127. }
  128. lastLine = lineToIndex[lineToIndex.length - 1]
  129. let min = 0
  130. if (offset >= lastLine) {
  131. min = lineToIndex.length - 1
  132. } else {
  133. let max = lineToIndex.length - 2
  134. let mid
  135. while (min < max) {
  136. mid = min + ((max - min) >> 1)
  137. if (offset < lineToIndex[mid]) {
  138. max = mid - 1
  139. } else if (offset >= lineToIndex[mid + 1]) {
  140. min = mid + 1
  141. } else {
  142. min = mid
  143. break
  144. }
  145. }
  146. }
  147. return {
  148. col: offset - lineToIndex[min] + 1,
  149. line: min + 1
  150. }
  151. }
  152. mapResolve(file) {
  153. if (/^\w+:\/\//.test(file)) {
  154. return file
  155. }
  156. return resolve(this.map.consumer().sourceRoot || this.map.root || '.', file)
  157. }
  158. origin(line, column, endLine, endColumn) {
  159. if (!this.map) return false
  160. let consumer = this.map.consumer()
  161. let from = consumer.originalPositionFor({ column, line })
  162. if (!from.source) return false
  163. let to
  164. if (typeof endLine === 'number') {
  165. to = consumer.originalPositionFor({ column: endColumn, line: endLine })
  166. }
  167. let fromUrl
  168. if (isAbsolute(from.source)) {
  169. fromUrl = pathToFileURL(from.source)
  170. } else {
  171. fromUrl = new URL(
  172. from.source,
  173. this.map.consumer().sourceRoot || pathToFileURL(this.map.mapFile)
  174. )
  175. }
  176. let result = {
  177. column: from.column,
  178. endColumn: to && to.column,
  179. endLine: to && to.line,
  180. line: from.line,
  181. url: fromUrl.toString()
  182. }
  183. if (fromUrl.protocol === 'file:') {
  184. if (fileURLToPath) {
  185. result.file = fileURLToPath(fromUrl)
  186. } else {
  187. /* c8 ignore next 2 */
  188. throw new Error(`file: protocol is not available in this PostCSS build`)
  189. }
  190. }
  191. let source = consumer.sourceContentFor(from.source)
  192. if (source) result.source = source
  193. return result
  194. }
  195. toJSON() {
  196. let json = {}
  197. for (let name of ['hasBOM', 'css', 'file', 'id']) {
  198. if (this[name] != null) {
  199. json[name] = this[name]
  200. }
  201. }
  202. if (this.map) {
  203. json.map = { ...this.map }
  204. if (json.map.consumerCache) {
  205. json.map.consumerCache = undefined
  206. }
  207. }
  208. return json
  209. }
  210. }
  211. module.exports = Input
  212. Input.default = Input
  213. if (terminalHighlight && terminalHighlight.registerInput) {
  214. terminalHighlight.registerInput(Input)
  215. }