node.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 stringRepresentation = this.source.input.css.slice(
  186. sourceOffset(this.source.input.css, this.source.start),
  187. sourceOffset(this.source.input.css, this.source.end)
  188. )
  189. let index = stringRepresentation.indexOf(opts.word)
  190. if (index !== -1) pos = this.positionInside(index)
  191. }
  192. return pos
  193. }
  194. positionInside(index) {
  195. let column = this.source.start.column
  196. let line = this.source.start.line
  197. let offset = sourceOffset(this.source.input.css, this.source.start)
  198. let end = offset + index
  199. for (let i = offset; i < end; i++) {
  200. if (this.source.input.css[i] === '\n') {
  201. column = 1
  202. line += 1
  203. } else {
  204. column += 1
  205. }
  206. }
  207. return { column, line }
  208. }
  209. prev() {
  210. if (!this.parent) return undefined
  211. let index = this.parent.index(this)
  212. return this.parent.nodes[index - 1]
  213. }
  214. rangeBy(opts) {
  215. let start = {
  216. column: this.source.start.column,
  217. line: this.source.start.line
  218. }
  219. let end = this.source.end
  220. ? {
  221. column: this.source.end.column + 1,
  222. line: this.source.end.line
  223. }
  224. : {
  225. column: start.column + 1,
  226. line: start.line
  227. }
  228. if (opts.word) {
  229. let stringRepresentation = this.source.input.css.slice(
  230. sourceOffset(this.source.input.css, this.source.start),
  231. sourceOffset(this.source.input.css, this.source.end)
  232. )
  233. let index = stringRepresentation.indexOf(opts.word)
  234. if (index !== -1) {
  235. start = this.positionInside(index)
  236. end = this.positionInside(
  237. index + opts.word.length,
  238. )
  239. }
  240. } else {
  241. if (opts.start) {
  242. start = {
  243. column: opts.start.column,
  244. line: opts.start.line
  245. }
  246. } else if (opts.index) {
  247. start = this.positionInside(opts.index)
  248. }
  249. if (opts.end) {
  250. end = {
  251. column: opts.end.column,
  252. line: opts.end.line
  253. }
  254. } else if (typeof opts.endIndex === 'number') {
  255. end = this.positionInside(opts.endIndex)
  256. } else if (opts.index) {
  257. end = this.positionInside(opts.index + 1)
  258. }
  259. }
  260. if (
  261. end.line < start.line ||
  262. (end.line === start.line && end.column <= start.column)
  263. ) {
  264. end = { column: start.column + 1, line: start.line }
  265. }
  266. return { end, start }
  267. }
  268. raw(prop, defaultType) {
  269. let str = new Stringifier()
  270. return str.raw(this, prop, defaultType)
  271. }
  272. remove() {
  273. if (this.parent) {
  274. this.parent.removeChild(this)
  275. }
  276. this.parent = undefined
  277. return this
  278. }
  279. replaceWith(...nodes) {
  280. if (this.parent) {
  281. let bookmark = this
  282. let foundSelf = false
  283. for (let node of nodes) {
  284. if (node === this) {
  285. foundSelf = true
  286. } else if (foundSelf) {
  287. this.parent.insertAfter(bookmark, node)
  288. bookmark = node
  289. } else {
  290. this.parent.insertBefore(bookmark, node)
  291. }
  292. }
  293. if (!foundSelf) {
  294. this.remove()
  295. }
  296. }
  297. return this
  298. }
  299. root() {
  300. let result = this
  301. while (result.parent && result.parent.type !== 'document') {
  302. result = result.parent
  303. }
  304. return result
  305. }
  306. toJSON(_, inputs) {
  307. let fixed = {}
  308. let emitInputs = inputs == null
  309. inputs = inputs || new Map()
  310. let inputsNextIndex = 0
  311. for (let name in this) {
  312. if (!Object.prototype.hasOwnProperty.call(this, name)) {
  313. /* c8 ignore next 2 */
  314. continue
  315. }
  316. if (name === 'parent' || name === 'proxyCache') continue
  317. let value = this[name]
  318. if (Array.isArray(value)) {
  319. fixed[name] = value.map(i => {
  320. if (typeof i === 'object' && i.toJSON) {
  321. return i.toJSON(null, inputs)
  322. } else {
  323. return i
  324. }
  325. })
  326. } else if (typeof value === 'object' && value.toJSON) {
  327. fixed[name] = value.toJSON(null, inputs)
  328. } else if (name === 'source') {
  329. let inputId = inputs.get(value.input)
  330. if (inputId == null) {
  331. inputId = inputsNextIndex
  332. inputs.set(value.input, inputsNextIndex)
  333. inputsNextIndex++
  334. }
  335. fixed[name] = {
  336. end: value.end,
  337. inputId,
  338. start: value.start
  339. }
  340. } else {
  341. fixed[name] = value
  342. }
  343. }
  344. if (emitInputs) {
  345. fixed.inputs = [...inputs.keys()].map(input => input.toJSON())
  346. }
  347. return fixed
  348. }
  349. toProxy() {
  350. if (!this.proxyCache) {
  351. this.proxyCache = new Proxy(this, this.getProxyProcessor())
  352. }
  353. return this.proxyCache
  354. }
  355. toString(stringifier = stringify) {
  356. if (stringifier.stringify) stringifier = stringifier.stringify
  357. let result = ''
  358. stringifier(this, i => {
  359. result += i
  360. })
  361. return result
  362. }
  363. warn(result, text, opts) {
  364. let data = { node: this }
  365. for (let i in opts) data[i] = opts[i]
  366. return result.warn(text, data)
  367. }
  368. get proxyOf() {
  369. return this
  370. }
  371. }
  372. module.exports = Node
  373. Node.default = Node