From 98f1c7dca3f271c1c02be9fa0ae7d81fab302413 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Mon, 17 Oct 2016 17:42:19 -0700 Subject: [PATCH] Fix a unit conversion bug. --- lib/src/value/number.dart | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/src/value/number.dart b/lib/src/value/number.dart index 787cb097..d7daa94f 100644 --- a/lib/src/value/number.dart +++ b/lib/src/value/number.dart @@ -497,18 +497,20 @@ class SassNumber extends Value { List denominators2) { // Short-circuit without allocating any new unit lists if possible. if (numerators1.isEmpty) { - if (denominators1.isEmpty) { - return new SassNumber.withUnits(value, - numeratorUnits: numerators2, denominatorUnits: denominators2); - } else if (denominators2.isEmpty) { + if (denominators2.isEmpty) { return new SassNumber.withUnits(value, numeratorUnits: numerators2, denominatorUnits: denominators1); + } else if (denominators1.isEmpty && + !_areAnyConvertible(numerators1, denominators2)) { + return new SassNumber.withUnits(value, + numeratorUnits: numerators2, denominatorUnits: denominators2); } } else if (numerators2.isEmpty) { - if (denominators1.isEmpty) { + if (denominators2.isEmpty) { return new SassNumber.withUnits(value, numeratorUnits: numerators1, denominatorUnits: denominators2); - } else if (denominators2.isEmpty) { + } else if (denominators1.isEmpty && + !_areAnyConvertible(numerators1, denominators2)) { return new SassNumber.withUnits(value, numeratorUnits: numerators1, denominatorUnits: denominators2); } @@ -526,6 +528,7 @@ class SassNumber extends Value { var factor = _conversionFactor(numerator, denominator); if (factor == null) return false; value *= factor; + return true; }, orElse: () { newNumerators.add(numerator); }); @@ -552,6 +555,16 @@ class SassNumber extends Value { denominatorUnits: mutableDenominators1..addAll(mutableDenominators2)); } + /// Returns whether there exists a unit in [units1] that can be converted to a + /// unit in [units2]. + bool _areAnyConvertible(List units1, List units2) { + return units1.any((unit1) { + if (!_isConvertable(unit1)) return false; + var innerMap = _conversions[unit1]; + return units2.any(innerMap.containsKey); + }); + } + /// Returns whether [unit] can be converted to or from any other units. bool _isConvertable(String unit) => _conversions.containsKey(unit);