Skip to content

Commit

Permalink
Adds proper handling of an open split character.
Browse files Browse the repository at this point in the history
When split value starts with an level openinig character we were not
handling such cases correctly.
  • Loading branch information
jakubpawlowicz committed Aug 24, 2015
1 parent f2d2a8e commit b53c8f8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/utils/split.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion test/utils/split-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }' ]);
}
}
})
Expand Down

0 comments on commit b53c8f8

Please sign in to comment.