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

Fix for #1379 #1380

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
22 changes: 21 additions & 1 deletion packages/rrweb-snapshot/src/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

// Check if parentheses are balanced
function areParenthesesBalanced(str: string) {
let balance = 0;

for (const char of str) {
if (char === '(') {
balance++;
} else if (char === ')') {
balance--;
if (balance < 0) {
return false;
}
}
}

return balance === 0; // Balanced if balance is 0
}

const HOVER_SELECTOR = /([^\\]):hover/;
const HOVER_SELECTOR_GLOBAL = new RegExp(HOVER_SELECTOR.source, 'g');
export function addHoverClass(cssText: string, cache: BuildCache): string {
Expand All @@ -81,7 +99,9 @@
if ('selectors' in rule) {
(rule.selectors || []).forEach((selector: string) => {
if (HOVER_SELECTOR.test(selector)) {
selectors.push(selector);
if (areParenthesesBalanced(selector)) {
selectors.push(selector);
}
}
});
}
Expand Down Expand Up @@ -490,7 +510,7 @@

for (const id of mirror.getIds()) {
if (mirror.has(id)) {
walk(mirror.getNode(id)!);

Check warning on line 513 in packages/rrweb-snapshot/src/rebuild.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb-snapshot/src/rebuild.ts#L513

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions packages/rrweb-snapshot/test/rebuild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ describe('rebuild', function () {
expect(addHoverClass(cssText, cache)).toEqual(cssText);
});

it('check that parentheses are balanced', () => {
const cssText = '[_nghost-ng-c4172599085]:not(.fit-content).aim-select:hover:not(:disabled, [_nghost-ng-c4172599085]:not(.fit-content).aim-select--disabled, [_nghost-ng-c4172599085]:not(.fit-content).aim-select--invalid, [_nghost-ng-c4172599085]:not(.fit-content).aim-select--active) { border-color: rgb(84, 84, 84); }';
expect(addHoverClass(cssText, cache)).toEqual(cssText);
});

// this benchmark is unreliable when run in parallel with other tests
it.skip('benchmark', () => {
const cssText = fs.readFileSync(
Expand Down
Loading