diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 4b8874ccd9..48a162d374 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -17,6 +17,8 @@ - Deep-clone selector groups, selector sequences, identifiers, functional pseudo-selector arguments, and legacy pseudo-element flags instead of sharing mutable AST nodes between the original and its clone. +- Preserve nested group terms, IE7-prefixed declaration flags, and variable + usage state when cloning CSS expression/declaration nodes. - Fix `Rgba.fromArgbValue`, `Rgba.argbValue` and `Hsla.color`, preserve alpha for grayscale colors in `Hsla.fromRgba`, and keep the exact 8-bit alpha in `Color.rgba`, `Color.hsla` and `Hsla.fromArgbValue` instead of rounding it to diff --git a/pkgs/csslib/lib/src/tree.dart b/pkgs/csslib/lib/src/tree.dart index 986ea2fcb3..b11e95eed3 100644 --- a/pkgs/csslib/lib/src/tree.dart +++ b/pkgs/csslib/lib/src/tree.dart @@ -1055,7 +1055,7 @@ class Declaration extends TreeNode { @override Declaration clone() => Declaration(_property?.clone(), expression?.clone(), dartStyle, span, - important: important); + important: important, ie7: isIE7); @override dynamic visit(VisitorBase visitor) => visitor.visitDeclaration(this); @@ -1077,7 +1077,8 @@ class VarDefinition extends Declaration { @override VarDefinition clone() => - VarDefinition(_property?.clone(), expression?.clone(), span); + VarDefinition(_property?.clone(), expression?.clone(), span) + ..badUsage = badUsage; @override dynamic visit(VisitorBase visitor) => visitor.visitVarDefinition(this); @@ -1473,7 +1474,14 @@ class GroupTerm extends Expression { } @override - GroupTerm clone() => GroupTerm(span); + GroupTerm clone() { + final copy = GroupTerm(span); + for (final term in _terms) { + copy.add(term.clone()); + } + return copy; + } + @override dynamic visit(VisitorBase visitor) => visitor.visitGroupTerm(this); } diff --git a/pkgs/csslib/test/declaration_group_clone_test.dart b/pkgs/csslib/test/declaration_group_clone_test.dart new file mode 100644 index 0000000000..73a9fafdd7 --- /dev/null +++ b/pkgs/csslib/test/declaration_group_clone_test.dart @@ -0,0 +1,71 @@ +// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:csslib/parser.dart'; +import 'package:csslib/visitor.dart'; +import 'package:source_span/source_span.dart'; +import 'package:test/test.dart'; + +import 'testing.dart'; + +void main() { + test('cloning a group preserves terms and detaches its list', () { + final group = GroupTerm(null) + ..add(NumberTerm(2, '2', null)) + ..add(NumberTerm(5, '5', null)); + final copy = group.clone(); + + expect((CssPrinter()..visitGroupTerm(copy)).toString(), '(2 5)'); + copy.add(NumberTerm(7, '7', null)); + expect((CssPrinter()..visitGroupTerm(copy)).toString(), '(2 5 7)'); + expect((CssPrinter()..visitGroupTerm(group)).toString(), '(2 5)'); + }); + + test('IE7-prefixed declarations retain their flag on clone', () { + final errors = []; + final sheet = parse('a { *zoom: 1 !important; }', errors: errors); + expect(errors, isEmpty); + final rule = sheet.topLevels.single as RuleSet; + final decl = rule.declarationGroup.declarations.single as Declaration; + final copy = decl.clone(); + + expect(decl.property, '*zoom'); + expect(copy.property, '*zoom'); + expect(copy.isIE7, isTrue); + expect(copy.important, isTrue); + + final originalExpr = decl.expression as Expressions; + final clonedExpr = copy.expression as Expressions; + clonedExpr.expressions.clear(); + expect(originalExpr.expressions, hasLength(1)); + }); + + test('cloned variable declarations preserve invalid-usage state', () { + final span = SourceFile.fromString('var-foo').span(0, 7); + final original = VarDefinition(Identifier('foo', span), null, span) + ..badUsage = true; + final copy = original.clone(); + + expect(copy.badUsage, isTrue); + copy.badUsage = false; + expect(original.badUsage, isTrue); + }); + + test('included mixin declarations keep groups and IE7 flags', () { + final errors = []; + final sheet = compileCss(''' +@mixin m { + *zoom: 1; + width: (2 5); +} +div { @include m; }''', errors: errors, opts: simpleOptions); + + expect(errors, isEmpty); + expect(prettyPrint(sheet), ''' +div { + *zoom: 1; + width: (2 5); +}'''); + }); +}