input.js 4.9 KB

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