container.js 9.9 KB

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