Skip to content
4 changes: 4 additions & 0 deletions pkgs/csslib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
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.
- Preserve Dart style priorities when cloning and detach mutable font-family
lists from cloned font expressions.
- Deep-clone declaration style payloads and preserve Dart style priorities;
detach mutable font-family lists in cloned font expressions.
- 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
77 changes: 63 additions & 14 deletions pkgs/csslib/lib/src/tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1053,9 +1053,17 @@ class Declaration extends TreeNode {
SourceSpan get span => super.span!;

@override
Declaration clone() =>
Declaration(_property?.clone(), expression?.clone(), dartStyle, span,
important: important, ie7: isIE7);
Declaration clone() {
final style = dartStyle?.clone() as DartStyleExpression?;
return Declaration(
_property?.clone(),
expression?.clone(),
style,
span,
important: important,
ie7: isIE7,
);
}

@override
dynamic visit(VisitorBase visitor) => visitor.visitDeclaration(this);
Expand Down Expand Up @@ -1618,13 +1626,18 @@ class FontExpression extends DartStyleExpression {
super(DartStyleExpression.fontStyle, span);

@override
FontExpression clone() => FontExpression(span,
FontExpression clone() {
final result = FontExpression(
span,
size: font.size,
family: font.family,
family: font.family?.toList(),
weight: font.weight,
style: font.style,
variant: font.variant,
lineHeight: font.lineHeight);
lineHeight: font.lineHeight,
);
return result..priority = priority;
}

@override
dynamic visit(VisitorBase visitor) => visitor.visitFontExpression(this);
Expand Down Expand Up @@ -1682,8 +1695,17 @@ class MarginExpression extends BoxExpression {
: super(x._styleType, span, BoxEdge.merge(x.box, y.box));

@override
MarginExpression clone() => MarginExpression(span,
top: box!.top, right: box!.right, bottom: box!.bottom, left: box!.left);
MarginExpression clone() {
final edge = box!;
final result = MarginExpression(
span,
top: edge.top,
right: edge.right,
bottom: edge.bottom,
left: edge.left,
);
return result..priority = priority;
}

@override
dynamic visit(VisitorBase visitor) => visitor.visitMarginExpression(this);
Expand Down Expand Up @@ -1718,8 +1740,17 @@ class BorderExpression extends BoxExpression {
DartStyleExpression.borderStyle, span, BoxEdge.merge(x.box, y.box));

@override
BorderExpression clone() => BorderExpression(span,
top: box!.top, right: box!.right, bottom: box!.bottom, left: box!.left);
BorderExpression clone() {
final edge = box!;
final result = BorderExpression(
span,
top: edge.top,
right: edge.right,
bottom: edge.bottom,
left: edge.left,
);
return result..priority = priority;
}

@override
dynamic visit(VisitorBase visitor) => visitor.visitBorderExpression(this);
Expand All @@ -1741,7 +1772,11 @@ class HeightExpression extends DartStyleExpression {
}

@override
HeightExpression clone() => HeightExpression(span, height);
HeightExpression clone() {
final result = HeightExpression(span, height);
return result..priority = priority;
}

@override
dynamic visit(VisitorBase visitor) => visitor.visitHeightExpression(this);
}
Expand All @@ -1762,7 +1797,11 @@ class WidthExpression extends DartStyleExpression {
}

@override
WidthExpression clone() => WidthExpression(span, width);
WidthExpression clone() {
final result = WidthExpression(span, width);
return result..priority = priority;
}

@override
dynamic visit(VisitorBase visitor) => visitor.visitWidthExpression(this);
}
Expand Down Expand Up @@ -1798,8 +1837,18 @@ class PaddingExpression extends BoxExpression {
BoxEdge.merge(x.box, y.box));

@override
PaddingExpression clone() => PaddingExpression(span,
top: box!.top, right: box!.right, bottom: box!.bottom, left: box!.left);
PaddingExpression clone() {
final edge = box!;
final result = PaddingExpression(
span,
top: edge.top,
right: edge.right,
bottom: edge.bottom,
left: edge.left,
);
return result..priority = priority;
}

@override
dynamic visit(VisitorBase visitor) => visitor.visitPaddingExpression(this);
}
69 changes: 69 additions & 0 deletions pkgs/csslib/test/dart_style_clone_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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/visitor.dart';
import 'package:source_span/source_span.dart';
import 'package:test/test.dart';

void main() {
test('font clones own their family list and retain priority', () {
final span = SourceFile.fromString('font').span(0, 4);
final names = <String>['Frank Ruhl', 'serif'];
final style = FontExpression(span, family: names)..priority = 37;
final clone = style.clone();

names[0] = 'different font';
names.add('fallback');
expect(clone.font.family, ['Frank Ruhl', 'serif']);
expect(clone.priority, 37);

clone.font.family!.add('separate');
expect(style.font.family, ['different font', 'serif', 'fallback']);
});

test('declaration clones own the style payload and keep priority', () {
final span = SourceFile.fromString('font-size').span(0, 9);
final families = <String>['Alef'];
final original = Declaration(
Identifier('font-family', span),
null,
FontExpression(span, family: families)..priority = 31,
span,
important: true,
ie7: true,
);
final copy = original.clone();
final originalStyle = original.dartStyle! as FontExpression;
final copiedStyle = copy.dartStyle! as FontExpression;

expect(copy.property, '*font-family');
expect(copy.important, isTrue);
expect(identical(copiedStyle, originalStyle), isFalse);
expect(copiedStyle.priority, 31);

copiedStyle.priority = 9;
copiedStyle.font.family!.add('serif');
expect(originalStyle.priority, 31);
expect(originalStyle.font.family, ['Alef']);
expect(families, ['Alef']);
});

test('all Dart style subclasses retain priority when cloned', () {
final span = SourceFile.fromString('margin').span(0, 6);
final styles = <DartStyleExpression>[
MarginExpression(span, left: 1),
BorderExpression(span, right: 2),
PaddingExpression(span, top: 3),
HeightExpression(span, 4),
WidthExpression(span, 5),
];
for (final style in styles) {
style.priority = 42;
final copy = style.clone() as DartStyleExpression;
expect(copy.priority, 42, reason: style.runtimeType.toString());
copy.priority = 7;
expect(style.priority, 42);
}
});
}
15 changes: 15 additions & 0 deletions pkgs/csslib/test/mixin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ div {
}''');
}

void declMixinIE7Hack() {
compileAndValidate(r'''
@mixin legacy-color {
*color: red;
}
div {
@include legacy-color;
}
''', r'''
div {
*color: #f00;
}''');
}

void declMixinTwoIncludes() {
compileAndValidate(r'''
@mixin div-border {
Expand Down Expand Up @@ -624,6 +638,7 @@ void main() {

group('Declaration mixin', () {
test('simple', declSimpleMixin);
test('keeps IE7 property hack', declMixinIE7Hack);
test('with two @includes', declMixinTwoIncludes);
test('with includes', declMixinNestedIncludes);
test('with deeper nesting', declMixinDeeperNestedIncludes);
Expand Down
Loading