container.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. return nodes.map(i => {
  9. if (i.nodes) i.nodes = cleanSource(i.nodes)
  10. delete i.source
  11. return i
  12. })
  13. }
  14. function markTreeDirty(node) {
  15. node[isClean] = false
  16. if (node.proxyOf.nodes) {
  17. for (let i of node.proxyOf.nodes) {
  18. markTreeDirty(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(
  133. add,
  134. this.proxyOf.nodes[existIndex],
  135. type
  136. ).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. normalize(nodes, sample) {
  150. if (typeof nodes === 'string') {
  151. nodes = cleanSource(parse(nodes).nodes)
  152. } else if (typeof nodes === 'undefined') {
  153. nodes = []
  154. } else if (Array.isArray(nodes)) {
  155. nodes = nodes.slice(0)
  156. for (let i of nodes) {
  157. if (i.parent) i.parent.removeChild(i, 'ignore')
  158. }
  159. } else if (nodes.type === 'root' && this.type !== 'document') {
  160. nodes = nodes.nodes.slice(0)
  161. for (let i of nodes) {
  162. if (i.parent) i.parent.removeChild(i, 'ignore')
  163. }
  164. } else if (nodes.type) {
  165. nodes = [nodes]
  166. } else if (nodes.prop) {
  167. if (typeof nodes.value === 'undefined') {
  168. throw new Error('Value field is missed in node creation')
  169. } else if (typeof nodes.value !== 'string') {
  170. nodes.value = String(nodes.value)
  171. }
  172. nodes = [new Declaration(nodes)]
  173. } else if (nodes.selector || nodes.selectors) {
  174. nodes = [new Rule(nodes)]
  175. } else if (nodes.name) {
  176. nodes = [new AtRule(nodes)]
  177. } else if (nodes.text) {
  178. nodes = [new Comment(nodes)]
  179. } else {
  180. throw new Error('Unknown node type in node creation')
  181. }
  182. let processed = nodes.map(i => {
  183. /* c8 ignore next */
  184. if (!i[my]) Container.rebuild(i)
  185. i = i.proxyOf
  186. if (i.parent) i.parent.removeChild(i)
  187. if (i[isClean]) markTreeDirty(i)
  188. if (!i.raws) i.raws = {}
  189. if (typeof i.raws.before === 'undefined') {
  190. if (sample && typeof sample.raws.before !== 'undefined') {
  191. i.raws.before = sample.raws.before.replace(/\S/g, '')
  192. }
  193. }
  194. i.parent = this.proxyOf
  195. return i
  196. })
  197. return processed
  198. }
  199. prepend(...children) {
  200. children = children.reverse()
  201. for (let child of children) {
  202. let nodes = this.normalize(child, this.first, 'prepend').reverse()
  203. for (let node of nodes) this.proxyOf.nodes.unshift(node)
  204. for (let id in this.indexes) {
  205. this.indexes[id] = this.indexes[id] + nodes.length
  206. }
  207. }
  208. this.markDirty()
  209. return this
  210. }
  211. push(child) {
  212. child.parent = this
  213. this.proxyOf.nodes.push(child)
  214. return this
  215. }
  216. removeAll() {
  217. for (let node of this.proxyOf.nodes) node.parent = undefined
  218. this.proxyOf.nodes = []
  219. this.markDirty()
  220. return this
  221. }
  222. removeChild(child) {
  223. child = this.index(child)
  224. this.proxyOf.nodes[child].parent = undefined
  225. this.proxyOf.nodes.splice(child, 1)
  226. let index
  227. for (let id in this.indexes) {
  228. index = this.indexes[id]
  229. if (index >= child) {
  230. this.indexes[id] = index - 1
  231. }
  232. }
  233. this.markDirty()
  234. return this
  235. }
  236. replaceValues(pattern, opts, callback) {
  237. if (!callback) {
  238. callback = opts
  239. opts = {}
  240. }
  241. this.walkDecls(decl => {
  242. if (opts.props && !opts.props.includes(decl.prop)) return
  243. if (opts.fast && !decl.value.includes(opts.fast)) return
  244. decl.value = decl.value.replace(pattern, callback)
  245. })
  246. this.markDirty()
  247. return this
  248. }
  249. some(condition) {
  250. return this.nodes.some(condition)
  251. }
  252. walk(callback) {
  253. return this.each((child, i) => {
  254. let result
  255. try {
  256. result = callback(child, i)
  257. } catch (e) {
  258. throw child.addToError(e)
  259. }
  260. if (result !== false && child.walk) {
  261. result = child.walk(callback)
  262. }
  263. return result
  264. })
  265. }
  266. walkAtRules(name, callback) {
  267. if (!callback) {
  268. callback = name
  269. return this.walk((child, i) => {
  270. if (child.type === 'atrule') {
  271. return callback(child, i)
  272. }
  273. })
  274. }
  275. if (name instanceof RegExp) {
  276. return this.walk((child, i) => {
  277. if (child.type === 'atrule' && name.test(child.name)) {
  278. return callback(child, i)
  279. }
  280. })
  281. }
  282. return this.walk((child, i) => {
  283. if (child.type === 'atrule' && child.name === name) {
  284. return callback(child, i)
  285. }
  286. })
  287. }
  288. walkComments(callback) {
  289. return this.walk((child, i) => {
  290. if (child.type === 'comment') {
  291. return callback(child, i)
  292. }
  293. })
  294. }
  295. walkDecls(prop, callback) {
  296. if (!callback) {
  297. callback = prop
  298. return this.walk((child, i) => {
  299. if (child.type === 'decl') {
  300. return callback(child, i)
  301. }
  302. })
  303. }
  304. if (prop instanceof RegExp) {
  305. return this.walk((child, i) => {
  306. if (child.type === 'decl' && prop.test(child.prop)) {
  307. return callback(child, i)
  308. }
  309. })
  310. }
  311. return this.walk((child, i) => {
  312. if (child.type === 'decl' && child.prop === prop) {
  313. return callback(child, i)
  314. }
  315. })
  316. }
  317. walkRules(selector, callback) {
  318. if (!callback) {
  319. callback = selector
  320. return this.walk((child, i) => {
  321. if (child.type === 'rule') {
  322. return callback(child, i)
  323. }
  324. })
  325. }
  326. if (selector instanceof RegExp) {
  327. return this.walk((child, i) => {
  328. if (child.type === 'rule' && selector.test(child.selector)) {
  329. return callback(child, i)
  330. }
  331. })
  332. }
  333. return this.walk((child, i) => {
  334. if (child.type === 'rule' && child.selector === selector) {
  335. return callback(child, i)
  336. }
  337. })
  338. }
  339. get first() {
  340. if (!this.proxyOf.nodes) return undefined
  341. return this.proxyOf.nodes[0]
  342. }
  343. get last() {
  344. if (!this.proxyOf.nodes) return undefined
  345. return this.proxyOf.nodes[this.proxyOf.nodes.length - 1]
  346. }
  347. }
  348. Container.registerParse = dependant => {
  349. parse = dependant
  350. }
  351. Container.registerRule = dependant => {
  352. Rule = dependant
  353. }
  354. Container.registerAtRule = dependant => {
  355. AtRule = dependant
  356. }
  357. Container.registerRoot = dependant => {
  358. Root = dependant
  359. }
  360. module.exports = Container
  361. Container.default = Container
  362. /* c8 ignore start */
  363. Container.rebuild = node => {
  364. if (node.type === 'atrule') {
  365. Object.setPrototypeOf(node, AtRule.prototype)
  366. } else if (node.type === 'rule') {
  367. Object.setPrototypeOf(node, Rule.prototype)
  368. } else if (node.type === 'decl') {
  369. Object.setPrototypeOf(node, Declaration.prototype)
  370. } else if (node.type === 'comment') {
  371. Object.setPrototypeOf(node, Comment.prototype)
  372. } else if (node.type === 'root') {
  373. Object.setPrototypeOf(node, Root.prototype)
  374. }
  375. node[my] = true
  376. if (node.nodes) {
  377. node.nodes.forEach(child => {
  378. Container.rebuild(child)
  379. })
  380. }
  381. }
  382. /* c8 ignore stop */