node.js 10 KB

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