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. get first() {
  54. if (!this.proxyOf.nodes) return undefined
  55. return this.proxyOf.nodes[0]
  56. }
  57. getIterator() {
  58. if (!this.lastEach) this.lastEach = 0
  59. if (!this.indexes) this.indexes = {}
  60. this.lastEach += 1
  61. let iterator = this.lastEach
  62. this.indexes[iterator] = 0
  63. return iterator
  64. }
  65. getProxyProcessor() {
  66. return {
  67. get(node, prop) {
  68. if (prop === 'proxyOf') {
  69. return node
  70. } else if (!node[prop]) {
  71. return node[prop]
  72. } else if (
  73. prop === 'each' ||
  74. (typeof prop === 'string' && prop.startsWith('walk'))
  75. ) {
  76. return (...args) => {
  77. return node[prop](
  78. ...args.map(i => {
  79. if (typeof i === 'function') {
  80. return (child, index) => i(child.toProxy(), index)
  81. } else {
  82. return i
  83. }
  84. })
  85. )
  86. }
  87. } else if (prop === 'every' || prop === 'some') {
  88. return cb => {
  89. return node[prop]((child, ...other) =>
  90. cb(child.toProxy(), ...other)
  91. )
  92. }
  93. } else if (prop === 'root') {
  94. return () => node.root().toProxy()
  95. } else if (prop === 'nodes') {
  96. return node.nodes.map(i => i.toProxy())
  97. } else if (prop === 'first' || prop === 'last') {
  98. return node[prop].toProxy()
  99. } else {
  100. return node[prop]
  101. }
  102. },
  103. set(node, prop, value) {
  104. if (node[prop] === value) return true
  105. node[prop] = value
  106. if (prop === 'name' || prop === 'params' || prop === 'selector') {
  107. node.markDirty()
  108. }
  109. return true
  110. }
  111. }
  112. }
  113. index(child) {
  114. if (typeof child === 'number') return child
  115. if (child.proxyOf) child = child.proxyOf
  116. return this.proxyOf.nodes.indexOf(child)
  117. }
  118. insertAfter(exist, add) {
  119. let existIndex = this.index(exist)
  120. let nodes = this.normalize(add, this.proxyOf.nodes[existIndex]).reverse()
  121. existIndex = this.index(exist)
  122. for (let node of nodes) this.proxyOf.nodes.splice(existIndex + 1, 0, node)
  123. let index
  124. for (let id in this.indexes) {
  125. index = this.indexes[id]
  126. if (existIndex < index) {
  127. this.indexes[id] = index + nodes.length
  128. }
  129. }
  130. this.markDirty()
  131. return this
  132. }
  133. insertBefore(exist, add) {
  134. let existIndex = this.index(exist)
  135. let type = existIndex === 0 ? 'prepend' : false
  136. let nodes = this.normalize(add, this.proxyOf.nodes[existIndex], type).reverse()
  137. existIndex = this.index(exist)
  138. for (let node of nodes) this.proxyOf.nodes.splice(existIndex, 0, node)
  139. let index
  140. for (let id in this.indexes) {
  141. index = this.indexes[id]
  142. if (existIndex <= index) {
  143. this.indexes[id] = index + nodes.length
  144. }
  145. }
  146. this.markDirty()
  147. return this
  148. }
  149. get last() {
  150. if (!this.proxyOf.nodes) return undefined
  151. return this.proxyOf.nodes[this.proxyOf.nodes.length - 1]
  152. }
  153. normalize(nodes, sample) {
  154. if (typeof nodes === 'string') {
  155. nodes = cleanSource(parse(nodes).nodes)
  156. } else if (Array.isArray(nodes)) {
  157. nodes = nodes.slice(0)
  158. for (let i of nodes) {
  159. if (i.parent) i.parent.removeChild(i, 'ignore')
  160. }
  161. } else if (nodes.type === 'root' && this.type !== 'document') {
  162. nodes = nodes.nodes.slice(0)
  163. for (let i of nodes) {
  164. if (i.parent) i.parent.removeChild(i, 'ignore')
  165. }
  166. } else if (nodes.type) {
  167. nodes = [nodes]
  168. } else if (nodes.prop) {
  169. if (typeof nodes.value === 'undefined') {
  170. throw new Error('Value field is missed in node creation')
  171. } else if (typeof nodes.value !== 'string') {
  172. nodes.value = String(nodes.value)
  173. }
  174. nodes = [new Declaration(nodes)]
  175. } else if (nodes.selector) {
  176. nodes = [new Rule(nodes)]
  177. } else if (nodes.name) {
  178. nodes = [new AtRule(nodes)]
  179. } else if (nodes.text) {
  180. nodes = [new Comment(nodes)]
  181. } else {
  182. throw new Error('Unknown node type in node creation')
  183. }
  184. let processed = nodes.map(i => {
  185. /* c8 ignore next */
  186. if (!i[my]) Container.rebuild(i)
  187. i = i.proxyOf
  188. if (i.parent) i.parent.removeChild(i)
  189. if (i[isClean]) markDirtyUp(i)
  190. if (typeof i.raws.before === 'undefined') {
  191. if (sample && typeof sample.raws.before !== 'undefined') {
  192. i.raws.before = sample.raws.before.replace(/\S/g, '')
  193. }
  194. }
  195. i.parent = this.proxyOf
  196. return i
  197. })
  198. return processed
  199. }
  200. prepend(...children) {
  201. children = children.reverse()
  202. for (let child of children) {
  203. let nodes = this.normalize(child, this.first, 'prepend').reverse()
  204. for (let node of nodes) this.proxyOf.nodes.unshift(node)
  205. for (let id in this.indexes) {
  206. this.indexes[id] = this.indexes[id] + nodes.length
  207. }
  208. }
  209. this.markDirty()
  210. return this
  211. }
  212. push(child) {
  213. child.parent = this
  214. this.proxyOf.nodes.push(child)
  215. return this
  216. }
  217. removeAll() {
  218. for (let node of this.proxyOf.nodes) node.parent = undefined
  219. this.proxyOf.nodes = []
  220. this.markDirty()
  221. return this
  222. }
  223. removeChild(child) {
  224. child = this.index(child)
  225. this.proxyOf.nodes[child].parent = undefined
  226. this.proxyOf.nodes.splice(child, 1)
  227. let index
  228. for (let id in this.indexes) {
  229. index = this.indexes[id]
  230. if (index >= child) {
  231. this.indexes[id] = index - 1
  232. }
  233. }
  234. this.markDirty()
  235. return this
  236. }
  237. replaceValues(pattern, opts, callback) {
  238. if (!callback) {
  239. callback = opts
  240. opts = {}
  241. }
  242. this.walkDecls(decl => {
  243. if (opts.props && !opts.props.includes(decl.prop)) return
  244. if (opts.fast && !decl.value.includes(opts.fast)) return
  245. decl.value = decl.value.replace(pattern, callback)
  246. })
  247. this.markDirty()
  248. return this
  249. }
  250. some(condition) {
  251. return this.nodes.some(condition)
  252. }
  253. walk(callback) {
  254. return this.each((child, i) => {
  255. let result
  256. try {
  257. result = callback(child, i)
  258. } catch (e) {
  259. throw child.addToError(e)
  260. }
  261. if (result !== false && child.walk) {
  262. result = child.walk(callback)
  263. }
  264. return result
  265. })
  266. }
  267. walkAtRules(name, callback) {
  268. if (!callback) {
  269. callback = name
  270. return this.walk((child, i) => {
  271. if (child.type === 'atrule') {
  272. return callback(child, i)
  273. }
  274. })
  275. }
  276. if (name instanceof RegExp) {
  277. return this.walk((child, i) => {
  278. if (child.type === 'atrule' && name.test(child.name)) {
  279. return callback(child, i)
  280. }
  281. })
  282. }
  283. return this.walk((child, i) => {
  284. if (child.type === 'atrule' && child.name === name) {
  285. return callback(child, i)
  286. }
  287. })
  288. }
  289. walkComments(callback) {
  290. return this.walk((child, i) => {
  291. if (child.type === 'comment') {
  292. return callback(child, i)
  293. }
  294. })
  295. }
  296. walkDecls(prop, callback) {
  297. if (!callback) {
  298. callback = prop
  299. return this.walk((child, i) => {
  300. if (child.type === 'decl') {
  301. return callback(child, i)
  302. }
  303. })
  304. }
  305. if (prop instanceof RegExp) {
  306. return this.walk((child, i) => {
  307. if (child.type === 'decl' && prop.test(child.prop)) {
  308. return callback(child, i)
  309. }
  310. })
  311. }
  312. return this.walk((child, i) => {
  313. if (child.type === 'decl' && child.prop === prop) {
  314. return callback(child, i)
  315. }
  316. })
  317. }
  318. walkRules(selector, callback) {
  319. if (!callback) {
  320. callback = selector
  321. return this.walk((child, i) => {
  322. if (child.type === 'rule') {
  323. return callback(child, i)
  324. }
  325. })
  326. }
  327. if (selector instanceof RegExp) {
  328. return this.walk((child, i) => {
  329. if (child.type === 'rule' && selector.test(child.selector)) {
  330. return callback(child, i)
  331. }
  332. })
  333. }
  334. return this.walk((child, i) => {
  335. if (child.type === 'rule' && child.selector === selector) {
  336. return callback(child, i)
  337. }
  338. })
  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 */