From 1b35440fdfc4c0ee132d5dff41529ace1803afbd Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 17:27:03 +0200 Subject: [PATCH 1/3] [csslib] Simplify TokenKind.decimalToHex and stop hanging on negatives --- pkgs/csslib/CHANGELOG.md | 2 ++ pkgs/csslib/lib/src/token_kind.dart | 27 ++++---------------- pkgs/csslib/test/hex_formatting_test.dart | 30 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 pkgs/csslib/test/hex_formatting_test.dart diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 2b5e3472da..ce70ecd05a 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,5 +1,7 @@ ## 1.0.3-wip +- Use native integer hexadecimal formatting for named CSS colors, reducing + temporary allocations and avoiding hangs on negative conversion inputs. - 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..e7ebf8a771 100644 --- a/pkgs/csslib/lib/src/token_kind.dart +++ b/pkgs/csslib/lib/src/token_kind.dart @@ -573,29 +573,12 @@ class TokenKind { } static String decimalToHex(int number, [int minDigits = 1]) { - final hexDigits = '0123456789abcdef'; - - var result = []; - - var dividend = number >> 4; - var remain = number % 16; - result.add(hexDigits[remain]); - while (dividend != 0) { - remain = dividend % 16; - dividend >>= 4; - result.add(hexDigits[remain]); - } - - var invertResult = StringBuffer(); - var paddings = minDigits - result.length; - while (paddings-- > 0) { - invertResult.write('0'); - } - for (var i = result.length - 1; i >= 0; i--) { - invertResult.write(result[i]); + final hex = number.toRadixString(16); + if (number < 0) { + // Pad the digits, not the sign. + return '-${hex.substring(1).padLeft(minDigits, '0')}'; } - - return invertResult.toString(); + return hex.padLeft(minDigits, '0'); } static String kindToString(int kind) => switch (kind) { diff --git a/pkgs/csslib/test/hex_formatting_test.dart b/pkgs/csslib/test/hex_formatting_test.dart new file mode 100644 index 0000000000..030e4ad9d0 --- /dev/null +++ b/pkgs/csslib/test/hex_formatting_test.dart @@ -0,0 +1,30 @@ +// 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:csslib/visitor.dart'; +import 'package:test/test.dart'; + +void main() { + test('hex conversion retains color padding', () { + expect(TokenKind.decimalToHex(0), '0'); + expect(TokenKind.decimalToHex(0, 6), '000000'); + expect(TokenKind.decimalToHex(15, 2), '0f'); + expect(TokenKind.decimalToHex(0xabcdef, 6), 'abcdef'); + expect(TokenKind.decimalToHex(0xffffff, 6), 'ffffff'); + }); + + test('negative hex conversion terminates safely', () { + expect(TokenKind.decimalToHex(-1), '-1'); + expect(TokenKind.decimalToHex(-255, 4), '-00ff'); + }); + + test('named CSS colors still roundtrip through the parser and printer', () { + final errors = []; + final sheet = parse('a { color: aliceblue; }', errors: errors); + expect(errors, isEmpty); + expect((CssPrinter()..visitTree(sheet, pretty: true)).toString(), + 'a {\n color: #f0f8ff;\n}'); + }); +} From a6b50e8323d5f61971a9f3be57dad6b1dae47693 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 19:31:29 +0200 Subject: [PATCH 2/3] Document TokenKind.decimalToHex --- pkgs/csslib/lib/src/token_kind.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkgs/csslib/lib/src/token_kind.dart b/pkgs/csslib/lib/src/token_kind.dart index e7ebf8a771..04891916da 100644 --- a/pkgs/csslib/lib/src/token_kind.dart +++ b/pkgs/csslib/lib/src/token_kind.dart @@ -572,6 +572,9 @@ class TokenKind { return null; } + /// Converts [number] to lowercase hexadecimal, zero-padded to [minDigits]. + /// + /// A negative [number] keeps its sign in front; only the digits are padded. static String decimalToHex(int number, [int minDigits = 1]) { final hex = number.toRadixString(16); if (number < 0) { From 9229b4e4cba270e5634a4d36b0b8698668a0f348 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Sat, 14 Nov 2026 18:41:47 +0200 Subject: [PATCH 3/3] Address review feedback --- pkgs/csslib/CHANGELOG.md | 3 +-- pkgs/csslib/test/hex_formatting_test.dart | 9 --------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index ce70ecd05a..3039f51cb5 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,7 +1,6 @@ ## 1.0.3-wip -- Use native integer hexadecimal formatting for named CSS colors, reducing - temporary allocations and avoiding hangs on negative conversion inputs. +- Fix `TokenKind.decimalToHex` hanging on negative input. - 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/test/hex_formatting_test.dart b/pkgs/csslib/test/hex_formatting_test.dart index 030e4ad9d0..80270da176 100644 --- a/pkgs/csslib/test/hex_formatting_test.dart +++ b/pkgs/csslib/test/hex_formatting_test.dart @@ -3,7 +3,6 @@ // BSD-style license that can be found in the LICENSE file. import 'package:csslib/parser.dart'; -import 'package:csslib/visitor.dart'; import 'package:test/test.dart'; void main() { @@ -19,12 +18,4 @@ void main() { expect(TokenKind.decimalToHex(-1), '-1'); expect(TokenKind.decimalToHex(-255, 4), '-00ff'); }); - - test('named CSS colors still roundtrip through the parser and printer', () { - final errors = []; - final sheet = parse('a { color: aliceblue; }', errors: errors); - expect(errors, isEmpty); - expect((CssPrinter()..visitTree(sheet, pretty: true)).toString(), - 'a {\n color: #f0f8ff;\n}'); - }); }