From 81d55444e6730c25937a2a96e9521fb401efec01 Mon Sep 17 00:00:00 2001 From: Thierry Bela Nanga Date: Thu, 24 Jul 2025 20:16:36 -0400 Subject: [PATCH] fix bug when nesting is disabled #89 --- dist/index-umd-web.js | 12 ++++++++++-- dist/index.cjs | 12 ++++++++++-- dist/index.d.ts | 12 +++++++++++- dist/lib/ast/minify.js | 3 +-- dist/lib/ast/types.js | 9 +++++++++ src/lib/ast/minify.ts | 5 ++--- test/specs/code/nesting.js | 23 +++++++++++++++++++++++ 7 files changed, 66 insertions(+), 10 deletions(-) diff --git a/dist/index-umd-web.js b/dist/index-umd-web.js index 7bd72388..9ac5e5b2 100644 --- a/dist/index-umd-web.js +++ b/dist/index-umd-web.js @@ -15,8 +15,17 @@ */ exports.ValidationLevel = void 0; (function (ValidationLevel) { + /** + * disable validation + */ ValidationLevel[ValidationLevel["None"] = 0] = "None"; + /** + * validate selectors and at-rules + */ ValidationLevel[ValidationLevel["Default"] = 1] = "Default"; + /** + * validate selectors, at-rules and declarations + */ ValidationLevel[ValidationLevel["All"] = 2] = "All"; // selectors + at-rules + declarations })(exports.ValidationLevel || (exports.ValidationLevel = {})); /** @@ -20923,8 +20932,7 @@ } let rule = selector.map(s => { if (s[0] == '&') { - // @ts-ignore - s[0] = node.optimized.optimized[0]; + s.splice(0, 1, ...node.optimized.optimized); } return s.join(''); }).join(','); diff --git a/dist/index.cjs b/dist/index.cjs index 123f50fe..4dfcb3cb 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -14,8 +14,17 @@ var SyntaxValidationResult; */ exports.ValidationLevel = void 0; (function (ValidationLevel) { + /** + * disable validation + */ ValidationLevel[ValidationLevel["None"] = 0] = "None"; + /** + * validate selectors and at-rules + */ ValidationLevel[ValidationLevel["Default"] = 1] = "Default"; + /** + * validate selectors, at-rules and declarations + */ ValidationLevel[ValidationLevel["All"] = 2] = "All"; // selectors + at-rules + declarations })(exports.ValidationLevel || (exports.ValidationLevel = {})); /** @@ -21032,8 +21041,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent, } let rule = selector.map(s => { if (s[0] == '&') { - // @ts-ignore - s[0] = node.optimized.optimized[0]; + s.splice(0, 1, ...node.optimized.optimized); } return s.join(''); }).join(','); diff --git a/dist/index.d.ts b/dist/index.d.ts index ee5c6d49..72f1d856 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -2,8 +2,17 @@ * validation level enum */ declare enum ValidationLevel { + /** + * disable validation + */ None = 0, + /** + * validate selectors and at-rules + */ Default = 1,// selectors + at-rules + /** + * validate selectors, at-rules and declarations + */ All = 2 } /** @@ -1160,6 +1169,8 @@ interface ValidationOptions { interface MinifyOptions { minify?: boolean; + parseColor?: boolean; + convertColor?: boolean; nestingRules?: boolean; expandNestingRules?: boolean; removeDuplicateDeclarations?: boolean; @@ -1179,7 +1190,6 @@ export declare interface ParserOptions extends MinifyOptions, MinifyFeatureOptio resolveUrls?: boolean; resolveImport?: boolean; cwd?: string; - parseColor?: boolean; removePrefix?: boolean; load?: (url: string, currentUrl: string) => Promise; dirname?: (path: string) => string; diff --git a/dist/lib/ast/minify.js b/dist/lib/ast/minify.js index 8b4112ba..d0e6b27b 100644 --- a/dist/lib/ast/minify.js +++ b/dist/lib/ast/minify.js @@ -347,8 +347,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent, } let rule = selector.map(s => { if (s[0] == '&') { - // @ts-ignore - s[0] = node.optimized.optimized[0]; + s.splice(0, 1, ...node.optimized.optimized); } return s.join(''); }).join(','); diff --git a/dist/lib/ast/types.js b/dist/lib/ast/types.js index 9f98fe16..a01f04dd 100644 --- a/dist/lib/ast/types.js +++ b/dist/lib/ast/types.js @@ -9,8 +9,17 @@ var SyntaxValidationResult; */ var ValidationLevel; (function (ValidationLevel) { + /** + * disable validation + */ ValidationLevel[ValidationLevel["None"] = 0] = "None"; + /** + * validate selectors and at-rules + */ ValidationLevel[ValidationLevel["Default"] = 1] = "Default"; + /** + * validate selectors, at-rules and declarations + */ ValidationLevel[ValidationLevel["All"] = 2] = "All"; // selectors + at-rules + declarations })(ValidationLevel || (ValidationLevel = {})); /** diff --git a/src/lib/ast/minify.ts b/src/lib/ast/minify.ts index 5cd827b1..3bb434c3 100644 --- a/src/lib/ast/minify.ts +++ b/src/lib/ast/minify.ts @@ -487,7 +487,7 @@ function doMinify(ast: AstNode, options: ParserOptions = {}, recursive: boolean return acc; }, []); - +`` if (!wrap) { wrap = selector.some((s: string[]) => s[0] != '&'); @@ -497,8 +497,7 @@ function doMinify(ast: AstNode, options: ParserOptions = {}, recursive: boolean if (s[0] == '&') { - // @ts-ignore - s[0] = node.optimized.optimized[0]; + s.splice(0, 1, ...(node as AstRule)!.optimized!.optimized); } return s.join(''); diff --git a/test/specs/code/nesting.js b/test/specs/code/nesting.js index b973048b..a7fe6381 100644 --- a/test/specs/code/nesting.js +++ b/test/specs/code/nesting.js @@ -586,6 +586,29 @@ article { expandNestingRules: false }).then(result => expect(result.code).equals(`article{color:red;&{color:blue}&{color:green}}`)); }); + + it('no nested rules #25', function () { + + const css = ` + +table.colortable td.c { + text-transform:uppercase; +} +table.colortable td:first-child, table.colortable td:first-child+td { + border:1px solid black; +} +`; + + return transform(css, { + beautify: true, + nestingRules: false + }).then(result => expect(result.code).equals(`table.colortable td.c { + text-transform: uppercase +} +table.colortable td:first-child,table.colortable td:first-child+td { + border: 1px solid #000 +}`)); + }); }); } \ No newline at end of file