node.js 8.5 KB

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