From c5c974009e24448aed622d4aa8d845ce220945e9 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 19:30:41 +0200 Subject: [PATCH 1/8] Cover alpha bytes >= 0x80 in ARGB round-trip tests --- pkgs/csslib/test/color_conversion_test.dart | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pkgs/csslib/test/color_conversion_test.dart b/pkgs/csslib/test/color_conversion_test.dart index e4cb0d05a3..b573fd3fe5 100644 --- a/pkgs/csslib/test/color_conversion_test.dart +++ b/pkgs/csslib/test/color_conversion_test.dart @@ -22,10 +22,17 @@ void main() { expect(Rgba(0xff, 0, 0, 1).argbValue, 0xffff0000); }); - test('ARGB round-trips retain each alpha byte, including 0x7f', () { - expect(const Color.hex('7f112233').rgba.toHexArgbString(), '7f112233'); - expect(Hsla.fromArgbValue(0x7f808080).toHexArgbString(), '7f808080'); - }); + test( + 'ARGB round-trips retain each alpha byte, including 0x7f and >= 0x80', + () { + expect(const Color.hex('7f112233').rgba.toHexArgbString(), '7f112233'); + expect(Hsla.fromArgbValue(0x7f808080).toHexArgbString(), '7f808080'); + expect(Hsla.fromArgbValue(0x80808080).toHexArgbString(), '80808080'); + expect(Hsla.fromArgbValue(0xff808080).toHexArgbString(), 'ff808080'); + expect(Rgba.fromArgbValue(0xff808080).argbValue, 0xff808080); + expect(Rgba.fromArgbValue(0x80808080).argbValue, 0x80808080); + }, + ); test('color string factories decode CSS colors only once', () { expect(Rgba.fromString('rgba(255,0,0,0.5)').toHexArgbString(), '80ff0000'); From 7a34d95a0b6aa2a25da6a6c0ee21563005610ba6 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 19:31:04 +0200 Subject: [PATCH 2/8] Throw a FormatException for unknown color names in Color.css --- pkgs/csslib/test/color_test.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkgs/csslib/test/color_test.dart b/pkgs/csslib/test/color_test.dart index 13073851dd..94f41f99df 100644 --- a/pkgs/csslib/test/color_test.dart +++ b/pkgs/csslib/test/color_test.dart @@ -56,6 +56,10 @@ void main() { expect(() => Color.css('#-12345'), throwsFormatException); expect(() => Color.css('# 12345'), throwsFormatException); }); + + test('unknown color names throw a FormatException', () { + expect(() => Color.css('notacolor'), throwsFormatException); + }); }); group('named colors', () { From 8f801f13c2fe2c7b0ed31dcb5d09bab05a42187f Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 19:31:26 +0200 Subject: [PATCH 3/8] 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 ecce859d9d..1a44bbd4f1 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -278,9 +278,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 3ee96917eba604438e67542d41b895321fdad618 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 19:31:29 +0200 Subject: [PATCH 4/8] Document TokenKind.decimalToHex --- pkgs/csslib/lib/src/token_kind.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkgs/csslib/lib/src/token_kind.dart b/pkgs/csslib/lib/src/token_kind.dart index 7a977ef317..73b4bf8fc3 100644 --- a/pkgs/csslib/lib/src/token_kind.dart +++ b/pkgs/csslib/lib/src/token_kind.dart @@ -607,6 +607,9 @@ class TokenKind { static String? hexToColorName(Object hexValue) => _colorNamesByValue[hexValue]; + /// Converts [number] to lowercase hexadecimal, zero-padded to [minDigits]. + /// + /// A negative [number] keeps its sign in front; only the digits are padded. static String decimalToHex(int number, [int minDigits = 1]) { final hex = number.toRadixString(16); if (number < 0) { From 82727b379b6347c23da31dd429f9d2b4e22c0f57 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 19:31:41 +0200 Subject: [PATCH 5/8] Add Color.rebeccaPurple --- pkgs/csslib/lib/src/property.dart | 1 + pkgs/csslib/test/token_lookup_test.dart | 1 + 2 files changed, 2 insertions(+) diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 1a44bbd4f1..103cc28565 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -540,6 +540,7 @@ class Color implements _StyleProperty, ColorBase { static final Color plum = const Color.hex('dda0dd'); static final Color powderBlue = const Color.hex('b0e0e6'); static final Color purple = const Color.hex('800080'); + static final Color rebeccaPurple = const Color.hex('663399'); static final Color red = const Color.hex('ff0000'); static final Color rosyBrown = const Color.hex('bc8f8f'); static final Color royalBlue = const Color.hex('4169e1'); diff --git a/pkgs/csslib/test/token_lookup_test.dart b/pkgs/csslib/test/token_lookup_test.dart index 5d01799e96..12bc7b94d7 100644 --- a/pkgs/csslib/test/token_lookup_test.dart +++ b/pkgs/csslib/test/token_lookup_test.dart @@ -17,6 +17,7 @@ void main() { ); expect(TokenKind.colorValue(TokenKind.matchColorName('rebeccapurple')!), 0x663399); + expect(Color.rebeccaPurple.argbValue, 0x663399); expect(TokenKind.matchColorName('not-a-color'), isNull); }); From 289df369217eb3669f775379d343a7bedd901efb Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 19:31:47 +0200 Subject: [PATCH 6/8] Skip matchList when no keyword has the identifier's length --- pkgs/csslib/lib/src/token_kind.dart | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkgs/csslib/lib/src/token_kind.dart b/pkgs/csslib/lib/src/token_kind.dart index 73b4bf8fc3..bc678a5dee 100644 --- a/pkgs/csslib/lib/src/token_kind.dart +++ b/pkgs/csslib/lib/src/token_kind.dart @@ -531,9 +531,11 @@ class TokenKind { String text, int offset, int length, - ) => - matchList(groups[length] ?? const >[], field, text, - offset, length); + ) { + final group = groups[length]; + if (group == null) return -1; + return matchList(group, field, text, offset, length); + } /// Return the token that matches the unit ident found. static int matchUnits(String text, int offset, int length) => From de8241f17993dceddefdacdc9c94c34cf466ffed Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 19:32:02 +0200 Subject: [PATCH 7/8] Parse escape hex digits with radix 16 and seed the buffer directly --- pkgs/csslib/lib/src/tokenizer.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkgs/csslib/lib/src/tokenizer.dart b/pkgs/csslib/lib/src/tokenizer.dart index 163425a803..27b5abe120 100644 --- a/pkgs/csslib/lib/src/tokenizer.dart +++ b/pkgs/csslib/lib/src/tokenizer.dart @@ -336,12 +336,13 @@ class Tokenizer extends TokenizerBase { // Decode CSS escapes without changing the raw source span. if (ch == 92 /*\*/ && _inString) { - decoded ??= StringBuffer()..write(_text.substring(_startIndex, _index)); + decoded ??= StringBuffer(_text.substring(_startIndex, _index)); final startHex = ++_index; eatHexDigits(startHex + 6); if (_index != startHex) { final hexVal = - int.tryParse('0x${_text.substring(startHex, _index)}') ?? 0xFFFD; + int.tryParse(_text.substring(startHex, _index), radix: 16) ?? + 0xFFFD; decoded.writeCharCode(hexVal <= 0x10FFFF ? hexVal : 0xFFFD); if (_index == _text.length) break; From 0a203ba5ebce62f1b42b7f0c772f6c6a72b441df Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 19:36:16 +0200 Subject: [PATCH 8/8] Update the changelog for the review fixes --- pkgs/csslib/CHANGELOG.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 20dc636f4b..e860bc3a9b 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -8,14 +8,15 @@ - 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. + 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 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. - 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 @@ -23,7 +24,7 @@ - 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. +- 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