-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (106 loc) · 3.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"use strict";
const BRACES = {
"(": ")",
"[": "]",
'"': '"',
"'": "'"
};
const CLOSING_BRACES = {
")": "(",
"]": "["
};
function splitSelector(selector, splitCharacters = [","]) {
let currentBraces = [],
selectorLen = selector.length,
selectors = [],
joiners = [],
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/splitter
let lastJoiner = joiners[joiners.length - 1];
if (lastJoiner === " " && currentSelector.length <= 0) {
// we just split by a space, but there seems to be another split character, so use
// this new one instead of the previous space
joiners[joiners.length - 1] = char;
} else if (currentSelector.length <= 0) {
// skip this character, as it's just padding
} else {
// split by this character
let newLength = selectors.push(currentSelector.trim());
joiners[newLength - 1] = char;
currentSelector = "";
}
} else {
// we're inside another block, so ignore the comma/splitter
currentSelector += char;
}
} else {
// just add this character
currentSelector += char;
}
}
selectors.push(currentSelector.trim());
return {
selectors: selectors.filter((cssSelector) => cssSelector.length > 0),
joiners: joiners
};
}
// output:
function extractSelectors(selector, splitChars) {
let split = splitSelector(selector, splitChars);
return split.selectors;
}
function extractSelectorBlocks(selector) {
return splitSelector(selector, ["+", "~", ">", " "]);
}
function joinParts(selectors, joiners) {
let selector = "",
selectorCount = selectors.length;
selectors.forEach(function(part, index) {
let suffix = joiners[index];
if (!suffix) {
if ((selectorCount - 1) === index) {
suffix = "";
} else {
throw new Error(`No joiner for index: ${index}`);
}
} else {
if (suffix !== " ") {
suffix = ` ${suffix} `;
}
}
selector += part + suffix;
});
return selector;
}
extractSelectors.splitSelectorBlocks = extractSelectorBlocks;
extractSelectors.joinSelector = joinParts;
module.exports = extractSelectors;