UNPKG

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