input.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. 'use strict'
  2. let { fileURLToPath, pathToFileURL } = require('url')
  3. let { resolve, isAbsolute } = require('path')
  4. let { nanoid } = require('nanoid/non-secure')
  5. let terminalHighlight = require('./terminal-highlight')
  6. let CssSyntaxError = require('./css-syntax-error')
  7. let PreviousMap = require('./previous-map')
  8. let fromOffsetCache = Symbol('fromOffset cache')
  9. class Input {
  10. constructor (css, opts = {}) {
  11. if (
  12. css === null ||
  13. typeof css === 'undefined' ||
  14. (typeof css === 'object' && !css.toString)
  15. ) {
  16. throw new Error(`PostCSS received ${css} instead of CSS string`)
  17. }
  18. this.css = css.toString()
  19. if (this.css[0] === '\uFEFF' || this.css[0] === '\uFFFE') {
  20. this.hasBOM = true
  21. this.css = this.css.slice(1)
  22. } else {
  23. this.hasBOM = false
  24. }
  25. if (opts.from) {
  26. if (/^\w+:\/\//.test(opts.from) || isAbsolute(opts.from)) {
  27. this.file = opts.from
  28. } else {
  29. this.file = resolve(opts.from)
  30. }
  31. }
  32. let map = new PreviousMap(this.css, opts)
  33. if (map.text) {
  34. this.map = map
  35. let file = map.consumer().file
  36. if (!this.file && file) this.file = this.mapResolve(file)
  37. }
  38. if (!this.file) {
  39. this.id = '<input css ' + nanoid(6) + '>'
  40. }
  41. if (this.map) this.map.file = this.from
  42. }
  43. fromOffset (offset) {
  44. let lastLine, lineToIndex
  45. if (!this[fromOffsetCache]) {
  46. let lines = this.css.split('\n')
  47. lineToIndex = new Array(lines.length)
  48. let prevIndex = 0
  49. for (let i = 0, l = lines.length; i < l; i++) {
  50. lineToIndex[i] = prevIndex
  51. prevIndex += lines[i].length + 1
  52. }
  53. this[fromOffsetCache] = lineToIndex
  54. } else {
  55. lineToIndex = this[fromOffsetCache]
  56. }
  57. lastLine = lineToIndex[lineToIndex.length - 1]
  58. let min = 0
  59. if (offset >= lastLine) {
  60. min = lineToIndex.length - 1
  61. } else {
  62. let max = lineToIndex.length - 2
  63. let mid
  64. while (min < max) {
  65. mid = min + ((max - min) >> 1)
  66. if (offset < lineToIndex[mid]) {
  67. max = mid - 1
  68. } else if (offset >= lineToIndex[mid + 1]) {
  69. min = mid + 1
  70. } else {
  71. min = mid
  72. break
  73. }
  74. }
  75. }
  76. return {
  77. line: min + 1,
  78. col: offset - lineToIndex[min] + 1
  79. }
  80. }
  81. error (message, line, column, opts = {}) {
  82. let result
  83. if (!column) {
  84. let pos = this.fromOffset(line)
  85. line = pos.line
  86. column = pos.col
  87. }
  88. let origin = this.origin(line, column)
  89. if (origin) {
  90. result = new CssSyntaxError(
  91. message,
  92. origin.line,
  93. origin.column,
  94. origin.source,
  95. origin.file,
  96. opts.plugin
  97. )
  98. } else {
  99. result = new CssSyntaxError(
  100. message,
  101. line,
  102. column,
  103. this.css,
  104. this.file,
  105. opts.plugin
  106. )
  107. }
  108. result.input = { line, column, source: this.css }
  109. if (this.file) {
  110. result.input.url = pathToFileURL(this.file).toString()
  111. result.input.file = this.file
  112. }
  113. return result
  114. }
  115. origin (line, column) {
  116. if (!this.map) return false
  117. let consumer = this.map.consumer()
  118. let from = consumer.originalPositionFor({ line, column })
  119. if (!from.source) return false
  120. let fromUrl
  121. if (isAbsolute(from.source)) {
  122. fromUrl = pathToFileURL(from.source)
  123. } else {
  124. fromUrl = new URL(
  125. from.source,
  126. this.map.consumer().sourceRoot || pathToFileURL(this.map.mapFile)
  127. )
  128. }
  129. let result = {
  130. url: fromUrl.toString(),
  131. line: from.line,
  132. column: from.column
  133. }
  134. if (fromUrl.protocol === 'file:') {
  135. result.file = fileURLToPath(fromUrl)
  136. }
  137. let source = consumer.sourceContentFor(from.source)
  138. if (source) result.source = source
  139. return result
  140. }
  141. mapResolve (file) {
  142. if (/^\w+:\/\//.test(file)) {
  143. return file
  144. }
  145. return resolve(this.map.consumer().sourceRoot || this.map.root || '.', file)
  146. }
  147. get from () {
  148. return this.file || this.id
  149. }
  150. toJSON () {
  151. let json = {}
  152. for (let name of ['hasBOM', 'css', 'file', 'id']) {
  153. if (this[name] != null) {
  154. json[name] = this[name]
  155. }
  156. }
  157. if (this.map) {
  158. json.map = { ...this.map }
  159. if (json.map.consumerCache) {
  160. json.map.consumerCache = undefined
  161. }
  162. }
  163. return json
  164. }
  165. }
  166. module.exports = Input
  167. Input.default = Input
  168. if (terminalHighlight && terminalHighlight.registerInput) {
  169. terminalHighlight.registerInput(Input)
  170. }