node.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. 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. /* c8 ignore next 3 */
  138. markClean() {
  139. this[isClean] = true
  140. }
  141. markDirty() {
  142. if (this[isClean]) {
  143. this[isClean] = false
  144. let next = this
  145. while ((next = next.parent)) {
  146. next[isClean] = false
  147. }
  148. }
  149. }
  150. next() {
  151. if (!this.parent) return undefined
  152. let index = this.parent.index(this)
  153. return this.parent.nodes[index + 1]
  154. }
  155. positionBy(opts, stringRepresentation) {
  156. let pos = this.source.start
  157. if (opts.index) {
  158. pos = this.positionInside(opts.index, stringRepresentation)
  159. } else if (opts.word) {
  160. stringRepresentation = this.toString()
  161. let index = stringRepresentation.indexOf(opts.word)
  162. if (index !== -1) pos = this.positionInside(index, stringRepresentation)
  163. }
  164. return pos
  165. }
  166. positionInside(index, stringRepresentation) {
  167. let string = stringRepresentation || this.toString()
  168. let column = this.source.start.column
  169. let line = this.source.start.line
  170. for (let i = 0; i < index; i++) {
  171. if (string[i] === '\n') {
  172. column = 1
  173. line += 1
  174. } else {
  175. column += 1
  176. }
  177. }
  178. return { column, line }
  179. }
  180. prev() {
  181. if (!this.parent) return undefined
  182. let index = this.parent.index(this)
  183. return this.parent.nodes[index - 1]
  184. }
  185. rangeBy(opts) {
  186. let start = {
  187. column: this.source.start.column,
  188. line: this.source.start.line
  189. }
  190. let end = this.source.end
  191. ? {
  192. column: this.source.end.column + 1,
  193. line: this.source.end.line
  194. }
  195. : {
  196. column: start.column + 1,
  197. line: start.line
  198. }
  199. if (opts.word) {
  200. let stringRepresentation = this.toString()
  201. let index = stringRepresentation.indexOf(opts.word)
  202. if (index !== -1) {
  203. start = this.positionInside(index, stringRepresentation)
  204. end = this.positionInside(
  205. index + opts.word.length,
  206. stringRepresentation
  207. )
  208. }
  209. } else {
  210. if (opts.start) {
  211. start = {
  212. column: opts.start.column,
  213. line: opts.start.line
  214. }
  215. } else if (opts.index) {
  216. start = this.positionInside(opts.index)
  217. }
  218. if (opts.end) {
  219. end = {
  220. column: opts.end.column,
  221. line: opts.end.line
  222. }
  223. } else if (typeof opts.endIndex === 'number') {
  224. end = this.positionInside(opts.endIndex)
  225. } else if (opts.index) {
  226. end = this.positionInside(opts.index + 1)
  227. }
  228. }
  229. if (
  230. end.line < start.line ||
  231. (end.line === start.line && end.column <= start.column)
  232. ) {
  233. end = { column: start.column + 1, line: start.line }
  234. }
  235. return { end, start }
  236. }
  237. raw(prop, defaultType) {
  238. let str = new Stringifier()
  239. return str.raw(this, prop, defaultType)
  240. }
  241. remove() {
  242. if (this.parent) {
  243. this.parent.removeChild(this)
  244. }
  245. this.parent = undefined
  246. return this
  247. }
  248. replaceWith(...nodes) {
  249. if (this.parent) {
  250. let bookmark = this
  251. let foundSelf = false
  252. for (let node of nodes) {
  253. if (node === this) {
  254. foundSelf = true
  255. } else if (foundSelf) {
  256. this.parent.insertAfter(bookmark, node)
  257. bookmark = node
  258. } else {
  259. this.parent.insertBefore(bookmark, node)
  260. }
  261. }
  262. if (!foundSelf) {
  263. this.remove()
  264. }
  265. }
  266. return this
  267. }
  268. root() {
  269. let result = this
  270. while (result.parent && result.parent.type !== 'document') {
  271. result = result.parent
  272. }
  273. return result
  274. }
  275. toJSON(_, inputs) {
  276. let fixed = {}
  277. let emitInputs = inputs == null
  278. inputs = inputs || new Map()
  279. let inputsNextIndex = 0
  280. for (let name in this) {
  281. if (!Object.prototype.hasOwnProperty.call(this, name)) {
  282. /* c8 ignore next 2 */
  283. continue
  284. }
  285. if (name === 'parent' || name === 'proxyCache') continue
  286. let value = this[name]
  287. if (Array.isArray(value)) {
  288. fixed[name] = value.map(i => {
  289. if (typeof i === 'object' && i.toJSON) {
  290. return i.toJSON(null, inputs)
  291. } else {
  292. return i
  293. }
  294. })
  295. } else if (typeof value === 'object' && value.toJSON) {
  296. fixed[name] = value.toJSON(null, inputs)
  297. } else if (name === 'source') {
  298. let inputId = inputs.get(value.input)
  299. if (inputId == null) {
  300. inputId = inputsNextIndex
  301. inputs.set(value.input, inputsNextIndex)
  302. inputsNextIndex++
  303. }
  304. fixed[name] = {
  305. end: value.end,
  306. inputId,
  307. start: value.start
  308. }
  309. } else {
  310. fixed[name] = value
  311. }
  312. }
  313. if (emitInputs) {
  314. fixed.inputs = [...inputs.keys()].map(input => input.toJSON())
  315. }
  316. return fixed
  317. }
  318. toProxy() {
  319. if (!this.proxyCache) {
  320. this.proxyCache = new Proxy(this, this.getProxyProcessor())
  321. }
  322. return this.proxyCache
  323. }
  324. toString(stringifier = stringify) {
  325. if (stringifier.stringify) stringifier = stringifier.stringify
  326. let result = ''
  327. stringifier(this, i => {
  328. result += i
  329. })
  330. return result
  331. }
  332. warn(result, text, opts) {
  333. let data = { node: this }
  334. for (let i in opts) data[i] = opts[i]
  335. return result.warn(text, data)
  336. }
  337. get proxyOf() {
  338. return this
  339. }
  340. }
  341. module.exports = Node
  342. Node.default = Node