diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 52127b3b7..14a6f7dce 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -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 diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 6f9af7e08..e2edae326 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -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) { diff --git a/pkgs/csslib/test/font_hash_test.dart b/pkgs/csslib/test/font_hash_test.dart new file mode 100644 index 000000000..5648b8eb5 --- /dev/null +++ b/pkgs/csslib/test/font_hash_test.dart @@ -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()); + } + }); + + test('equal fonts have equal hashes using the existing equality policy', () { + final family = ['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'); + }); +}