From cd6daadb2b2c2d1efdedfa4d9b3fe2c5c4613b6b Mon Sep 17 00:00:00 2001 From: Leon Senft Date: Mon, 8 May 2017 13:46:39 -0700 Subject: [PATCH] Prevents exception for invalid dimension terms While running outside of checked mode, parsing an invalid term expression followed by a known dimension unit like width: Infinity%; caused the following exception to be thrown: The getter 'text' was called on null. Receiver: null Instead the parser now discards the invalid term, likely resulting in a useful error message indicating the source of failure such as error on ...: expected }, but found % width: Infinity%; ^ Fixes #43. --- lib/parser.dart | 2 +- test/declaration_test.dart | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/parser.dart b/lib/parser.dart index 4c65a29..9903b4c 100644 --- a/lib/parser.dart +++ b/lib/parser.dart @@ -2476,7 +2476,7 @@ class _Parser { break; } - return processDimension(t, value, _makeSpan(start)); + return t != null ? processDimension(t, value, _makeSpan(start)) : null; } /** Process all dimension units. */ diff --git a/test/declaration_test.dart b/test/declaration_test.dart index a9a51cb..05e504d 100644 --- a/test/declaration_test.dart +++ b/test/declaration_test.dart @@ -69,6 +69,17 @@ void testSimpleTerms() { expect(stylesheet != null, true); expect(errors.isEmpty, true, reason: errors.toString()); expect(prettyPrint(stylesheet), generated2); + + // Regression test to ensure invalid percentages don't throw an exception and + // instead print a useful error message when not in checked mode. + var css = ''' +.foo { + width: Infinity%; +}'''; + stylesheet = parseCss(css, errors: errors..clear(), opts: simpleOptions); + expect(errors, isNotEmpty); + expect(errors.first.message, 'expected }, but found %'); + expect(errors.first.span.text, '%'); } /**