UNPKG

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