Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/csslib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 20 additions & 19 deletions pkgs/csslib/lib/src/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');
}
Comment thread
palmoni5 marked this conversation as resolved.
Resolved

Font scale(num ratio) => Font(
Expand Down Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions pkgs/csslib/test/font_css_serialization_test.dart
Original file line number Diff line number Diff line change
@@ -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',
);
});
}
Comment thread
palmoni5 marked this conversation as resolved.
Resolved
Loading