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 diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index ecce859d9d..103cc28565 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; } @@ -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/lib/src/token_kind.dart b/pkgs/csslib/lib/src/token_kind.dart index 7a977ef317..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) => @@ -607,6 +609,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) { 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; 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'); 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)')), + ); + }); } 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', () { 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); });