Fix crash using `Color.css` with rgb(), rgba(). (#104)
Fixes #88
Use `.toInt()` over `as int` to convert `double` to `int`.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index be21202..a5415ee 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.17.1-dev
+
+- Fix `Color.css` constructor when there are double values in the `rgba` string.
+
## 0.17.0
- Migrate to null safety.
diff --git a/lib/src/property.dart b/lib/src/property.dart
index a8c3c80..c3b74bc 100644
--- a/lib/src/property.dart
+++ b/lib/src/property.dart
@@ -258,10 +258,10 @@
switch (type) {
case _rgbCss:
return Color.convertToHexString(
- args[0] as int, args[1] as int, args[2] as int);
+ args[0].toInt(), args[1].toInt(), args[2].toInt());
case _rgbaCss:
return Color.convertToHexString(
- args[0] as int, args[1] as int, args[2] as int, args[3]);
+ args[0].toInt(), args[1].toInt(), args[2].toInt(), args[3]);
case _hslCss:
return Hsla(args[0], args[1], args[2]).toHexArgbString();
case _hslaCss:
@@ -290,15 +290,7 @@
return '$aHex$rHex$gHex$bHex'.toLowerCase();
}
- static String _numAs2DigitHex(num v) {
- // TODO(terry): v.toInt().toRadixString instead of v.toRadixString
- // Bug <http://code.google.com/p/dart/issues/detail?id=2671>.
- var hex = v.toInt().toRadixString(16);
- if (hex.length == 1) {
- hex = '0${hex}';
- }
- return hex;
- }
+ static String _numAs2DigitHex(int v) => v.toRadixString(16).padLeft(2, '0');
static T _clamp<T extends num>(T value, T min, T max) =>
math.max(math.min(max, value), min);
diff --git a/pubspec.yaml b/pubspec.yaml
index 2656efb..ed521e9 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: csslib
-version: 0.17.0
+version: 0.17.1-dev
description: A library for parsing CSS.
repository: https://github.com/dart-lang/csslib
diff --git a/test/color_test.dart b/test/color_test.dart
new file mode 100644
index 0000000..9231848
--- /dev/null
+++ b/test/color_test.dart
@@ -0,0 +1,20 @@
+// Copyright (c) 2021, 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() {
+ group('css', () {
+ test('rgb', () {
+ final color = Color.css('rgb(0, 0, 255)');
+ expect(color, equals(Color(0x0000FF)));
+ });
+
+ test('rgba', () {
+ final color = Color.css('rgba(0, 0, 255, 1.0)');
+ expect(color, equals(Color.createRgba(0, 0, 255, 1.0)));
+ });
+ });
+}