You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

434 lines
9.7 KiB

1 month ago
  1. 'use strict'
  2. let CssSyntaxError = require('./css-syntax-error')
  3. let Stringifier = require('./stringifier')
  4. let stringify = require('./stringify')
  5. let { isClean, my } = require('./symbols')
  6. function cloneNode(obj, parent) {
  7. let cloned = new obj.constructor()
  8. for (let i in obj) {
  9. if (!Object.prototype.hasOwnProperty.call(obj, i)) {
  10. /* c8 ignore next 2 */
  11. continue
  12. }
  13. if (i === 'proxyCache') continue
  14. let value = obj[i]
  15. let type = typeof value
  16. if (i === 'parent' && type === 'object') {
  17. if (parent) cloned[i] = parent
  18. } else if (i === 'source') {
  19. cloned[i] = value
  20. } else if (Array.isArray(value)) {
  21. cloned[i] = value.map(j => cloneNode(j, cloned))
  22. } else {
  23. if (type === 'object' && value !== null) value = cloneNode(value)
  24. cloned[i] = value
  25. }
  26. }
  27. return cloned
  28. }
  29. function sourceOffset(inputCSS, position) {
  30. // Not all custom syntaxes support `offset` in `source.start` and `source.end`
  31. if (
  32. position &&
  33. typeof position.offset !== 'undefined'
  34. ) {
  35. return position.offset;
  36. }
  37. let column = 1
  38. let line = 1
  39. let offset = 0
  40. for (let i = 0; i < inputCSS.length; i++) {
  41. if (line === position.line && column === position.column) {
  42. offset = i
  43. break
  44. }
  45. if (inputCSS[i] === '\n') {
  46. column = 1
  47. line += 1
  48. } else {
  49. column += 1
  50. }
  51. }
  52. return offset
  53. }
  54. class Node {
  55. constructor(defaults = {}) {
  56. this.raws = {}
  57. this[isClean] = false
  58. this[my] = true
  59. for (let name in defaults) {
  60. if (name === 'nodes') {
  61. this.nodes = []
  62. for (let node of defaults[name]) {
  63. if (typeof node.clone === 'function') {
  64. this.append(node.clone())
  65. } else {
  66. this.append(node)
  67. }
  68. }
  69. } else {
  70. this[name] = defaults[name]
  71. }
  72. }
  73. }
  74. addToError(error) {
  75. error.postcssNode = this
  76. if (error.stack && this.source && /\n\s{4}at /.test(error.stack)) {
  77. let s = this.source
  78. error.stack = error.stack.replace(
  79. /\n\s{4}at /,
  80. `$&${s.input.from}:${s.start.line}:${s.start.column}$&`
  81. )
  82. }
  83. return error
  84. }
  85. after(add) {
  86. this.parent.insertAfter(this, add)
  87. return this
  88. }
  89. assign(overrides = {}) {
  90. for (let name in overrides) {
  91. this[name] = overrides[name]
  92. }
  93. return this
  94. }
  95. before(add) {
  96. this.parent.insertBefore(this, add)
  97. return this
  98. }
  99. cleanRaws(keepBetween) {
  100. delete this.raws.before
  101. delete this.raws.after
  102. if (!keepBetween) delete this.raws.between
  103. }
  104. clone(overrides = {}) {
  105. let cloned = cloneNode(this)
  106. for (let name in overrides) {
  107. cloned[name] = overrides[name]
  108. }
  109. return cloned
  110. }
  111. cloneAfter(overrides = {}) {
  112. let cloned = this.clone(overrides)
  113. this.parent.insertAfter(this, cloned)
  114. return cloned
  115. }
  116. cloneBefore(overrides = {}) {
  117. let cloned = this.clone(overrides)
  118. this.parent.insertBefore(this, cloned)
  119. return cloned
  120. }
  121. error(message, opts = {}) {
  122. if (this.source) {
  123. let { end, start } = this.rangeBy(opts)
  124. return this.source.input.error(
  125. message,
  126. { column: start.column, line: start.line },
  127. { column: end.column, line: end.line },
  128. opts
  129. )
  130. }
  131. return new CssSyntaxError(message)
  132. }
  133. getProxyProcessor() {
  134. return {
  135. get(node, prop) {
  136. if (prop === 'proxyOf') {
  137. return node
  138. } else if (prop === 'root') {
  139. return () => node.root().toProxy()
  140. } else {
  141. return node[prop]
  142. }
  143. },
  144. set(node, prop, value) {
  145. if (node[prop] === value) return true
  146. node[prop] = value
  147. if (
  148. prop === 'prop' ||
  149. prop === 'value' ||
  150. prop === 'name' ||
  151. prop === 'params' ||
  152. prop === 'important' ||
  153. /* c8 ignore next */
  154. prop === 'text'
  155. ) {
  156. node.markDirty()
  157. }
  158. return true
  159. }
  160. }
  161. }
  162. /* c8 ignore next 3 */
  163. markClean() {
  164. this[isClean] = true
  165. }
  166. markDirty() {
  167. if (this[isClean]) {
  168. this[isClean] = false
  169. let next = this
  170. while ((next = next.parent)) {
  171. next[isClean] = false
  172. }
  173. }
  174. }
  175. next() {
  176. if (!this.parent) return undefined
  177. let index = this.parent.index(this)
  178. return this.parent.nodes[index + 1]
  179. }
  180. positionBy(opts) {
  181. let pos = this.source.start
  182. if (opts.index) {
  183. pos = this.positionInside(opts.index)
  184. } else if (opts.word) {
  185. let inputString = ('document' in this.source.input)
  186. ? this.source.input.document
  187. : this.source.input.css
  188. let stringRepresentation = inputString.slice(
  189. sourceOffset(inputString, this.source.start),
  190. sourceOffset(inputString, this.source.end)
  191. )
  192. let index = stringRepresentation.indexOf(opts.word)
  193. if (index !== -1) pos = this.positionInside(index)
  194. }
  195. return pos
  196. }
  197. positionInside(index) {
  198. let column = this.source.start.column
  199. let line = this.source.start.line
  200. let inputString = ('document' in this.source.input)
  201. ? this.source.input.document
  202. : this.source.input.css
  203. let offset = sourceOffset(inputString, this.source.start)
  204. let end = offset + index
  205. for (let i = offset; i < end; i++) {
  206. if (inputString[i] === '\n') {
  207. column = 1
  208. line += 1
  209. } else {
  210. column += 1
  211. }
  212. }
  213. return { column, line }
  214. }
  215. prev() {
  216. if (!this.parent) return undefined
  217. let index = this.parent.index(this)
  218. return this.parent.nodes[index - 1]
  219. }
  220. rangeBy(opts) {
  221. let start = {
  222. column: this.source.start.column,
  223. line: this.source.start.line
  224. }
  225. let end = this.source.end
  226. ? {
  227. column: this.source.end.column + 1,
  228. line: this.source.end.line
  229. }
  230. : {
  231. column: start.column + 1,
  232. line: start.line
  233. }
  234. if (opts.word) {
  235. let inputString = ('document' in this.source.input)
  236. ? this.source.input.document
  237. : this.source.input.css
  238. let stringRepresentation = inputString.slice(
  239. sourceOffset(inputString, this.source.start),
  240. sourceOffset(inputString, this.source.end)
  241. )
  242. let index = stringRepresentation.indexOf(opts.word)
  243. if (index !== -1) {
  244. start = this.positionInside(index)
  245. end = this.positionInside(
  246. index + opts.word.length,
  247. )
  248. }
  249. } else {
  250. if (opts.start) {
  251. start = {
  252. column: opts.start.column,
  253. line: opts.start.line
  254. }
  255. } else if (opts.index) {
  256. start = this.positionInside(opts.index)
  257. }
  258. if (opts.end) {
  259. end = {
  260. column: opts.end.column,
  261. line: opts.end.line
  262. }
  263. } else if (typeof opts.endIndex === 'number') {
  264. end = this.positionInside(opts.endIndex)
  265. } else if (opts.index) {
  266. end = this.positionInside(opts.index + 1)
  267. }
  268. }
  269. if (
  270. end.line < start.line ||
  271. (end.line === start.line && end.column <= start.column)
  272. ) {
  273. end = { column: start.column + 1, line: start.line }
  274. }
  275. return { end, start }
  276. }
  277. raw(prop, defaultType) {
  278. let str = new Stringifier()
  279. return str.raw(this, prop, defaultType)
  280. }
  281. remove() {
  282. if (this.parent) {
  283. this.parent.removeChild(this)
  284. }
  285. this.parent = undefined
  286. return this
  287. }
  288. replaceWith(...nodes) {
  289. if (this.parent) {
  290. let bookmark = this
  291. let foundSelf = false
  292. for (let node of nodes) {
  293. if (node === this) {
  294. foundSelf = true
  295. } else if (foundSelf) {
  296. this.parent.insertAfter(bookmark, node)
  297. bookmark = node
  298. } else {
  299. this.parent.insertBefore(bookmark, node)
  300. }
  301. }
  302. if (!foundSelf) {
  303. this.remove()
  304. }
  305. }
  306. return this
  307. }
  308. root() {
  309. let result = this
  310. while (result.parent && result.parent.type !== 'document') {
  311. result = result.parent
  312. }
  313. return result
  314. }
  315. toJSON(_, inputs) {
  316. let fixed = {}
  317. let emitInputs = inputs == null
  318. inputs = inputs || new Map()
  319. let inputsNextIndex = 0
  320. for (let name in this) {
  321. if (!Object.prototype.hasOwnProperty.call(this, name)) {
  322. /* c8 ignore next 2 */
  323. continue
  324. }
  325. if (name === 'parent' || name === 'proxyCache') continue
  326. let value = this[name]
  327. if (Array.isArray(value)) {
  328. fixed[name] = value.map(i => {
  329. if (typeof i === 'object' && i.toJSON) {
  330. return i.toJSON(null, inputs)
  331. } else {
  332. return i
  333. }
  334. })
  335. } else if (typeof value === 'object' && value.toJSON) {
  336. fixed[name] = value.toJSON(null, inputs)
  337. } else if (name === 'source') {
  338. let inputId = inputs.get(value.input)
  339. if (inputId == null) {
  340. inputId = inputsNextIndex
  341. inputs.set(value.input, inputsNextIndex)
  342. inputsNextIndex++
  343. }
  344. fixed[name] = {
  345. end: value.end,
  346. inputId,
  347. start: value.start
  348. }
  349. } else {
  350. fixed[name] = value
  351. }
  352. }
  353. if (emitInputs) {
  354. fixed.inputs = [...inputs.keys()].map(input => input.toJSON())
  355. }
  356. return fixed
  357. }
  358. toProxy() {
  359. if (!this.proxyCache) {
  360. this.proxyCache = new Proxy(this, this.getProxyProcessor())
  361. }
  362. return this.proxyCache
  363. }
  364. toString(stringifier = stringify) {
  365. if (stringifier.stringify) stringifier = stringifier.stringify
  366. let result = ''
  367. stringifier(this, i => {
  368. result += i
  369. })
  370. return result
  371. }
  372. warn(result, text, opts) {
  373. let data = { node: this }
  374. for (let i in opts) data[i] = opts[i]
  375. return result.warn(text, data)
  376. }
  377. get proxyOf() {
  378. return this
  379. }
  380. }
  381. module.exports = Node
  382. Node.default = Node