container.js 10 KB

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