container.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. 'use strict'
  2. let Comment = require('./comment')
  3. let Declaration = require('./declaration')
  4. let Node = require('./node')
  5. let { isClean, my } = require('./symbols')
  6. let AtRule, parse, Root, Rule
  7. function cleanSource(nodes) {
  8. let stack = nodes.slice()
  9. while (stack.length > 0) {
  10. let node = stack.pop()
  11. delete node.source
  12. if (node.nodes) {
  13. node.nodes = node.nodes.slice()
  14. for (let i of node.nodes) stack.push(i)
  15. }
  16. }
  17. return nodes.slice()
  18. }
  19. function markTreeDirty(node) {
  20. let stack = [node]
  21. while (stack.length > 0) {
  22. let next = stack.pop()
  23. next[isClean] = false
  24. if (next.proxyOf.nodes) {
  25. for (let i of next.proxyOf.nodes) stack.push(i)
  26. }
  27. }
  28. }
  29. class Container extends Node {
  30. get first() {
  31. if (!this.proxyOf.nodes) return undefined
  32. return this.proxyOf.nodes[0]
  33. }
  34. get last() {
  35. if (!this.proxyOf.nodes) return undefined
  36. return this.proxyOf.nodes[this.proxyOf.nodes.length - 1]
  37. }
  38. append(...children) {
  39. for (let child of children) {
  40. let nodes = this.normalize(child, this.last)
  41. for (let node of nodes) this.proxyOf.nodes.push(node)
  42. }
  43. this.markDirty()
  44. return this
  45. }
  46. cleanRaws(keepBetween) {
  47. let stack = [this]
  48. while (stack.length > 0) {
  49. let node = stack.pop()
  50. if (node !== this && node.cleanRaws !== Container.prototype.cleanRaws) {
  51. // Subclass with own logic; let it handle its subtree
  52. node.cleanRaws(keepBetween)
  53. continue
  54. }
  55. Node.prototype.cleanRaws.call(node, keepBetween)
  56. if (node.nodes) {
  57. for (let child of node.nodes) stack.push(child)
  58. }
  59. }
  60. }
  61. each(callback) {
  62. if (!this.proxyOf.nodes) return undefined
  63. let iterator = this.getIterator()
  64. let index, result
  65. while (this.indexes[iterator] < this.proxyOf.nodes.length) {
  66. index = this.indexes[iterator]
  67. result = callback(this.proxyOf.nodes[index], index)
  68. if (result === false) break
  69. this.indexes[iterator] += 1
  70. }
  71. delete this.indexes[iterator]
  72. return result
  73. }
  74. every(condition) {
  75. return this.nodes.every(condition)
  76. }
  77. getIterator() {
  78. if (!this.lastEach) this.lastEach = 0
  79. if (!this.indexes) this.indexes = {}
  80. this.lastEach += 1
  81. let iterator = this.lastEach
  82. this.indexes[iterator] = 0
  83. return iterator
  84. }
  85. getProxyProcessor() {
  86. return {
  87. get(node, prop) {
  88. if (prop === 'proxyOf') {
  89. return node
  90. } else if (!node[prop]) {
  91. return node[prop]
  92. } else if (
  93. prop === 'each' ||
  94. (typeof prop === 'string' && prop.startsWith('walk'))
  95. ) {
  96. return (...args) => {
  97. return node[prop](
  98. ...args.map(i => {
  99. if (typeof i === 'function') {
  100. return (child, index) => i(child.toProxy(), index)
  101. } else {
  102. return i
  103. }
  104. })
  105. )
  106. }
  107. } else if (prop === 'every' || prop === 'some') {
  108. return cb => {
  109. return node[prop]((child, ...other) =>
  110. cb(child.toProxy(), ...other)
  111. )
  112. }
  113. } else if (prop === 'root') {
  114. return () => node.root().toProxy()
  115. } else if (prop === 'nodes') {
  116. return node.nodes.map(i => i.toProxy())
  117. } else if (prop === 'first' || prop === 'last') {
  118. return node[prop].toProxy()
  119. } else {
  120. return node[prop]
  121. }
  122. },
  123. set(node, prop, value) {
  124. if (node[prop] === value) return true
  125. node[prop] = value
  126. if (prop === 'name' || prop === 'params' || prop === 'selector') {
  127. node.markDirty()
  128. }
  129. return true
  130. }
  131. }
  132. }
  133. index(child) {
  134. if (typeof child === 'number') return child
  135. if (child.proxyOf) child = child.proxyOf
  136. return this.proxyOf.nodes.indexOf(child)
  137. }
  138. insertAfter(exist, add) {
  139. let existIndex = this.index(exist)
  140. let nodes = this.normalize(add, this.proxyOf.nodes[existIndex]).reverse()
  141. existIndex = this.index(exist)
  142. for (let node of nodes) this.proxyOf.nodes.splice(existIndex + 1, 0, node)
  143. let index
  144. for (let id in this.indexes) {
  145. index = this.indexes[id]
  146. if (existIndex < index) {
  147. this.indexes[id] = index + nodes.length
  148. }
  149. }
  150. this.markDirty()
  151. return this
  152. }
  153. insertBefore(exist, add) {
  154. let existIndex = this.index(exist)
  155. let type = existIndex === 0 ? 'prepend' : false
  156. let nodes = this.normalize(
  157. add,
  158. this.proxyOf.nodes[existIndex],
  159. type
  160. ).reverse()
  161. existIndex = this.index(exist)
  162. for (let node of nodes) this.proxyOf.nodes.splice(existIndex, 0, node)
  163. let index
  164. for (let id in this.indexes) {
  165. index = this.indexes[id]
  166. if (existIndex <= index) {
  167. this.indexes[id] = index + nodes.length
  168. }
  169. }
  170. this.markDirty()
  171. return this
  172. }
  173. normalize(nodes, sample) {
  174. if (typeof nodes === 'string') {
  175. nodes = cleanSource(parse(nodes).nodes)
  176. } else if (typeof nodes === 'undefined') {
  177. nodes = []
  178. } else if (Array.isArray(nodes)) {
  179. nodes = nodes.slice(0)
  180. for (let i of nodes) {
  181. if (i.parent) i.parent.removeChild(i, 'ignore')
  182. }
  183. } else if (nodes.type === 'root' && this.type !== 'document') {
  184. nodes = nodes.nodes.slice(0)
  185. for (let i of nodes) {
  186. if (i.parent) i.parent.removeChild(i, 'ignore')
  187. }
  188. } else if (nodes.type) {
  189. nodes = [nodes]
  190. } else if (nodes.prop) {
  191. if (typeof nodes.value === 'undefined') {
  192. throw new Error('Value field is missed in node creation')
  193. } else if (typeof nodes.value !== 'string') {
  194. nodes.value = String(nodes.value)
  195. }
  196. nodes = [new Declaration(nodes)]
  197. } else if (nodes.selector || nodes.selectors) {
  198. nodes = [new Rule(nodes)]
  199. } else if (nodes.name) {
  200. nodes = [new AtRule(nodes)]
  201. } else if (nodes.text) {
  202. nodes = [new Comment(nodes)]
  203. } else {
  204. throw new Error('Unknown node type in node creation')
  205. }
  206. let processed = nodes.map(i => {
  207. /* c8 ignore next */
  208. if (!i[my]) Container.rebuild(i)
  209. i = i.proxyOf
  210. if (i.parent) i.parent.removeChild(i)
  211. if (i[isClean]) markTreeDirty(i)
  212. if (!i.raws) i.raws = {}
  213. if (typeof i.raws.before === 'undefined') {
  214. if (sample && typeof sample.raws.before !== 'undefined') {
  215. i.raws.before = sample.raws.before.replace(/\S/g, '')
  216. }
  217. }
  218. i.parent = this.proxyOf
  219. return i
  220. })
  221. return processed
  222. }
  223. prepend(...children) {
  224. children = children.reverse()
  225. for (let child of children) {
  226. let nodes = this.normalize(child, this.first, 'prepend').reverse()
  227. for (let node of nodes) this.proxyOf.nodes.unshift(node)
  228. for (let id in this.indexes) {
  229. this.indexes[id] = this.indexes[id] + nodes.length
  230. }
  231. }
  232. this.markDirty()
  233. return this
  234. }
  235. push(child) {
  236. child.parent = this
  237. this.proxyOf.nodes.push(child)
  238. return this
  239. }
  240. removeAll() {
  241. for (let node of this.proxyOf.nodes) node.parent = undefined
  242. this.proxyOf.nodes = []
  243. this.markDirty()
  244. return this
  245. }
  246. removeChild(child) {
  247. child = this.index(child)
  248. this.proxyOf.nodes[child].parent = undefined
  249. this.proxyOf.nodes.splice(child, 1)
  250. let index
  251. for (let id in this.indexes) {
  252. index = this.indexes[id]
  253. if (index >= child) {
  254. this.indexes[id] = index - 1
  255. }
  256. }
  257. this.markDirty()
  258. return this
  259. }
  260. replaceValues(pattern, opts, callback) {
  261. if (!callback) {
  262. callback = opts
  263. opts = {}
  264. }
  265. this.walkDecls(decl => {
  266. if (opts.props && !opts.props.includes(decl.prop)) return
  267. if (opts.fast && !decl.value.includes(opts.fast)) return
  268. decl.value = decl.value.replace(pattern, callback)
  269. })
  270. this.markDirty()
  271. return this
  272. }
  273. some(condition) {
  274. return this.nodes.some(condition)
  275. }
  276. walk(callback) {
  277. if (!this.proxyOf.nodes) return undefined
  278. // An explicit stack instead of recursive `each()` calls to survive
  279. // deeply nested trees. Each frame keeps a live `indexes` slot, so
  280. // insertion and removal during the walk behave like `each()`: the
  281. // slot stays at the current child until its subtree is finished.
  282. let stack = [{ iterator: this.getIterator(), node: this.proxyOf }]
  283. while (stack.length > 0) {
  284. let { iterator, node } = stack[stack.length - 1]
  285. let index = node.indexes[iterator]
  286. if (index >= node.proxyOf.nodes.length) {
  287. delete node.indexes[iterator]
  288. stack.pop()
  289. let parent = stack[stack.length - 1]
  290. // Finish the parent’s step for the child subtree we just left
  291. if (parent) parent.node.indexes[parent.iterator] += 1
  292. continue
  293. }
  294. let child = node.proxyOf.nodes[index]
  295. let result
  296. try {
  297. result = callback(child, index)
  298. } catch (e) {
  299. throw child.addToError(e)
  300. }
  301. if (result === false) {
  302. for (let opened of stack) {
  303. delete opened.node.indexes[opened.iterator]
  304. }
  305. return false
  306. }
  307. if (child.walk && child.proxyOf.nodes) {
  308. stack.push({ iterator: child.getIterator(), node: child })
  309. } else {
  310. node.indexes[iterator] += 1
  311. }
  312. }
  313. return undefined
  314. }
  315. walkAtRules(name, callback) {
  316. if (!callback) {
  317. callback = name
  318. return this.walk((child, i) => {
  319. if (child.type === 'atrule') {
  320. return callback(child, i)
  321. }
  322. })
  323. }
  324. if (name instanceof RegExp) {
  325. return this.walk((child, i) => {
  326. if (child.type === 'atrule' && name.test(child.name)) {
  327. return callback(child, i)
  328. }
  329. })
  330. }
  331. return this.walk((child, i) => {
  332. if (child.type === 'atrule' && child.name === name) {
  333. return callback(child, i)
  334. }
  335. })
  336. }
  337. walkComments(callback) {
  338. return this.walk((child, i) => {
  339. if (child.type === 'comment') {
  340. return callback(child, i)
  341. }
  342. })
  343. }
  344. walkDecls(prop, callback) {
  345. if (!callback) {
  346. callback = prop
  347. return this.walk((child, i) => {
  348. if (child.type === 'decl') {
  349. return callback(child, i)
  350. }
  351. })
  352. }
  353. if (prop instanceof RegExp) {
  354. return this.walk((child, i) => {
  355. if (child.type === 'decl' && prop.test(child.prop)) {
  356. return callback(child, i)
  357. }
  358. })
  359. }
  360. return this.walk((child, i) => {
  361. if (child.type === 'decl' && child.prop === prop) {
  362. return callback(child, i)
  363. }
  364. })
  365. }
  366. walkRules(selector, callback) {
  367. if (!callback) {
  368. callback = selector
  369. return this.walk((child, i) => {
  370. if (child.type === 'rule') {
  371. return callback(child, i)
  372. }
  373. })
  374. }
  375. if (selector instanceof RegExp) {
  376. return this.walk((child, i) => {
  377. if (child.type === 'rule' && selector.test(child.selector)) {
  378. return callback(child, i)
  379. }
  380. })
  381. }
  382. return this.walk((child, i) => {
  383. if (child.type === 'rule' && child.selector === selector) {
  384. return callback(child, i)
  385. }
  386. })
  387. }
  388. }
  389. Container.registerParse = dependant => {
  390. parse = dependant
  391. }
  392. Container.registerRule = dependant => {
  393. Rule = dependant
  394. }
  395. Container.registerAtRule = dependant => {
  396. AtRule = dependant
  397. }
  398. Container.registerRoot = dependant => {
  399. Root = dependant
  400. }
  401. module.exports = Container
  402. Container.default = Container
  403. /* c8 ignore start */
  404. Container.rebuild = node => {
  405. let stack = [node]
  406. while (stack.length > 0) {
  407. let next = stack.pop()
  408. if (next.type === 'atrule') {
  409. Object.setPrototypeOf(next, AtRule.prototype)
  410. } else if (next.type === 'rule') {
  411. Object.setPrototypeOf(next, Rule.prototype)
  412. } else if (next.type === 'decl') {
  413. Object.setPrototypeOf(next, Declaration.prototype)
  414. } else if (next.type === 'comment') {
  415. Object.setPrototypeOf(next, Comment.prototype)
  416. } else if (next.type === 'root') {
  417. Object.setPrototypeOf(next, Root.prototype)
  418. }
  419. next[my] = true
  420. if (next.nodes) {
  421. for (let child of next.nodes) stack.push(child)
  422. }
  423. }
  424. }
  425. /* c8 ignore stop */