Skip to content

Commit

Permalink
better splitting of selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
daibhin committed Apr 9, 2024
1 parent 3d1877c commit 8f9fe85
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions packages/rrweb-snapshot/src/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,40 @@ export function parse(css: string, options: ParserOptions = {}): Stylesheet {
if (!m) {
return;
}

function splitRootSelectors(input: string) {
const parts = [];
let nestedLevel = 0;
let currentPart = '';

for (let i = 0; i < input.length; i++) {
const char = input[i];
currentPart += char;

if (char === '(') {
nestedLevel++;
} else if (char === ')') {
nestedLevel--;
} else if (char === ',' && nestedLevel === 0) {
parts.push(currentPart.slice(0, -1).trim());
currentPart = '';
}
}

if (currentPart.trim() !== '') {
parts.push(currentPart.trim());
}

return parts;
}

/* @fix Remove all comments from selectors
* http://ostermiller.org/findcomment.html */
return trim(m[0])
return splitRootSelectors(trim(m[0])
.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, '')
.replace(/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'/g, (m) => {
return m.replace(/,/g, '\u200C');
})
.split(/\s*(?![^(]*\)),\s*/)
}))
.map((s) => {
return s.replace(/\u200C/g, ',');
});
Expand Down

0 comments on commit 8f9fe85

Please sign in to comment.