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

- Correct `Font.hashCode` for partial/empty font styles.
- `Color.css` now reads the `hsl()`/`hsla()` hue as degrees (optional `deg`)
instead of a 0..1 fraction, and accepts percentages for saturation, lightness
and alpha. Functional colors with the wrong number of components or invalid
Expand Down
3 changes: 1 addition & 2 deletions pkgs/csslib/lib/src/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1128,8 +1128,7 @@ class Font implements _StyleProperty {

@override
int get hashCode =>
// TODO(jimhug): Lot's of potential collisions here. List of fonts, etc.
size!.toInt() % family![0].hashCode;
Object.hash(size, family, weight, lineHeight, style, variant);

@override
bool operator ==(Object other) {
Expand Down
39 changes: 39 additions & 0 deletions pkgs/csslib/test/font_hash_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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('hashCode is safe for partial and empty font properties', () {
final fonts = [
const Font(),
const Font(size: 12),
const Font(family: []),
const Font(size: 12, family: []),
const Font(size: 12, family: ['serif']),
];
for (final font in fonts) {
expect(() => font.hashCode, returnsNormally);
expect(font.hashCode, isA<int>());
}
});

test('equal fonts have equal hashes using the existing equality policy', () {
final family = <String>['Alef', 'serif'];
final first = Font(
size: 16,
family: family,
weight: FontWeight.bold,
);
final second = Font(
size: 16,
family: family,
weight: FontWeight.bold,
);
expect(first, second);
expect(first.hashCode, second.hashCode);
expect({first: 'found'}[second], 'found');
});
}
Loading