From 040cb1dbd6726d8d5961dc03a6b795b7414d93d8 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 17:27:00 +0200 Subject: [PATCH 1/4] [csslib] Parse CSS hsl() angles and percentages in Color.css --- pkgs/csslib/CHANGELOG.md | 5 ++ pkgs/csslib/lib/src/property.dart | 82 +++++++++++++++++++---- pkgs/csslib/test/color_function_test.dart | 47 +++++++++++++ 3 files changed, 122 insertions(+), 12 deletions(-) create mode 100644 pkgs/csslib/test/color_function_test.dart diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 2b5e3472da..30702d0e42 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,5 +1,10 @@ ## 1.0.3-wip +- `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 non-finite values + now throw a `FormatException`. - 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..9bebbfd231 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -250,26 +250,84 @@ class Color implements _StyleProperty, ColorBase { color = color.substring(0, color.length - 1); // Strip close paren. - var args = []; - var params = color.split(','); - for (var param in params) { - args.add(double.parse(param)); + final params = color.split(','); + final hasAlpha = type == _rgbaCss || type == _hslaCss; + if (params.length != (hasAlpha ? 4 : 3)) { + throw FormatException('Invalid CSS color component count', value); } + + double parseNumber(String text) { + final number = double.parse(text.trim()); + if (!number.isFinite) { + throw FormatException('Non-finite CSS color component', value); + } + return number; + } + + double parseFraction(String text) { + final trimmed = text.trim(); + if (trimmed.endsWith('%')) { + return parseNumber(trimmed.substring(0, trimmed.length - 1)) / 100; + } + return parseNumber(trimmed); + } + + int parseRgbChannel(String text) { + final trimmed = text.trim(); + if (trimmed.endsWith('%')) { + return (parseFraction(trimmed) * 255).round(); + } + return parseNumber(trimmed).toInt(); + } + + double parseHue(String text) { + final trimmed = text.trim().toLowerCase(); + double degrees; + if (trimmed.endsWith('turn')) { + degrees = parseNumber(trimmed.substring(0, trimmed.length - 4)) * 360; + } else if (trimmed.endsWith('grad')) { + degrees = parseNumber(trimmed.substring(0, trimmed.length - 4)) * 0.9; + } else if (trimmed.endsWith('rad')) { + degrees = parseNumber(trimmed.substring(0, trimmed.length - 3)) * + 180 / + math.pi; + } else if (trimmed.endsWith('deg')) { + degrees = parseNumber(trimmed.substring(0, trimmed.length - 3)); + } else { + degrees = parseNumber(trimmed); + } + // CSS hue is an angle, not the 0..1 fraction expected by Hsla. + // Dart's `%` is never negative here, so -120 wraps to 240 too. + return degrees % 360 / 360; + } + switch (type) { case _rgbCss: return Color.convertToHexString( - args[0].toInt(), args[1].toInt(), args[2].toInt()); + parseRgbChannel(params[0]), + parseRgbChannel(params[1]), + parseRgbChannel(params[2]), + ); case _rgbaCss: return Color.convertToHexString( - args[0].toInt(), args[1].toInt(), args[2].toInt(), args[3]); + parseRgbChannel(params[0]), + parseRgbChannel(params[1]), + parseRgbChannel(params[2]), + parseFraction(params[3]), + ); case _hslCss: - return Hsla(args[0], args[1], args[2]).toHexArgbString(); + return Hsla( + parseHue(params[0]), + parseFraction(params[1]), + parseFraction(params[2]), + ).toHexArgbString(); case _hslaCss: - return Hsla(args[0], args[1], args[2], args[3]).toHexArgbString(); - default: - // Type not defined UnsupportedOperationException should have thrown. - assert(false); - break; + return Hsla( + parseHue(params[0]), + parseFraction(params[1]), + parseFraction(params[2]), + parseFraction(params[3]), + ).toHexArgbString(); } } return null; diff --git a/pkgs/csslib/test/color_function_test.dart b/pkgs/csslib/test/color_function_test.dart new file mode 100644 index 0000000000..036265af18 --- /dev/null +++ b/pkgs/csslib/test/color_function_test.dart @@ -0,0 +1,47 @@ +// 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('HSL function converts degrees and percentages', () { + expect(Color.css('hsl(0, 100%, 50%)'), Color(0xff0000)); + expect(Color.css('hsl(120, 100%, 50%)'), Color(0x00ff00)); + expect(Color.css('hsl(240, 100%, 50%)'), Color(0x0000ff)); + }); + + test('HSL hue wraps and respects CSS angle units', () { + expect(Color.css('hsl(480deg,100%,50%)'), Color(0x00ff00)); + expect(Color.css('hsl(-120deg,100%,50%)'), Color(0x0000ff)); + expect(Color.css('hsl(0.5turn,100%,50%)'), Color(0x00ffff)); + expect(Color.css('hsl(100grad,100%,50%)'), Color(0x80ff00)); + expect(Color.css('hsl(1.5707963267948966rad,100%,50%)'), Color(0x80ff00)); + expect(Color.css('HSL(120DEG,100%,50%)'), Color(0x00ff00)); + }); + + test('HSLA parses fractional and percentage alpha', () { + expect( + Color.css('hsla(240,100%,50%,0.5)').toHexArgbString(), + '800000ff', + ); + expect( + Color.css('hsla(240,100%,50%,50%)').toHexArgbString(), + '800000ff', + ); + }); + + test('RGB and RGBA accept percentages', () { + expect(Color.css('rgb(100%,0%,0%)'), Color(0xff0000)); + expect( + Color.css('rgba(100%,0%,0%,50%)').toHexArgbString(), + '80ff0000', + ); + }); + + test('invalid color arity and nonfinite components fail cleanly', () { + expect(() => Color.css('rgba(10,20,30)'), throwsFormatException); + expect(() => Color.css('rgb(NaN,20,30)'), throwsFormatException); + }); +} From 7a014a1a196b56692d2a23ed33dc4510b9c6d2e9 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 19:31:26 +0200 Subject: [PATCH 2/4] Report the whole CSS color as the source of component parse errors --- pkgs/csslib/lib/src/property.dart | 6 +++--- pkgs/csslib/test/color_function_test.dart | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 9bebbfd231..7f339c4cb2 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -257,9 +257,9 @@ class Color implements _StyleProperty, ColorBase { } double parseNumber(String text) { - final number = double.parse(text.trim()); - if (!number.isFinite) { - throw FormatException('Non-finite CSS color component', value); + final number = double.tryParse(text.trim()); + if (number == null || !number.isFinite) { + throw FormatException('Invalid CSS color component', value); } return number; } diff --git a/pkgs/csslib/test/color_function_test.dart b/pkgs/csslib/test/color_function_test.dart index 036265af18..d4a41efad0 100644 --- a/pkgs/csslib/test/color_function_test.dart +++ b/pkgs/csslib/test/color_function_test.dart @@ -44,4 +44,12 @@ void main() { expect(() => Color.css('rgba(10,20,30)'), throwsFormatException); expect(() => Color.css('rgb(NaN,20,30)'), throwsFormatException); }); + + test('component errors report the whole color as their source', () { + expect( + () => Color.css('rgb(1x,20,30)'), + throwsA(isA() + .having((e) => e.source, 'source', 'rgb(1x,20,30)')), + ); + }); } From 72795b83aadfa5def816068e7b0a58bdd22fe8c7 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 19:36:17 +0200 Subject: [PATCH 3/4] Update the changelog for the review fix --- pkgs/csslib/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 30702d0e42..c1a8c943d3 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -3,8 +3,8 @@ - `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 non-finite values - now throw a `FormatException`. + Functional colors with the wrong number of components or invalid or + non-finite values now throw a `FormatException` whose source is the color. - 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 264968e4223a60c6f5f05847869da850b81827f8 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Sat, 14 Nov 2026 18:39:42 +0200 Subject: [PATCH 4/4] Address review feedback --- pkgs/csslib/CHANGELOG.md | 9 ++++---- pkgs/csslib/lib/src/property.dart | 25 ++++------------------- pkgs/csslib/test/color_function_test.dart | 19 ----------------- 3 files changed, 8 insertions(+), 45 deletions(-) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index c1a8c943d3..2f16773112 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,10 +1,9 @@ ## 1.0.3-wip -- `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. +- `Color.css` now reads the `hsl()`/`hsla()` hue as degrees (optional `deg`) + instead of a 0..1 fraction, and accepts percentages for saturation, lightness + and alpha. Functional colors with the wrong number of components or invalid + or non-finite values now throw a `FormatException`. - 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 7f339c4cb2..8dfdec07a7 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -272,30 +272,13 @@ class Color implements _StyleProperty, ColorBase { return parseNumber(trimmed); } - int parseRgbChannel(String text) { - final trimmed = text.trim(); - if (trimmed.endsWith('%')) { - return (parseFraction(trimmed) * 255).round(); - } - return parseNumber(trimmed).toInt(); - } + int parseRgbChannel(String text) => parseNumber(text).toInt(); double parseHue(String text) { final trimmed = text.trim().toLowerCase(); - double degrees; - if (trimmed.endsWith('turn')) { - degrees = parseNumber(trimmed.substring(0, trimmed.length - 4)) * 360; - } else if (trimmed.endsWith('grad')) { - degrees = parseNumber(trimmed.substring(0, trimmed.length - 4)) * 0.9; - } else if (trimmed.endsWith('rad')) { - degrees = parseNumber(trimmed.substring(0, trimmed.length - 3)) * - 180 / - math.pi; - } else if (trimmed.endsWith('deg')) { - degrees = parseNumber(trimmed.substring(0, trimmed.length - 3)); - } else { - degrees = parseNumber(trimmed); - } + final degrees = trimmed.endsWith('deg') + ? parseNumber(trimmed.substring(0, trimmed.length - 3)) + : parseNumber(trimmed); // CSS hue is an angle, not the 0..1 fraction expected by Hsla. // Dart's `%` is never negative here, so -120 wraps to 240 too. return degrees % 360 / 360; diff --git a/pkgs/csslib/test/color_function_test.dart b/pkgs/csslib/test/color_function_test.dart index d4a41efad0..f932687329 100644 --- a/pkgs/csslib/test/color_function_test.dart +++ b/pkgs/csslib/test/color_function_test.dart @@ -15,9 +15,6 @@ void main() { test('HSL hue wraps and respects CSS angle units', () { expect(Color.css('hsl(480deg,100%,50%)'), Color(0x00ff00)); expect(Color.css('hsl(-120deg,100%,50%)'), Color(0x0000ff)); - expect(Color.css('hsl(0.5turn,100%,50%)'), Color(0x00ffff)); - expect(Color.css('hsl(100grad,100%,50%)'), Color(0x80ff00)); - expect(Color.css('hsl(1.5707963267948966rad,100%,50%)'), Color(0x80ff00)); expect(Color.css('HSL(120DEG,100%,50%)'), Color(0x00ff00)); }); @@ -32,24 +29,8 @@ void main() { ); }); - test('RGB and RGBA accept percentages', () { - expect(Color.css('rgb(100%,0%,0%)'), Color(0xff0000)); - expect( - Color.css('rgba(100%,0%,0%,50%)').toHexArgbString(), - '80ff0000', - ); - }); - test('invalid color arity and nonfinite components fail cleanly', () { expect(() => Color.css('rgba(10,20,30)'), throwsFormatException); expect(() => Color.css('rgb(NaN,20,30)'), throwsFormatException); }); - - test('component errors report the whole color as their source', () { - expect( - () => Color.css('rgb(1x,20,30)'), - throwsA(isA() - .having((e) => e.source, 'source', 'rgb(1x,20,30)')), - ); - }); }