-function split(value, separator, includeSeparator, openLevel, closeLevel, firstOnly) {
- var withRegex = typeof separator != 'string';
- var hasSeparator = withRegex ?
- separator.test(value) :
- value.indexOf(separator) > -1;
-
- if (!hasSeparator)
- return [value];
-
- openLevel = openLevel || '(';
- closeLevel = closeLevel || ')';
-
- if (value.indexOf(openLevel) == -1 && !includeSeparator && !firstOnly)
- return value.split(separator);
+var Marker = require('../tokenizer/marker');
+function split(value, separator) {
+ var openLevel = Marker.OPEN_ROUND_BRACKET;
+ var closeLevel = Marker.CLOSE_ROUND_BRACKET;
var level = 0;
var cursor = 0;
var lastStart = 0;
+ var lastValue;
+ var lastCharacter;
var len = value.length;
- var tokens = [];
+ var parts = [];
+
+ if (value.indexOf(separator) == -1) {
+ return [value];
+ }
+
+ if (value.indexOf(openLevel) == -1) {
+ return value.split(separator);
+ }
while (cursor < len) {
if (value[cursor] == openLevel) {
level--;
}
- 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)));
+ if (level === 0 && cursor > 0 && cursor + 1 < len && value[cursor] == separator) {
+ parts.push(value.substring(lastStart, cursor));
lastStart = cursor + 1;
-
- if (firstOnly && tokens.length == 1) {
- break;
- }
}
cursor++;
}
if (lastStart < cursor + 1) {
- var lastValue = value.substring(lastStart);
- var lastCharacter = lastValue[lastValue.length - 1];
- if (!includeSeparator && (withRegex ? separator.test(lastCharacter) : lastCharacter == separator))
+ lastValue = value.substring(lastStart);
+ lastCharacter = lastValue[lastValue.length - 1];
+ if (lastCharacter == separator) {
lastValue = lastValue.substring(0, lastValue.length - 1);
+ }
- tokens.push(lastValue);
+ parts.push(lastValue);
}
- return tokens;
+ return parts;
}
module.exports = split;
split: function (input) {
assert.deepEqual(split(input, ';'), ['apply(--var)', 'color:red']);
}
- },
- 'with regex': {
- topic: 'no-repeat,0/0',
- split: function (input) {
- assert.deepEqual(split(input, /[ ,\/]/), ['no-repeat', '0', '0']);
- }
- }
- })
- .addBatch({
- 'including separator - leading space and quote': {
- topic: ' "Font"',
- split: function (input) {
- assert.deepEqual(split(input, ' ', true), [' "Font"']);
- }
- },
- 'including separator - comma separated - level 2': {
- topic: 'linear-gradient(0,#fff,rgba(0,0,0)),red',
- split: function (input) {
- assert.deepEqual(split(input, ',', true), ['linear-gradient(0,#fff,rgba(0,0,0)),', 'red']);
- }
- },
- 'including separator - space separated - level 2 with spaces': {
- topic: 'linear-gradient(0, #fff, rgba(0, 0, 0)) red',
- split: function (input) {
- assert.deepEqual(split(input, ' ', true), ['linear-gradient(0, #fff, rgba(0, 0, 0)) ', 'red']);
- }
- },
- 'including separator - with regex': {
- topic: 'no-repeat,0/0',
- split: function (input) {
- assert.deepEqual(split(input, /[ ,\/]/, true), ['no-repeat,', '0/', '0']);
- }
- }
- })
- .addBatch({
- 'with custom wrappers - level 1': {
- topic: 'a{ color:red; width:100% } p{ color:red }',
- split: function (input) {
- assert.deepEqual(split(input, ' ', true, '{', '}'), [ 'a{ color:red; width:100% } ', 'p{ color:red }' ]);
- }
- },
- 'with custom wrappers - level 2': {
- 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 }' ]);
- }
- },
- 'semicolon separated - variable list': {
- topic: '--my-toolbar:{color:red;width:100%}',
- split: function (input) {
- assert.deepEqual(split(input, ';', false, '{', '}'), ['--my-toolbar:{color:red;width:100%}']);
- }
- },
- '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 }' ]);
- }
- },
- '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 }' ]);
- }
- }
- })
- .addBatch({
- 'just first one': {
- topic: 'linear-gradient(0, #fff, rgba(0, 0, 0)) red',
- split: function (input) {
- assert.deepEqual(split(input, ' ', false, '(', ')', true), ['linear-gradient(0, #fff, rgba(0, 0, 0))']);
- }
- },
- 'just first one when no opening token': {
- topic: 'red blue',
- split: function (input) {
- assert.deepEqual(split(input, ' ', false, '(', ')', true), ['red']);
- }
- },
- 'just first one when no closing token in last token': {
- topic: 'red linear-gradient(0 0',
- split: function (input) {
- assert.deepEqual(split(input, ' ', false, '(', ')', true), ['red']);
- }
}
})
.export(module);