diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 3bd0734d1..7248531c7 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,5 +1,6 @@ ## 1.0.3-wip +- Speed up named CSS color lookups by indexing the color table. - Recognize the `rebeccapurple` color keyword. - Correctly parse unit-like type selectors (`rem`, `vw`, `dpi`, etc.) at the start of `selector()` and `parseSelectorGroup()` input and of style rules. diff --git a/pkgs/csslib/lib/src/token_kind.dart b/pkgs/csslib/lib/src/token_kind.dart index 60b62008f..541e0bbcb 100644 --- a/pkgs/csslib/lib/src/token_kind.dart +++ b/pkgs/csslib/lib/src/token_kind.dart @@ -550,28 +550,34 @@ class TokenKind { return ''; // Not a unit token. } - /// Match color name, case insensitive match and return the associated color - /// entry from _EXTENDED_COLOR_NAMES list, return `null` if not found. - static Map? matchColorName(String text) { - var name = text.toLowerCase(); - for (var color in _EXTENDED_COLOR_NAMES) { - if (color['name'] == name) return color; - } - return null; - } + // Keep the existing color entry objects for callers, but avoid scanning the + // entire CSS color-name table for every parsed identifier. + static final Map> _colorNamesByName = { + for (final entry in _EXTENDED_COLOR_NAMES) entry['name'] as String: entry, + }; + + /// Match a CSS color keyword case-insensitively. + static Map? matchColorName(String text) => + _colorNamesByName[text.toLowerCase()]; /// Return RGB value as [int] from a color entry in _EXTENDED_COLOR_NAMES. static int colorValue(Map entry) => entry['value'] as int; - static String? hexToColorName(Object hexValue) { + // Match the old linear scan's first-name-wins behavior for aliases such as + // aqua/cyan and fuchsia/magenta, while avoiding a scan for every print. + static final Map _colorNamesByValue = () { + final colors = {}; for (final entry in _EXTENDED_COLOR_NAMES) { - if (entry['value'] == hexValue) { - return entry['name'] as String?; - } + colors.putIfAbsent( + entry['value'] as int, + () => entry['name'] as String, + ); } + return colors; + }(); - return null; - } + static String? hexToColorName(Object hexValue) => + _colorNamesByValue[hexValue]; static String decimalToHex(int number, [int minDigits = 1]) { final hexDigits = '0123456789abcdef'; diff --git a/pkgs/csslib/test/color_test.dart b/pkgs/csslib/test/color_test.dart index 4ba519a2c..9eff32f6b 100644 --- a/pkgs/csslib/test/color_test.dart +++ b/pkgs/csslib/test/color_test.dart @@ -32,4 +32,22 @@ void main() { expect(prettyPrint(stylesheet), 'a {\n color: #f0f8ff;\n}'); }); }); + + group('named color lookup', () { + test('remains case insensitive', () { + expect(TokenKind.colorValue(TokenKind.matchColorName('RED')!), 0xFF0000); + expect( + TokenKind.colorValue(TokenKind.matchColorName('aliceblue')!), + 0xF0F8FF, + ); + expect(TokenKind.matchColorName('not-a-color'), isNull); + }); + + test('reverse lookups preserve first canonical aliases', () { + expect(TokenKind.hexToColorName(0x00FFFF), 'aqua'); + expect(TokenKind.hexToColorName(0xFF00FF), 'fuchsia'); + expect(TokenKind.hexToColorName(0xF0F8FF), 'aliceblue'); + expect(TokenKind.hexToColorName(0x010203), isNull); + }); + }); }