From 0a4cc46d09caeacff83143aaf2a963836666e9aa Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 22:17:19 +0200 Subject: [PATCH 1/4] [csslib] Serialize partial Font styles as valid CSS --- pkgs/csslib/CHANGELOG.md | 49 +++++++++++++++ pkgs/csslib/lib/src/property.dart | 33 +++++----- .../test/font_css_serialization_test.dart | 63 +++++++++++++++++++ 3 files changed, 128 insertions(+), 17 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 2b5e3472da..3e557484b0 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,5 +1,54 @@ ## 1.0.3-wip +- Correct `Font.hashCode` for partial/empty font styles and distribute hashes + across font families and other properties, avoiding hash-table collisions. +- Preserve fixed/relative line heights and independent font-family lists + when scaling fonts. +- `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 + two digits. `Rgba.fromString` and `Hsla.fromString` parse the CSS color once, + and `Color.css` throws a `FormatException` for unsupported colors. +- Correct CSS namespace URI serialization in pretty-printed stylesheets while + preserving compact `url()` representation. +- Fix fractional alpha in the numeric `Color` constructor and normalize CSS + short and eight-digit hex colors, with validation of malformed input and + unknown color names. + `Color.css` also accepts named colors and `transparent`, and + `Rgba.fromString`/`Hsla.fromString` now parse their argument only once. +- `Color.css` now reads the `hsl()`/`hsla()` hue as a CSS angle (degrees by + default, or `deg`, `grad`, `rad` and `turn`) instead of a 0..1 fraction, and + accepts percentages for saturation, lightness, RGB channels and alpha. + Functional colors with the wrong number of components or invalid or + non-finite values now throw a `FormatException` whose source is the color. +- Correctly parse unit-like type selectors (`rem`, `vw`, `dpi`, etc.) at the + start of `selector()` and `parseSelectorGroup()` input and of style rules. +- Use native integer hexadecimal formatting for named CSS colors, reducing + temporary allocations and avoiding hangs on negative conversion inputs. +- Speed up named CSS color lookups by indexing the color table. +- Recognize the `@left-bottom` page-margin directive, and stop parsing + `@right-bottom` as `@left-bottom`. +- Recognize the `rebeccapurple` color keyword and add `Color.rebeccaPurple`. +- Reduce repeated CSS unit, directive, and media keyword scans by indexing + keyword tables by length while retaining allocation-free token matching. +- Reduce allocations when tokenizing ordinary CSS identifiers; add a parsing + benchmark and regression coverage for Unicode and escaped identifiers. +- Flatten nested top-level selectors with a single ordered pass instead of + repeated linear searches and list insertions on large stylesheets. +- Avoid recursive tokenizer calls when skipping CSS comments and whitespace, + preventing stack overflow on comment-heavy stylesheets. +- End HTML comments at the first `-->` even when more dashes precede it, and + don't end comments at a NUL character. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 0188c2b8e2..20711cd9a1 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1025,16 +1025,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) => Font( @@ -1079,13 +1083,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, + '', + ); + }); +} From 3b747d65e40a1b2e86ad73a68fa5806e36e30fad Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 23:26:21 +0200 Subject: [PATCH 2/4] Keep only this change's changelog entry --- pkgs/csslib/CHANGELOG.md | 46 ---------------------------------------- 1 file changed, 46 deletions(-) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 3e557484b0..eccecc1720 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,54 +1,8 @@ ## 1.0.3-wip -- Correct `Font.hashCode` for partial/empty font styles and distribute hashes - across font families and other properties, avoiding hash-table collisions. -- Preserve fixed/relative line heights and independent font-family lists - when scaling fonts. -- `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 - two digits. `Rgba.fromString` and `Hsla.fromString` parse the CSS color once, - and `Color.css` throws a `FormatException` for unsupported colors. -- Correct CSS namespace URI serialization in pretty-printed stylesheets while - preserving compact `url()` representation. -- Fix fractional alpha in the numeric `Color` constructor and normalize CSS - short and eight-digit hex colors, with validation of malformed input and - unknown color names. - `Color.css` also accepts named colors and `transparent`, and - `Rgba.fromString`/`Hsla.fromString` now parse their argument only once. -- `Color.css` now reads the `hsl()`/`hsla()` hue as a CSS angle (degrees by - default, or `deg`, `grad`, `rad` and `turn`) instead of a 0..1 fraction, and - accepts percentages for saturation, lightness, RGB channels and alpha. - Functional colors with the wrong number of components or invalid or - non-finite values now throw a `FormatException` whose source is the color. -- Correctly parse unit-like type selectors (`rem`, `vw`, `dpi`, etc.) at the - start of `selector()` and `parseSelectorGroup()` input and of style rules. -- Use native integer hexadecimal formatting for named CSS colors, reducing - temporary allocations and avoiding hangs on negative conversion inputs. -- Speed up named CSS color lookups by indexing the color table. -- Recognize the `@left-bottom` page-margin directive, and stop parsing - `@right-bottom` as `@left-bottom`. -- Recognize the `rebeccapurple` color keyword and add `Color.rebeccaPurple`. -- Reduce repeated CSS unit, directive, and media keyword scans by indexing - keyword tables by length while retaining allocation-free token matching. -- Reduce allocations when tokenizing ordinary CSS identifiers; add a parsing - benchmark and regression coverage for Unicode and escaped identifiers. -- Flatten nested top-level selectors with a single ordered pass instead of - repeated linear searches and list insertions on large stylesheets. -- Avoid recursive tokenizer calls when skipping CSS comments and whitespace, - preventing stack overflow on comment-heavy stylesheets. -- End HTML comments at the first `-->` even when more dashes precede it, and - don't end comments at a NUL character. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, From 0e97e5deb2296bf61b2668c741130d459498f9a2 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 23:27:32 +0200 Subject: [PATCH 3/4] Serialize the font style and variant --- pkgs/csslib/CHANGELOG.md | 3 ++- pkgs/csslib/lib/src/property.dart | 2 ++ pkgs/csslib/test/font_css_serialization_test.dart | 13 +++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index eccecc1720..4ceb579efb 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -2,7 +2,8 @@ - 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). + keep the `px` unit on pixel line heights (a unitless value is a multiplier); + include the font style and variant. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 20711cd9a1..ce14f66e5e 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1035,6 +1035,8 @@ class Font implements _StyleProperty { ? '${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, diff --git a/pkgs/csslib/test/font_css_serialization_test.dart b/pkgs/csslib/test/font_css_serialization_test.dart index c5667c6ab6..516b2f66d2 100644 --- a/pkgs/csslib/test/font_css_serialization_test.dart +++ b/pkgs/csslib/test/font_css_serialization_test.dart @@ -60,4 +60,17 @@ void main() { '', ); }); + + 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', + ); + }); } From 7826b98aa97f60829fb884a40facc6507e88440b Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Mon, 16 Nov 2026 00:19:24 +0200 Subject: [PATCH 4/4] Address review feedback --- pkgs/csslib/CHANGELOG.md | 5 +---- pkgs/csslib/lib/src/property.dart | 8 ++++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 4ceb579efb..71483d795d 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,9 +1,6 @@ ## 1.0.3-wip -- 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); - include the font style and variant. +- Fix `Font.cssExpression` output for partial fonts and line heights. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index ce14f66e5e..e5432e820d 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1021,12 +1021,12 @@ 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 { - // Partial fonts omit absent parts. A unitless line height is a multiplier, - // so pixel heights need their unit. + // A unitless line height is a multiplier, so pixel heights need their unit. final families = _fontsAsString; final height = lineHeight; final sizePart = size == null