postcss 8.3.9 → 8.4.31

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