diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 48a162d374..e8ae2bf557 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -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 diff --git a/pkgs/csslib/lib/src/tree.dart b/pkgs/csslib/lib/src/tree.dart index b11e95eed3..3c143f7c68 100644 --- a/pkgs/csslib/lib/src/tree.dart +++ b/pkgs/csslib/lib/src/tree.dart @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); } @@ -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); } @@ -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); } diff --git a/pkgs/csslib/test/dart_style_clone_test.dart b/pkgs/csslib/test/dart_style_clone_test.dart new file mode 100644 index 0000000000..f6c50816ef --- /dev/null +++ b/pkgs/csslib/test/dart_style_clone_test.dart @@ -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 = ['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 = ['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 = [ + 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); + } + }); +} diff --git a/pkgs/csslib/test/mixin_test.dart b/pkgs/csslib/test/mixin_test.dart index 684f914efc..5363ac4b71 100644 --- a/pkgs/csslib/test/mixin_test.dart +++ b/pkgs/csslib/test/mixin_test.dart @@ -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 { @@ -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);