postcss-nested 7.0.1 → 8.0.0
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.
- package/LICENSE +1 -1
- package/README.md +5 -7
- package/index.d.ts +3 -3
- package/index.js +82 -87
- package/package.json +11 -10
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright 2014 Andrey Sitnik <andrey@sitnik.
|
|
3
|
+
Copyright 2014 Andrey Sitnik <andrey@sitnik.es>
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
6
|
this software and associated documentation files (the "Software"), to deal in
|
package/README.md
CHANGED
|
@@ -69,10 +69,11 @@ Alternatives:
|
|
|
69
69
|
- See also [`postcss-nesting`], which implements [CSSWG draft].
|
|
70
70
|
- [`postcss-nested-props`] for nested properties like `font-size`.
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
<img src="https://cdn.evilmartians.com/badges/logo-no-label.svg" alt="" width="22" height="16" /> PostCSS Nested is built by <b><a href="https://evilmartians.com/">Evil Martians</a></b>, an American design and engineering consultancy for <b>developer tools, AI, and cybersecurity startups</b>.
|
|
75
|
+
|
|
76
|
+
---
|
|
76
77
|
|
|
77
78
|
[`postcss-current-selector`]: https://github.com/komlev/postcss-current-selector
|
|
78
79
|
[`postcss-nested-ancestors`]: https://github.com/toomuchdesign/postcss-nested-ancestors
|
|
@@ -80,6 +81,3 @@ Alternatives:
|
|
|
80
81
|
[`postcss-nesting`]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-nesting
|
|
81
82
|
[CSSWG draft]: https://drafts.csswg.org/css-nesting-1/
|
|
82
83
|
[PostCSS]: https://github.com/postcss/postcss
|
|
83
|
-
|
|
84
|
-
## Docs
|
|
85
|
-
Read full docs **[here](https://github.com/postcss/postcss-nested#readme)**.
|
package/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Original definitions (@types/postcss-nested)
|
|
2
2
|
// by Maxim Vorontsov <https://github.com/VorontsovMaxim>
|
|
3
3
|
|
|
4
|
-
import { PluginCreator } from 'postcss'
|
|
4
|
+
import type { PluginCreator } from 'postcss'
|
|
5
5
|
|
|
6
6
|
declare namespace nested {
|
|
7
7
|
interface Options {
|
|
@@ -36,6 +36,6 @@ declare namespace nested {
|
|
|
36
36
|
type Nested = PluginCreator<Options>
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
declare
|
|
39
|
+
declare let nested: nested.Nested
|
|
40
40
|
|
|
41
|
-
export
|
|
41
|
+
export default nested
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { AtRule, Rule } from 'postcss'
|
|
2
|
+
import parser from 'postcss-selector-parser'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Run a selector string through postcss-selector-parser
|
|
@@ -75,35 +75,24 @@ function mergeSelectors(parent, child) {
|
|
|
75
75
|
/**
|
|
76
76
|
* Move a child and its preceding comment(s) to after "after"
|
|
77
77
|
*/
|
|
78
|
-
function breakOut(child,
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (!node.nodes) continue
|
|
84
|
-
|
|
85
|
-
let prevNode = node.prev()
|
|
86
|
-
if (prevNode?.type !== 'comment') continue
|
|
87
|
-
|
|
88
|
-
let parentRule = parent.toString()
|
|
78
|
+
function breakOut(child, currentNode) {
|
|
79
|
+
if (child.prev()?.type !== 'comment') {
|
|
80
|
+
currentNode.after(child)
|
|
81
|
+
return child
|
|
82
|
+
}
|
|
89
83
|
|
|
90
|
-
|
|
91
|
-
/* comment about the rule below /*
|
|
92
|
-
.rule {}
|
|
93
|
-
*/
|
|
94
|
-
let regexp = /[*]\/ *\n.*{/
|
|
84
|
+
let prevNode = child.prev()
|
|
95
85
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
86
|
+
/* Checking that the comment "describes" the rule following. Like this:
|
|
87
|
+
/* comment about the rule below *\
|
|
88
|
+
.rule {}
|
|
89
|
+
*/
|
|
90
|
+
let regexp = /[*]\/ *\n.*{/
|
|
99
91
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// It is necessary if the above child has never been moved
|
|
105
|
-
if (changeParent) {
|
|
106
|
-
parent.after(child)
|
|
92
|
+
if (child.parent.toString().match(regexp)) {
|
|
93
|
+
currentNode.after(child).after(prevNode)
|
|
94
|
+
} else {
|
|
95
|
+
currentNode.after(child)
|
|
107
96
|
}
|
|
108
97
|
|
|
109
98
|
return child
|
|
@@ -120,7 +109,7 @@ function createFnAtruleChilds(bubble) {
|
|
|
120
109
|
} else if (child.type === 'atrule' && child.nodes) {
|
|
121
110
|
if (bubble[child.name]) {
|
|
122
111
|
atruleChilds(rule, child, mergeSels)
|
|
123
|
-
} else if (atrule[
|
|
112
|
+
} else if (atrule[ROOT_RULE_MERGE_SEL] !== false) {
|
|
124
113
|
children.push(child)
|
|
125
114
|
}
|
|
126
115
|
} else {
|
|
@@ -146,15 +135,15 @@ function pickDeclarations(selector, declarations, after) {
|
|
|
146
135
|
after.after(parent)
|
|
147
136
|
return parent
|
|
148
137
|
}
|
|
149
|
-
function pickAndClearDeclarations(
|
|
138
|
+
function pickAndClearDeclarations(
|
|
139
|
+
ruleSelector,
|
|
140
|
+
declarations,
|
|
141
|
+
after,
|
|
142
|
+
clear = true
|
|
143
|
+
) {
|
|
150
144
|
if (!declarations.length) return [after, declarations]
|
|
151
|
-
|
|
152
145
|
after = pickDeclarations(ruleSelector, declarations, after)
|
|
153
|
-
|
|
154
|
-
if (clear) {
|
|
155
|
-
declarations = []
|
|
156
|
-
}
|
|
157
|
-
|
|
146
|
+
if (clear) declarations = []
|
|
158
147
|
return [after, declarations]
|
|
159
148
|
}
|
|
160
149
|
|
|
@@ -213,7 +202,7 @@ function getAncestorRules(leaf) {
|
|
|
213
202
|
}
|
|
214
203
|
|
|
215
204
|
function unwrapRootRule(rule) {
|
|
216
|
-
let escapes = rule[
|
|
205
|
+
let escapes = rule[ROOT_RULE_ESCAPES]
|
|
217
206
|
|
|
218
207
|
if (!escapes) {
|
|
219
208
|
rule.after(rule.nodes)
|
|
@@ -235,7 +224,7 @@ function unwrapRootRule(rule) {
|
|
|
235
224
|
} else {
|
|
236
225
|
let oldClone = clone
|
|
237
226
|
clone = parent.clone({ nodes: [] })
|
|
238
|
-
oldClone
|
|
227
|
+
if (oldClone) clone.append(oldClone)
|
|
239
228
|
breakoutLeaf = breakoutLeaf || clone
|
|
240
229
|
}
|
|
241
230
|
})
|
|
@@ -255,26 +244,29 @@ function unwrapRootRule(rule) {
|
|
|
255
244
|
lineage.slice(0, topEscapedIdx + 1).forEach((parent, i, arr) => {
|
|
256
245
|
let oldRoot = restRoot
|
|
257
246
|
restRoot = parent.clone({ nodes: [] })
|
|
258
|
-
oldRoot
|
|
247
|
+
if (oldRoot) restRoot.append(oldRoot)
|
|
259
248
|
|
|
260
249
|
let nextSibs = []
|
|
261
|
-
let
|
|
262
|
-
let next =
|
|
250
|
+
let child = arr[i - 1] || rule
|
|
251
|
+
let next = child.next()
|
|
263
252
|
while (next) {
|
|
264
253
|
nextSibs.push(next)
|
|
265
254
|
next = next.next()
|
|
266
255
|
}
|
|
267
256
|
restRoot.append(nextSibs)
|
|
268
257
|
})
|
|
269
|
-
|
|
258
|
+
if (restRoot) {
|
|
259
|
+
let insert = breakoutRoot || nodes[nodes.length - 1]
|
|
260
|
+
insert.after(restRoot)
|
|
261
|
+
}
|
|
270
262
|
}
|
|
271
263
|
}
|
|
272
264
|
|
|
273
265
|
rule.remove()
|
|
274
266
|
}
|
|
275
267
|
|
|
276
|
-
const
|
|
277
|
-
const
|
|
268
|
+
const ROOT_RULE_MERGE_SEL = Symbol('rootRuleMergeSel')
|
|
269
|
+
const ROOT_RULE_ESCAPES = Symbol('rootRuleEscapes')
|
|
278
270
|
|
|
279
271
|
function normalizeRootRule(rule) {
|
|
280
272
|
let { params } = rule
|
|
@@ -289,13 +281,13 @@ function normalizeRootRule(rule) {
|
|
|
289
281
|
rule.removeAll()
|
|
290
282
|
rule.append(selectorBlock)
|
|
291
283
|
}
|
|
292
|
-
rule[
|
|
293
|
-
rule[
|
|
284
|
+
rule[ROOT_RULE_ESCAPES] = escapes
|
|
285
|
+
rule[ROOT_RULE_MERGE_SEL] = escapes ? !escapes('all') : type === 'noop'
|
|
294
286
|
}
|
|
295
287
|
|
|
296
|
-
const
|
|
288
|
+
const HAS_ROOT_RULE = Symbol('hasRootRule')
|
|
297
289
|
|
|
298
|
-
|
|
290
|
+
let nested = (opts = {}) => {
|
|
299
291
|
let bubble = atruleNames(
|
|
300
292
|
['media', 'supports', 'layer', 'container', 'starting-style'],
|
|
301
293
|
opts.bubble
|
|
@@ -318,17 +310,17 @@ module.exports = (opts = {}) => {
|
|
|
318
310
|
Once(root) {
|
|
319
311
|
root.walkAtRules(rootRuleName, node => {
|
|
320
312
|
normalizeRootRule(node)
|
|
321
|
-
root[
|
|
313
|
+
root[HAS_ROOT_RULE] = true
|
|
322
314
|
})
|
|
323
315
|
},
|
|
324
316
|
|
|
325
317
|
postcssPlugin: 'postcss-nested',
|
|
326
318
|
|
|
327
319
|
RootExit(root) {
|
|
328
|
-
if (!root[
|
|
320
|
+
if (!root[HAS_ROOT_RULE]) return
|
|
329
321
|
|
|
330
322
|
root.walkAtRules(rootRuleName, unwrapRootRule)
|
|
331
|
-
root[
|
|
323
|
+
root[HAS_ROOT_RULE] = false
|
|
332
324
|
},
|
|
333
325
|
|
|
334
326
|
Rule(rule) {
|
|
@@ -338,44 +330,45 @@ module.exports = (opts = {}) => {
|
|
|
338
330
|
let declarations = []
|
|
339
331
|
|
|
340
332
|
rule.each(child => {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
after = breakOut(child, after)
|
|
354
|
-
} else if (unwrap[child.name]) {
|
|
355
|
-
copyDeclarations = true
|
|
356
|
-
unwrapped = true
|
|
357
|
-
atruleChilds(rule, child, false)
|
|
358
|
-
after = breakOut(child, after)
|
|
359
|
-
} else if (copyDeclarations) {
|
|
360
|
-
declarations.push(child)
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
break
|
|
364
|
-
case 'decl':
|
|
365
|
-
if (copyDeclarations) {
|
|
366
|
-
declarations.push(child)
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
break
|
|
370
|
-
case 'rule':
|
|
371
|
-
[after, declarations] = pickAndClearDeclarations(rule.selector, declarations, after)
|
|
372
|
-
|
|
333
|
+
if (child.type === 'atrule') {
|
|
334
|
+
;[after, declarations] = pickAndClearDeclarations(
|
|
335
|
+
rule.selector,
|
|
336
|
+
declarations,
|
|
337
|
+
after
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
if (child.name === rootRuleName) {
|
|
341
|
+
unwrapped = true
|
|
342
|
+
atruleChilds(rule, child, true, child[ROOT_RULE_MERGE_SEL])
|
|
343
|
+
after = breakOut(child, after)
|
|
344
|
+
} else if (bubble[child.name]) {
|
|
373
345
|
copyDeclarations = true
|
|
374
346
|
unwrapped = true
|
|
375
|
-
|
|
347
|
+
atruleChilds(rule, child, true)
|
|
376
348
|
after = breakOut(child, after)
|
|
377
|
-
|
|
378
|
-
|
|
349
|
+
} else if (unwrap[child.name]) {
|
|
350
|
+
copyDeclarations = true
|
|
351
|
+
unwrapped = true
|
|
352
|
+
atruleChilds(rule, child, false)
|
|
353
|
+
after = breakOut(child, after)
|
|
354
|
+
} else if (copyDeclarations) {
|
|
355
|
+
declarations.push(child)
|
|
356
|
+
}
|
|
357
|
+
} else if (child.type === 'decl') {
|
|
358
|
+
if (copyDeclarations) {
|
|
359
|
+
declarations.push(child)
|
|
360
|
+
}
|
|
361
|
+
} else if (child.type === 'rule') {
|
|
362
|
+
;[after, declarations] = pickAndClearDeclarations(
|
|
363
|
+
rule.selector,
|
|
364
|
+
declarations,
|
|
365
|
+
after
|
|
366
|
+
)
|
|
367
|
+
|
|
368
|
+
copyDeclarations = true
|
|
369
|
+
unwrapped = true
|
|
370
|
+
child.selectors = mergeSelectors(rule, child)
|
|
371
|
+
after = breakOut(child, after)
|
|
379
372
|
}
|
|
380
373
|
})
|
|
381
374
|
|
|
@@ -388,4 +381,6 @@ module.exports = (opts = {}) => {
|
|
|
388
381
|
}
|
|
389
382
|
}
|
|
390
383
|
}
|
|
391
|
-
|
|
384
|
+
nested.postcss = true
|
|
385
|
+
|
|
386
|
+
export default nested
|
package/package.json
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-nested",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"description": "PostCSS plugin to unwrap nested rules like how Sass does it",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"postcss",
|
|
7
6
|
"css",
|
|
7
|
+
"nested",
|
|
8
|
+
"postcss",
|
|
8
9
|
"postcss-plugin",
|
|
9
|
-
"sass"
|
|
10
|
-
"nested"
|
|
10
|
+
"sass"
|
|
11
11
|
],
|
|
12
|
-
"author": "Andrey Sitnik <andrey@sitnik.ru>",
|
|
13
12
|
"license": "MIT",
|
|
13
|
+
"author": "Andrey Sitnik <andrey@sitnik.es>",
|
|
14
14
|
"repository": "postcss/postcss-nested",
|
|
15
|
-
"engines": {
|
|
16
|
-
"node": ">=18.0"
|
|
17
|
-
},
|
|
18
15
|
"funding": [
|
|
19
16
|
{
|
|
20
17
|
"type": "opencollective",
|
|
@@ -25,10 +22,14 @@
|
|
|
25
22
|
"url": "https://github.com/sponsors/ai"
|
|
26
23
|
}
|
|
27
24
|
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"postcss-selector-parser": "^7.1.4"
|
|
28
|
+
},
|
|
28
29
|
"peerDependencies": {
|
|
29
30
|
"postcss": "^8.2.14"
|
|
30
31
|
},
|
|
31
|
-
"
|
|
32
|
-
"
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": "^22.0.0 || ^24.0.0 || >= 26.0.0"
|
|
33
34
|
}
|
|
34
35
|
}
|