postcss-nested 2.0.3 → 2.1.2

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/CHANGELOG.md CHANGED
@@ -1,6 +1,18 @@
1
1
  # Change Log
2
2
  This project adheres to [Semantic Versioning](http://semver.org/).
3
3
 
4
+ ## 2.1.2
5
+ * Fix replacing multiple `&`.
6
+
7
+ ## 2.1.1
8
+ * Fix `:not(&)` support.
9
+
10
+ ## 2.1
11
+ * Add `preserveEmpty` option (by Federico Zivolo).
12
+
13
+ ## 2.0.4
14
+ * Fix finding `&` in some selectors (by Stepan Mikhaylyuk).
15
+
4
16
  ## 2.0.3
5
17
  * Doesn’t replace `&` inside string (by Paul Kiddle).
6
18
 
package/README.md CHANGED
@@ -78,5 +78,29 @@ By default, plugin will unwrap only `@media`, `@supports` and `@document`
78
78
  at-rules. You can add your custom at-rules to this list by `bubble` option:
79
79
 
80
80
  ```js
81
- postcss([ require('postcss-nested')({ bubble: ['phone'] }) ]
81
+ postcss([ require('postcss-nested')({ bubble: ['phone'] }) ])
82
82
  ```
83
+
84
+ ### `preserveEmpty`
85
+
86
+ By default, plugin will strip out any empty selector generated by intermediate
87
+ nesting levels. You can set `preserveEmpty` to `true` to preserve them.
88
+
89
+ ```css
90
+ .a {
91
+ .b {
92
+ color: black;
93
+ }
94
+ }
95
+ ```
96
+
97
+ Will be compiled to:
98
+
99
+ ```css
100
+ .a { }
101
+ .a .b {
102
+ color: black;
103
+ }
104
+ ```
105
+
106
+ This is especially useful if you want to export the empty classes with `postcss-modules`.
package/index.js CHANGED
@@ -1,29 +1,34 @@
1
1
  var postcss = require('postcss');
2
2
  var parser = require('postcss-selector-parser');
3
3
 
4
- function concatNested(selector, parent) {
4
+ function replace(nodes, parent) {
5
5
  var replaced = false;
6
-
7
- selector.walkNesting(function (ampersand) {
8
- ampersand.replaceWith(parent.clone());
9
- replaced = true;
6
+ nodes.forEach(function (i) {
7
+ if (i.type === 'nesting') {
8
+ i.replaceWith(parent.clone());
9
+ replaced = true;
10
+ } else if (i.nodes) {
11
+ if (replace(i.nodes, parent)) {
12
+ replaced = true;
13
+ }
14
+ }
10
15
  });
11
-
12
- if (!replaced) {
13
- selector.prepend(parser.combinator({ value: ' ' }));
14
- selector.prepend(parent.clone());
15
- }
16
-
17
- return selector;
16
+ return replaced;
18
17
  }
19
18
 
20
- function selectors(parent, node) {
19
+ function selectors(parent, child) {
21
20
  var result = [];
22
21
  parent.selectors.forEach(function (i) {
23
- var parsed = parser().process(i).res.first;
22
+ var parentNode = parser().process(i).res.first;
24
23
 
25
- node.selectors.forEach(function (j) {
26
- result.push(concatNested(parser().process(j).res.first, parsed));
24
+ child.selectors.forEach(function (j) {
25
+ var node = parser().process(j).res.first;
26
+ var replaced = replace(node.nodes, parentNode);
27
+ if (!replaced) {
28
+ node.prepend(parser.combinator({ value: ' ' }));
29
+ node.prepend(parentNode.clone());
30
+ }
31
+ result.push(node.toString());
27
32
  });
28
33
  });
29
34
  return result;
@@ -60,7 +65,7 @@ function atruleChilds(rule, atrule) {
60
65
  }
61
66
  }
62
67
 
63
- function processRule(rule, bubble) {
68
+ function processRule(rule, bubble, preserveEmpty) {
64
69
  var unwrapped = false;
65
70
  var after = rule;
66
71
  rule.each(function (child) {
@@ -80,7 +85,7 @@ function processRule(rule, bubble) {
80
85
  }
81
86
  }
82
87
  });
83
- if ( unwrapped ) {
88
+ if ( unwrapped && preserveEmpty !== true ) {
84
89
  rule.raws.semicolon = true;
85
90
  if ( rule.nodes.length === 0 ) rule.remove();
86
91
  }
@@ -93,11 +98,12 @@ module.exports = postcss.plugin('postcss-nested', function (opts) {
93
98
  return i.replace(/^@/, '');
94
99
  }));
95
100
  }
101
+ var preserveEmpty = opts ? opts.preserveEmpty : false;
96
102
 
97
103
  var process = function (node) {
98
104
  node.each(function (child) {
99
105
  if ( child.type === 'rule' ) {
100
- processRule(child, bubble);
106
+ processRule(child, bubble, preserveEmpty);
101
107
  } else if ( child.type === 'atrule' ) {
102
108
  process(child);
103
109
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-nested",
3
- "version": "2.0.3",
3
+ "version": "2.1.2",
4
4
  "description": "PostCSS plugin to unwrap nested rules like how Sass does it.",
5
5
  "keywords": [
6
6
  "postcss",
@@ -13,11 +13,11 @@
13
13
  "license": "MIT",
14
14
  "repository": "postcss/postcss-nested",
15
15
  "dependencies": {
16
- "postcss": "^6.0.6",
16
+ "postcss": "^6.0.9",
17
17
  "postcss-selector-parser": "^2.2.3"
18
18
  },
19
19
  "devDependencies": {
20
- "eslint": "^4.2.0",
20
+ "eslint": "^4.4.1",
21
21
  "eslint-config-postcss": "^2.0.2",
22
22
  "jest": "^20.0.4"
23
23
  },