Skip to content

Commit 84b4e19

Browse files
committed
Ensure we are ignoring commas within strings
- the added test case didn't actually fail before due to some voodoo with `m.replace(/,/g, '\u200C');` which took me ages to figure out. Anyhow I figure test and refactor is good in case we replace the css library
1 parent 89fd41f commit 84b4e19

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

packages/rrweb-snapshot/src/css.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -440,21 +440,21 @@ export function parse(css: string, options: ParserOptions = {}): Stylesheet {
440440
const char = input[i];
441441
currentPart += char;
442442

443-
if (char === '(' && !currentStringChar) {
443+
const hasStringEscape = i > 0 && input[i - 1] === '\\';
444+
445+
if (currentStringChar) {
446+
if (currentStringChar === char && !hasStringEscape) {
447+
currentStringChar = null;
448+
}
449+
} else if (char === '(') {
444450
nestedLevel++;
445-
} else if (char === ')' && !currentStringChar) {
451+
} else if (char === ')') {
446452
nestedLevel--;
447453
} else if (char === ',' && nestedLevel === 0) {
448454
parts.push(currentPart.slice(0, -1).trim());
449455
currentPart = '';
450456
} else if ('\'"'.includes(char)) {
451-
if (currentStringChar === char) {
452-
if (!(i > 0 && input[i - 1] === '\\')) {
453-
currentStringChar = null;
454-
}
455-
} else {
456-
currentStringChar = char;
457-
}
457+
currentStringChar = char;
458458
}
459459
}
460460

packages/rrweb-snapshot/test/css.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ li[attr="weirder\\"("] a:hover, li[attr="weirder\\")"] a {
150150
expect(
151151
(weirderresult.stylesheet!.rules[0] as Rule)!.selectors!.length,
152152
).toEqual(2);
153+
154+
const commainstrresult = parse(
155+
`
156+
li[attr="has,comma"] a:hover {
157+
background-color: red;
158+
}
159+
`,
160+
);
161+
expect(
162+
(commainstrresult.stylesheet!.rules[0] as Rule)!.selectors!.length,
163+
).toEqual(1);
164+
153165
});
154166

155167
it('parses imports with quotes correctly', () => {

0 commit comments

Comments
 (0)