Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkgs/csslib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions pkgs/csslib/lib/src/tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
71 changes: 71 additions & 0 deletions pkgs/csslib/test/declaration_group_clone_test.dart
Original file line number Diff line number Diff line change
@@ -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 = <Message>[];
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 = <Message>[];
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);
}''');
});
}
Loading