From: Jakub Pawlowicz Date: Mon, 24 Aug 2015 04:37:11 +0000 (+0100) Subject: Adds proper handling of an open split character. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=b53c8f837d368b4208df747a16fea46495b4b0c0;p=clean-css.git Adds proper handling of an open split character. When split value starts with an level openinig character we were not handling such cases correctly. --- diff --git a/lib/utils/split.js b/lib/utils/split.js index d4ffa603..8a9e419d 100644 --- a/lib/utils/split.js +++ b/lib/utils/split.js @@ -19,17 +19,19 @@ function split(value, separator, includeSeparator, openLevel, closeLevel) { var len = value.length; var tokens = []; - while (cursor++ < len) { + while (cursor < len) { if (value[cursor] == openLevel) { level++; } else if (value[cursor] == closeLevel) { level--; } - if ((withRegex ? separator.test(value[cursor]) : value[cursor] == separator) && level === 0) { + if (level === 0 && cursor > 0 && cursor + 1 < len && (withRegex ? separator.test(value[cursor]) : value[cursor] == separator)) { tokens.push(value.substring(lastStart, cursor + (includeSeparator ? 1 : 0))); lastStart = cursor + 1; } + + cursor++; } if (lastStart < cursor + 1) diff --git a/test/utils/split-test.js b/test/utils/split-test.js index fb1df8f1..001cc751 100644 --- a/test/utils/split-test.js +++ b/test/utils/split-test.js @@ -107,7 +107,13 @@ vows.describe(split) 'with custom wrappers - on close brace': { topic: 'a{ color:red; --var { color:red; display: none } } p{ color:red }', split: function (input) { - assert.deepEqual(split(input, '}', true, '{', '}'), [ 'a{ color:red; --var { color:red; display: none } }', ' p{ color:red }', '' ]); + assert.deepEqual(split(input, '}', true, '{', '}'), [ 'a{ color:red; --var { color:red; display: none } }', ' p{ color:red }' ]); + } + }, + 'with custom wrappers - one block on close brace': { + topic: '{ color:red; --var { color:red; display: none } color:blue }', + split: function (input) { + assert.deepEqual(split(input, '}', true, '{', '}'), [ '{ color:red; --var { color:red; display: none } color:blue }' ]); } } })