Skip to content
7 changes: 7 additions & 0 deletions pkgs/csslib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 16 additions & 17 deletions pkgs/csslib/lib/src/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions pkgs/csslib/test/font_css_serialization_test.dart
Original file line number Diff line number Diff line change
@@ -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,
'',
);
});
}
Loading