@thejaredwilcurt/csslop 0.0.51 → 0.0.52

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@thejaredwilcurt/csslop",
3
3
  "main": "index.js",
4
4
  "type": "module",
5
- "version": "0.0.51",
5
+ "version": "0.0.52",
6
6
  "description": "Experimental CSS minification",
7
7
  "scripts": {
8
8
  "prestart": "node ./scripts/pre/start.js",
@@ -163,19 +163,56 @@ function nestFlatRules (rules) {
163
163
  const result = [];
164
164
  const indexBySelector = new Map();
165
165
  for (const rule of rules) {
166
- if (rule.type !== 'rule' || rule.selectors?.length !== 1) {
166
+ if (rule.type !== 'rule') {
167
167
  result.push(rule);
168
168
  continue;
169
169
  }
170
- const childSelector = rule.selectors[0].trim();
171
- const nesting = findNestingParent(result, indexBySelector, childSelector);
172
- if (nesting) {
173
- const parentRule = result[nesting.parentIndex];
174
- parentRule.declarations = parentRule.declarations || [];
175
- parentRule.declarations.push({ ...rule, selectors: [nesting.nestedSelector] });
170
+ if (rule.selectors?.length === 1) {
171
+ const childSelector = rule.selectors[0].trim();
172
+ const nesting = findNestingParent(result, indexBySelector, childSelector);
173
+ if (nesting) {
174
+ const parentRule = result[nesting.parentIndex];
175
+ parentRule.declarations = parentRule.declarations || [];
176
+ parentRule.declarations.push({ ...rule, selectors: [nesting.nestedSelector] });
177
+ continue;
178
+ }
179
+ indexBySelector.set(childSelector, result.length);
180
+ result.push(rule);
181
+ continue;
182
+ }
183
+ if (rule.selectors?.length > 1) {
184
+ const nestedSelectors = [];
185
+ let commonParentIndex = null;
186
+ let canNestAll = true;
187
+
188
+ for (const childSelector of rule.selectors) {
189
+ const nesting = findNestingParent(result, indexBySelector, childSelector.trim());
190
+ if (!nesting) {
191
+ canNestAll = false;
192
+ break;
193
+ }
194
+ if (commonParentIndex === null) {
195
+ commonParentIndex = nesting.parentIndex;
196
+ } else if (nesting.parentIndex !== commonParentIndex) {
197
+ canNestAll = false;
198
+ break;
199
+ }
200
+ nestedSelectors.push(nesting.nestedSelector);
201
+ }
202
+
203
+ if (canNestAll && commonParentIndex !== null) {
204
+ const parentRule = result[commonParentIndex];
205
+ parentRule.declarations = parentRule.declarations || [];
206
+ parentRule.declarations.push({ ...rule, selectors: nestedSelectors });
207
+ continue;
208
+ }
209
+
210
+ for (const selector of rule.selectors) {
211
+ indexBySelector.set(selector.trim(), result.length);
212
+ }
213
+ result.push(rule);
176
214
  continue;
177
215
  }
178
- indexBySelector.set(childSelector, result.length);
179
216
  result.push(rule);
180
217
  }
181
218
  for (const rule of result) {