const BRACES = {
    "(": ")",
    "[": "]",
    '"': '"',
    "'": "'"
};

const CLOSING_BRACES = {
    ")": "(",
    "]": "["
};

function splitSelector(selector, splitCharacters = [","]) {
    let currentBraces = [],
        selectorLen = selector.length,
        selectors = [],
        currentSelector = "",
        closingBraces = {};
    for (var i = 0; i < selectorLen; i += 1) {
        let char = selector[i];
        if (BRACES.hasOwnProperty(char)) {
            if (currentBraces.length === 0) {
                currentBraces.push(char);
            } else {
                let lastBrace = currentBraces[currentBraces.length - 1];
                if (lastBrace === "\"" || lastBrace === "'") {
                    // within quotes
                    if (char === lastBrace) {
                        // closing quote
                        currentBraces.pop();
                    }
                } else {
                    // inside brackets or square brackets
                    currentBraces.push(char);
                }
            }
            currentSelector += char;
        } else if (CLOSING_BRACES.hasOwnProperty(char)) {
            let lastBrace = currentBraces[currentBraces.length - 1],
                matchingOpener = CLOSING_BRACES[char];
            if (lastBrace === matchingOpener) {
                currentBraces.pop();
            }
            currentSelector += char;
        } else if (splitCharacters.indexOf(char) >= 0) {
            if (currentBraces.length <= 0) {
                // we're not inside another block, so we can split using the comma
                selectors.push(currentSelector.trim());
                currentSelector = "";
            } else {
                // we're inside another block, so ignore the comma
                currentSelector += char;
            }
        } else {
            // just add this character
            currentSelector += char;
        }
    }
    selectors.push(currentSelector.trim());
    return selectors.filter((cssSelector) => cssSelector.length > 0);
}

function extractSelectorBlocks(selector) {
    return splitSelector(selector, ["+", "~", ">", " "]);
}

splitSelector.extractBlocks = extractSelectorBlocks;

module.exports = splitSelector;

@@ -268,32 +268,54 @@
 selectors = %5B%5D,%0A
+        joiners = %5B%5D,%0A
         currentS
@@ -1606,32 +1606,41 @@
  using the comma
+/splitter
 %0A               
@@ -1635,24 +1635,476 @@
             
+ let lastJoiner = joiners%5Bjoiners.length - 1%5D;%0A                if (currentSelector.length %3C= 0 && lastJoiner === %22 %22) %7B%0A                    // we just split by a space, but there seems to be another split character, so use%0A                    // this new one instead of the previous space%0A                    joiners%5Bjoiners.length - 1%5D = char;%0A                %7D else %7B%0A                    // split by this character%0A                    let newLength =
  selectors.p
@@ -2128,24 +2128,79 @@
 or.trim());%0A
+                    joiners%5BnewLength - 1%5D = char;%0A    
             
@@ -2225,16 +2225,34 @@
 r = %22%22;%0A
+                %7D%0A
         
@@ -2330,16 +2330,25 @@
 he comma
+/splitter
 %0A       
@@ -2558,16 +2558,37 @@
   return
+ %7B%0A        selectors:
  selecto
@@ -2641,167 +2641,971 @@
 %3E 0)
-;%0A%7D%0A%0Afunction extractSelectorBlocks(selector) %7B%0A    return splitSelector(selector, %5B%22+%22, %22~%22, %22%3E%22, %22 %22%5D);%0A%7D%0A%0AsplitSelector.extractBlocks = extractSelectorBlock
+,%0A        joiners: joiners%0A    %7D;%0A%7D%0A%0A// output:%0A%0Afunction extractSelectors(selector, splitChars) %7B%0A    let split = splitSelector(selector, splitChars);%0A    return split.selectors;%0A%7D%0A%0Afunction extractSelectorBlocks(selector) %7B%0A    return splitSelector(selector, %5B%22+%22, %22~%22, %22%3E%22, %22 %22%5D);%0A%7D%0A%0Afunction joinParts(selectors, joiners) %7B%0A    let selector = %22%22,%0A        selectorCount = selectors.length;%0A    selectors.forEach(function(part, index) %7B%0A        let suffix = joiners%5Bindex%5D;%0A        if (!suffix) %7B%0A            if ((selectorCount - 1) === index) %7B%0A                suffix = %22%22;%0A            %7D else %7B%0A                throw new Error(%60No joiner for index: $%7Bindex%7D%60);%0A            %7D%0A        %7D else %7B%0A            if (suffix !== %22 %22) %7B%0A                suffix = %60 $%7Bsuffix%7D %60;%0A            %7D%0A        %7D%0A        selector += part + suffix;%0A    %7D);%0A    return selector;%0A%7D%0A%0AextractSelectors.splitSelectorBlocks = extractSelectorBlocks;%0A%0AextractSelectors.joinSelector = joinPart
 s;%0A%0A
@@ -3621,20 +3621,22 @@
 ports = 
-spli
+extrac
 tSelecto
@@ -3632,14 +3632,15 @@
 ractSelector
+s
 ;%0A

Rework splitting algorithm