From 6726ec425dc27b0a738edec62ce87809614b6260 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:11:48 +0200 Subject: [PATCH 1/8] fix(csslib): serialize partial font CSS without bogus family names --- pkgs/csslib/CHANGELOG.md | 2 ++ pkgs/csslib/lib/src/property.dart | 17 ++++------ .../test/font_css_serialization_test.dart | 34 +++++++++++++++++++ 3 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 pkgs/csslib/test/font_css_serialization_test.dart diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 1868125515..912c1dac30 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -7,6 +7,8 @@ - `Font.scale` keeps the line height (scaling pixel heights and keeping relative ratios), copies the font-family list, and no longer throws when the font size is unspecified. +- Serialize partial font styles without bogus `ul` family names or trailing + spaces, and join font family lists directly. - 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/property.dart b/pkgs/csslib/lib/src/property.dart index e233da1a1d..ff5c2ce4a3 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1106,16 +1106,16 @@ class Font implements _StyleProperty { /// values are missing the default value is used. @override String get cssExpression { - // TODO(jimhug): include variant, style, other options + // Partial font properties need not specify a font family. + final families = _fontsAsString; + final familySuffix = families.isEmpty ? '' : ' $families'; if (weight != null) { - // TODO(jacobr): is this really correct for lineHeight? if (lineHeight != null) { - return '$weight ${size}px/$lineHeightInPixels $_fontsAsString'; + return '$weight ${size}px/$lineHeightInPixels$familySuffix'; } - return '$weight ${size}px $_fontsAsString'; + return '$weight ${size}px$familySuffix'; } - - return '${size}px $_fontsAsString'; + return '${size}px$familySuffix'; } Font scale(num ratio) { @@ -1175,10 +1175,7 @@ class Font implements _StyleProperty { // TODO(terry): This is fragile should probably just iterate through the list // of fonts construction the font-family string. /// Return fonts as a comma seperated list sans the square brackets. - String get _fontsAsString { - var fonts = family.toString(); - return fonts.length > 2 ? fonts.substring(1, fonts.length - 1) : ''; - } + String get _fontsAsString => family?.join(', ') ?? ''; } /// This class stores the sizes of the box edges in the CSS [box model][]. Each diff --git a/pkgs/csslib/test/font_css_serialization_test.dart b/pkgs/csslib/test/font_css_serialization_test.dart new file mode 100644 index 0000000000..b635f68009 --- /dev/null +++ b/pkgs/csslib/test/font_css_serialization_test.dart @@ -0,0 +1,34 @@ +// 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:test/test.dart'; + +void main() { + test('font CSS omits null and empty family lists', () { + expect(const Font(size: 16).cssExpression, '16px'); + expect(const Font(size: 16, family: []).cssExpression, '16px'); + expect( + const Font(size: 16, weight: FontWeight.bold).cssExpression, + '700 16px', + ); + }); + + test('font CSS serializes multiple named families', () { + expect( + const Font(size: 16, family: ['Alef', 'serif']).cssExpression, + '16px Alef, serif', + ); + }); + + test('font CSS keeps weight and explicit line height', () { + final font = Font( + size: 16, + weight: FontWeight.bold, + lineHeight: const LineHeight(24), + family: const ['Alef'], + ); + expect(font.cssExpression, '700 16px/24 Alef'); + }); +} From d6112e6f7771694ed36321bf1cc8b185aa9402b8 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:31:18 +0200 Subject: [PATCH 2/8] test(csslib): use const font in serialization regression --- pkgs/csslib/test/font_css_serialization_test.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkgs/csslib/test/font_css_serialization_test.dart b/pkgs/csslib/test/font_css_serialization_test.dart index b635f68009..0547b848c0 100644 --- a/pkgs/csslib/test/font_css_serialization_test.dart +++ b/pkgs/csslib/test/font_css_serialization_test.dart @@ -23,11 +23,11 @@ void main() { }); test('font CSS keeps weight and explicit line height', () { - final font = Font( + const font = Font( size: 16, weight: FontWeight.bold, - lineHeight: const LineHeight(24), - family: const ['Alef'], + lineHeight: LineHeight(24), + family: ['Alef'], ); expect(font.cssExpression, '700 16px/24 Alef'); }); From 7ecf76cb8afff935ebf06970874a63f4e142a49a Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:38:17 +0200 Subject: [PATCH 3/8] fix(csslib): serialize explicit font line-height without font-weight --- pkgs/csslib/lib/src/property.dart | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index ff5c2ce4a3..e9ccfa8d2c 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1109,13 +1109,9 @@ class Font implements _StyleProperty { // Partial font properties need not specify a font family. final families = _fontsAsString; final familySuffix = families.isEmpty ? '' : ' $families'; - if (weight != null) { - if (lineHeight != null) { - return '$weight ${size}px/$lineHeightInPixels$familySuffix'; - } - return '$weight ${size}px$familySuffix'; - } - return '${size}px$familySuffix'; + final weightPrefix = weight == null ? '' : '$weight '; + final heightSuffix = lineHeight == null ? '' : '/$lineHeightInPixels'; + return '$weightPrefix${size}px$heightSuffix$familySuffix'; } Font scale(num ratio) { From 07791770ce92dab0512d9f099fdfc3ba6628066a Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:38:31 +0200 Subject: [PATCH 4/8] test(csslib): cover font line-height without weight --- pkgs/csslib/test/font_css_serialization_test.dart | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkgs/csslib/test/font_css_serialization_test.dart b/pkgs/csslib/test/font_css_serialization_test.dart index 0547b848c0..5aec697317 100644 --- a/pkgs/csslib/test/font_css_serialization_test.dart +++ b/pkgs/csslib/test/font_css_serialization_test.dart @@ -22,6 +22,17 @@ void main() { ); }); + test('line height is retained without explicit font weight', () { + const font = Font( + size: 16, + lineHeight: LineHeight(24), + family: ['Alef'], + ); + expect(font.cssExpression, '16px/24 Alef'); + expect(const Font(size: 16, lineHeight: LineHeight(24)).cssExpression, + '16px/24'); + }); + test('font CSS keeps weight and explicit line height', () { const font = Font( size: 16, From 1ab279512cd5c57885c76bbd911a82bf3aa96fed Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:41:10 +0200 Subject: [PATCH 5/8] style(csslib): keep font line-height assertion analyzer-friendly --- pkgs/csslib/test/font_css_serialization_test.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/csslib/test/font_css_serialization_test.dart b/pkgs/csslib/test/font_css_serialization_test.dart index 5aec697317..c6538d1d46 100644 --- a/pkgs/csslib/test/font_css_serialization_test.dart +++ b/pkgs/csslib/test/font_css_serialization_test.dart @@ -29,8 +29,8 @@ void main() { family: ['Alef'], ); expect(font.cssExpression, '16px/24 Alef'); - expect(const Font(size: 16, lineHeight: LineHeight(24)).cssExpression, - '16px/24'); + const withoutFamily = Font(size: 16, lineHeight: LineHeight(24)); + expect(withoutFamily.cssExpression, '16px/24'); }); test('font CSS keeps weight and explicit line height', () { From d2c24ca723ce46a22d7c651f68690cd428714d15 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:45:14 +0200 Subject: [PATCH 6/8] docs(csslib): document complete clone and font serialization behavior --- pkgs/csslib/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 912c1dac30..4b8257caa8 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -9,6 +9,8 @@ the font size is unspecified. - Serialize partial font styles without bogus `ul` family names or trailing spaces, and join font family lists directly. +- Serialize partial font styles without bogus family names or trailing + spaces; preserve explicit line heights without font weight. - 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 From 46dbf7acc3fca9e6b238d66fc6e25e4b83a28311 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:45:17 +0200 Subject: [PATCH 7/8] docs(csslib): document complete clone and font serialization behavior --- pkgs/csslib/lib/src/property.dart | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index e9ccfa8d2c..14bbc89e6c 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1168,9 +1168,7 @@ class Font implements _StyleProperty { other.variant == variant; } - // TODO(terry): This is fragile should probably just iterate through the list - // of fonts construction the font-family string. - /// Return fonts as a comma seperated list sans the square brackets. + /// Return font family names as a comma-separated list. String get _fontsAsString => family?.join(', ') ?? ''; } From a76249351a782bfc736ed4de190936edcb2f5f4c Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 21:36:25 +0200 Subject: [PATCH 8/8] fix(csslib): keep line-height units and omit missing size in font CSS --- pkgs/csslib/CHANGELOG.md | 3 +++ pkgs/csslib/lib/src/property.dart | 18 ++++++++++---- .../test/font_css_serialization_test.dart | 24 ++++++++++++++++--- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 4b8257caa8..3e557484b0 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -11,6 +11,9 @@ spaces, and join font family lists directly. - Serialize partial font styles without bogus family names or trailing spaces; preserve explicit line heights without font weight. +- Serialize partial font styles without bogus family names, `nullpx` sizes or + trailing spaces; preserve explicit line heights without font weight and + keep the `px` unit on pixel line heights (a unitless value is a multiplier). - 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/property.dart b/pkgs/csslib/lib/src/property.dart index 14bbc89e6c..421046c87b 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1106,12 +1106,20 @@ class Font implements _StyleProperty { /// values are missing the default value is used. @override String get cssExpression { - // Partial font properties need not specify a font family. + // Partial fonts omit absent parts. A unitless line height is a multiplier, + // so pixel heights need their unit. final families = _fontsAsString; - final familySuffix = families.isEmpty ? '' : ' $families'; - final weightPrefix = weight == null ? '' : '$weight '; - final heightSuffix = lineHeight == null ? '' : '/$lineHeightInPixels'; - return '$weightPrefix${size}px$heightSuffix$familySuffix'; + final height = lineHeight; + final sizePart = size == null + ? null + : height == null + ? '${size}px' + : '${size}px/${height.height}${height.inPixels ? 'px' : ''}'; + return [ + if (weight != null) '$weight', + if (sizePart != null) sizePart, + if (families.isNotEmpty) families, + ].join(' '); } Font scale(num ratio) { diff --git a/pkgs/csslib/test/font_css_serialization_test.dart b/pkgs/csslib/test/font_css_serialization_test.dart index c6538d1d46..c5667c6ab6 100644 --- a/pkgs/csslib/test/font_css_serialization_test.dart +++ b/pkgs/csslib/test/font_css_serialization_test.dart @@ -28,9 +28,9 @@ void main() { lineHeight: LineHeight(24), family: ['Alef'], ); - expect(font.cssExpression, '16px/24 Alef'); + expect(font.cssExpression, '16px/24px Alef'); const withoutFamily = Font(size: 16, lineHeight: LineHeight(24)); - expect(withoutFamily.cssExpression, '16px/24'); + expect(withoutFamily.cssExpression, '16px/24px'); }); test('font CSS keeps weight and explicit line height', () { @@ -40,6 +40,24 @@ void main() { lineHeight: LineHeight(24), family: ['Alef'], ); - expect(font.cssExpression, '700 16px/24 Alef'); + expect(font.cssExpression, '700 16px/24px Alef'); + }); + + test('unitless line height stays a multiplier', () { + const font = Font( + size: 16, + lineHeight: LineHeight(1.5, inPixels: false), + family: ['Alef'], + ); + expect(font.cssExpression, '16px/1.5 Alef'); + }); + + test('font CSS omits a missing size', () { + expect(const Font(weight: FontWeight.bold).cssExpression, '700'); + expect(const Font(family: ['Alef']).cssExpression, 'Alef'); + expect( + const Font(lineHeight: LineHeight(1.5, inPixels: false)).cssExpression, + '', + ); }); }