diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 14a6f7dce..6bceb8be4 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,5 +1,6 @@ ## 1.0.3-wip +- Fix `Font.cssExpression` output for partial fonts and line heights. - Correct `Font.hashCode` for partial/empty font styles. - `Color.css` now reads the `hsl()`/`hsla()` hue as degrees (optional `deg`) instead of a 0..1 fraction, and accepts percentages for saturation, lightness diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index e2edae326..5927f062a 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1084,20 +1084,26 @@ class Font implements _StyleProperty { /// /// font-style font-variant font-weight font-size/line-height font-family /// - /// The font-size and font-family values are required. If any of the other - /// values are missing the default value is used. + /// Only the parts that are set are printed, so a partial font (for example + /// one with only a weight, which prints `700`) is not a complete `font` + /// shorthand; that needs at least a size and a family. @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'; + // 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 (style != null) style, + if (variant != null) variant, + if (weight != null) '$weight', + if (sizePart != null) sizePart, + if (families.isNotEmpty) families, + ].join(' '); } Font scale(num ratio) => Font( @@ -1141,13 +1147,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 000000000..516b2f66d --- /dev/null +++ b/pkgs/csslib/test/font_css_serialization_test.dart @@ -0,0 +1,76 @@ +// 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, + '', + ); + }); + + test('font CSS includes style and variant in shorthand order', () { + expect( + const Font( + size: 16, + weight: FontWeight.bold, + style: FontStyle.italic, + variant: FontVariant.smallCaps, + family: ['Alef'], + ).cssExpression, + 'italic small-caps 700 16px Alef', + ); + }); +}