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