diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 1868125515..3e557484b0 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -7,6 +7,13 @@ - `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. +- 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 e233da1a1d..421046c87b 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1106,16 +1106,20 @@ class Font implements _StyleProperty { /// values are missing the default value is used. @override String get cssExpression { - // TODO(jimhug): include variant, style, other options - if (weight != null) { - // TODO(jacobr): is this really correct for lineHeight? - if (lineHeight != null) { - return '$weight ${size}px/$lineHeightInPixels $_fontsAsString'; - } - return '$weight ${size}px $_fontsAsString'; - } - - return '${size}px $_fontsAsString'; + // Partial fonts omit absent parts. A unitless line height is a multiplier, + // so pixel heights need their unit. + final families = _fontsAsString; + 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) { @@ -1172,13 +1176,8 @@ 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. - String get _fontsAsString { - var fonts = family.toString(); - return fonts.length > 2 ? fonts.substring(1, fonts.length - 1) : ''; - } + /// Return font family names as a comma-separated list. + 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..c5667c6ab6 --- /dev/null +++ b/pkgs/csslib/test/font_css_serialization_test.dart @@ -0,0 +1,63 @@ +// 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('line height is retained without explicit font weight', () { + const font = Font( + size: 16, + lineHeight: LineHeight(24), + family: ['Alef'], + ); + expect(font.cssExpression, '16px/24px Alef'); + const withoutFamily = Font(size: 16, lineHeight: LineHeight(24)); + expect(withoutFamily.cssExpression, '16px/24px'); + }); + + test('font CSS keeps weight and explicit line height', () { + const font = Font( + size: 16, + weight: FontWeight.bold, + lineHeight: LineHeight(24), + family: ['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, + '', + ); + }); +}