From da25f4fed2627087a3a97420759b7223cbc3e881 Mon Sep 17 00:00:00 2001 From: Dave Clark Date: Wed, 13 Mar 2019 10:52:41 +0000 Subject: [PATCH] Prevent error if property block has no value (level 0) (#1061) The tokenizer will parse a property like `width: ;`, but the property helper for level 0 (i.e. minification only) was not accounting for properties without a value and would throw a TypeError if the value was missing With this change, the property helper now checks for a missing property value before trying to access it --- lib/writer/helpers.js | 8 ++++++-- test/optimizer/level-0/optimizations-test.js | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/writer/helpers.js b/lib/writer/helpers.js index 11727402c..6cbb54074 100644 --- a/lib/writer/helpers.js +++ b/lib/writer/helpers.js @@ -77,7 +77,9 @@ function lastPropertyIndex(tokens) { function property(context, tokens, position, lastPropertyAt) { var store = context.store; var token = tokens[position]; - var isPropertyBlock = token[2][0] == Token.PROPERTY_BLOCK; + + var propertyValue = token[2]; + var isPropertyBlock = propertyValue && propertyValue[0] === Token.PROPERTY_BLOCK; var needsSemicolon; if ( context.format ) { @@ -111,7 +113,9 @@ function property(context, tokens, position, lastPropertyAt) { case Token.PROPERTY: store(context, token[1]); store(context, colon(context)); - value(context, token); + if (propertyValue) { + value(context, token); + } store(context, needsSemicolon ? semicolon(context, Breaks.AfterProperty, isLast) : emptyCharacter); break; case Token.RAW: diff --git a/test/optimizer/level-0/optimizations-test.js b/test/optimizer/level-0/optimizations-test.js index 7fa74a633..c86166b19 100644 --- a/test/optimizer/level-0/optimizations-test.js +++ b/test/optimizer/level-0/optimizations-test.js @@ -11,4 +11,12 @@ vows.describe('level 0') ] }, { level: 0 }) ) + .addBatch( + optimizerContext('empty properties', { + 'are written': [ + 'a{color:#f00;font-weight:;background:red}', + 'a{color:#f00;font-weight:;background:red}' + ] + }, { level: 0 }) + ) .export(module);