Index: lib/common.js |
diff --git a/lib/common.js b/lib/common.js |
index 54118e54d82caa582ce9b16d0d467eef21987388..9bf00db7d5f89e8bfd516a1259055087148e33db 100644 |
--- a/lib/common.js |
+++ b/lib/common.js |
@@ -48,5 +48,44 @@ function filterToRegExp(text) |
.replace(/(\.\*)$/, ""); |
} |
-if (typeof exports != "undefined") |
- exports.filterToRegExp = filterToRegExp; |
+exports.filterToRegExp = filterToRegExp; |
+ |
+function splitSelector(selector) |
+{ |
+ if (selector.indexOf(",") == -1) |
+ return [selector]; |
+ |
+ let selectors = []; |
+ let start = 0; |
+ let level = 0; |
+ let sep = ""; |
+ |
+ for (let i = 0; i < selector.length; i++) |
+ { |
+ let chr = selector[i]; |
+ |
+ if (chr == "\\") // ignore escaped characters |
+ i++; |
+ else if (chr == sep) // don't split within quoted text |
+ sep = ""; // e.g. [attr=","] |
+ else if (sep == "") |
+ { |
+ if (chr == '"' || chr == "'") |
+ sep = chr; |
+ else if (chr == "(") // don't split between parentheses |
+ level++; // e.g. :matches(div,span) |
+ else if (chr == ")") |
+ level = Math.max(0, level - 1); |
+ else if (chr == "," && level == 0) |
+ { |
+ selectors.push(selector.substring(start, i)); |
+ start = i + 1; |
+ } |
+ } |
+ } |
+ |
+ selectors.push(selector.substring(start)); |
+ return selectors; |
+} |
+ |
+exports.splitSelector = splitSelector; |