Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better splitting of selectors #1440

Merged
merged 17 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/modern-doors-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rrweb-snapshot': patch
---

better nested css selector splitting
eoghanmurray marked this conversation as resolved.
Show resolved Hide resolved
52 changes: 43 additions & 9 deletions packages/rrweb-snapshot/src/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,17 +429,51 @@ export function parse(css: string, options: ParserOptions = {}): Stylesheet {
if (!m) {
return;
}

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

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

if (char === '(' && !currentStringChar) {
nestedLevel++;
} else if (char === ')' && !currentStringChar) {
nestedLevel--;
} else if (char === ',' && nestedLevel === 0) {
parts.push(currentPart.slice(0, -1).trim());
currentPart = '';
} else if (char.match(/"|'|`/)) {
eoghanmurray marked this conversation as resolved.
Show resolved Hide resolved
daibhin marked this conversation as resolved.
Show resolved Hide resolved
if (currentStringChar === char) {
currentStringChar = null;
daibhin marked this conversation as resolved.
Show resolved Hide resolved
} else {
currentStringChar = char;
}
}
}

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

return parts;
}

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

/**
Expand Down
22 changes: 22 additions & 0 deletions packages/rrweb-snapshot/test/css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ describe('css parser', () => {
expect(out3).toEqual('[data-aa\\:other] { color: red; }');
});

it('parses nested commas in selectors correctly', () => {
const result = parse(
`
body > ul :is(li:not(:first-of-type) a:hover, li:not(:first-of-type).active a) {
background: red;
}
`,
);
expect((result.stylesheet!.rules[0] as Rule)!.selectors!.length).toEqual(1);

const trickresult = parse(
`
li[attr="weirdly("] a:hover, li[attr="weirdly)"] a {
background-color: red;
}
`,
);
expect(
(trickresult.stylesheet!.rules[0] as Rule)!.selectors!.length,
).toEqual(2);
});

it('parses imports with quotes correctly', () => {
const out1 = escapeImportStatement({
cssText: `@import url("/foo.css;900;800"");`,
Expand Down
Loading