Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/csslib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
36 changes: 21 additions & 15 deletions pkgs/csslib/lib/src/token_kind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -550,28 +550,34 @@ class TokenKind {
return '<BAD UNIT>'; // 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<String, Object>? 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<String, Map<String, Object>> _colorNamesByName = {
for (final entry in _EXTENDED_COLOR_NAMES) entry['name'] as String: entry,
};

/// Match a CSS color keyword case-insensitively.
static Map<String, Object>? matchColorName(String text) =>
_colorNamesByName[text.toLowerCase()];

/// Return RGB value as [int] from a color entry in _EXTENDED_COLOR_NAMES.
static int colorValue(Map<String, Object> 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<int, String> _colorNamesByValue = () {
final colors = <int, String>{};
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];
Comment thread
palmoni5 marked this conversation as resolved.
Resolved

static String decimalToHex(int number, [int minDigits = 1]) {
final hexDigits = '0123456789abcdef';
Expand Down
18 changes: 18 additions & 0 deletions pkgs/csslib/test/color_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}
Loading