From 07081c766490a9e7bf046bd22d5c69325ef338af Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 17:39:04 +0200 Subject: [PATCH 1/2] [csslib] Index the named color table for color lookups --- pkgs/csslib/CHANGELOG.md | 1 + pkgs/csslib/lib/src/token_kind.dart | 36 ++++++++++++++----------- pkgs/csslib/test/color_lookup_test.dart | 24 +++++++++++++++++ 3 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 pkgs/csslib/test/color_lookup_test.dart diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 2b5e3472da..0f4bf7b695 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. - 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/token_kind.dart b/pkgs/csslib/lib/src/token_kind.dart index 754d661fc7..73d63eb960 100644 --- a/pkgs/csslib/lib/src/token_kind.dart +++ b/pkgs/csslib/lib/src/token_kind.dart @@ -549,28 +549,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_lookup_test.dart b/pkgs/csslib/test/color_lookup_test.dart new file mode 100644 index 0000000000..a4eba4133b --- /dev/null +++ b/pkgs/csslib/test/color_lookup_test.dart @@ -0,0 +1,24 @@ +// 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('named color lookup 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 color 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); + }); +} From 4e519bfec41d93806b3dbc7135b64e4372a7bc5e Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Sat, 14 Nov 2026 18:40:58 +0200 Subject: [PATCH 2/2] Address review feedback --- pkgs/csslib/test/color_lookup_test.dart | 24 ------------------------ pkgs/csslib/test/color_test.dart | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 24 deletions(-) delete mode 100644 pkgs/csslib/test/color_lookup_test.dart diff --git a/pkgs/csslib/test/color_lookup_test.dart b/pkgs/csslib/test/color_lookup_test.dart deleted file mode 100644 index a4eba4133b..0000000000 --- a/pkgs/csslib/test/color_lookup_test.dart +++ /dev/null @@ -1,24 +0,0 @@ -// 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('named color lookup 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 color 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); - }); -} diff --git a/pkgs/csslib/test/color_test.dart b/pkgs/csslib/test/color_test.dart index 4ba519a2cc..9eff32f6b7 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); + }); + }); }