Adds proper handling of an open split character.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 24 Aug 2015 04:37:11 +0000 (05:37 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 24 Aug 2015 05:50:44 +0000 (06:50 +0100)
When split value starts with an level openinig character we were not
handling such cases correctly.

lib/utils/split.js
test/utils/split-test.js

index d4ffa60..8a9e419 100644 (file)
@@ -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)
index fb1df8f..001cc75 100644 (file)
@@ -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 }' ]);
       }
     }
   })