Skip to content

Commit

Permalink
fix: Fix CSS rules captured in Safari (#1253)
Browse files Browse the repository at this point in the history
* fix: Fix CSS rules captured in Safari

* Apply formatting changes

* add changeset

* fix

---------

Co-authored-by: mydea <mydea@users.noreply.github.com>
  • Loading branch information
mydea and mydea authored Jul 7, 2023
1 parent d0fbe23 commit c6600e7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/thirty-baboons-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rrweb-snapshot': patch
---

Fix CSS rules captured in Safari
5 changes: 4 additions & 1 deletion packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getCssRulesString,
getInputType,
toLowerCase,
validateStringifiedCssRule,
} from './utils';

let _id = 1;
Expand Down Expand Up @@ -53,7 +54,9 @@ function getValidTagName(element: HTMLElement): Lowercase<string> {
function stringifyStyleSheet(sheet: CSSStyleSheet): string {
return sheet.cssRules
? Array.from(sheet.cssRules)
.map((rule) => rule.cssText || '')
.map((rule) =>
rule.cssText ? validateStringifiedCssRule(rule.cssText) : '',
)
.join('')
: '';
}
Expand Down
11 changes: 11 additions & 0 deletions packages/rrweb-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ export function getCssRuleString(rule: CSSRule): string {
// ignore
}
}
return validateStringifiedCssRule(cssStringified);
}

export function validateStringifiedCssRule(cssStringified: string): string {
// Safari does not escape selectors with : properly
if (cssStringified.includes(':')) {
// Replace e.g. [aa:bb] with [aa\\:bb]
const regex = /(\[(?:[\w-]+)[^\\])(:(?:[\w-]+)\])/gm;
return cssStringified.replace(regex, '$1\\$2');
}

return cssStringified;
}

Expand Down
14 changes: 14 additions & 0 deletions packages/rrweb-snapshot/test/css.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parse, Rule, Media } from '../src/css';
import { validateStringifiedCssRule } from './../src/utils';

describe('css parser', () => {
it('should save the filename and source', () => {
Expand Down Expand Up @@ -106,4 +107,17 @@ describe('css parser', () => {
decl = rule.declarations![0];
expect(decl.parent).toEqual(rule);
});

it('parses : in attribute selectors correctly', () => {
const out1 = validateStringifiedCssRule('[data-foo] { color: red; }');
expect(out1).toEqual('[data-foo] { color: red; }');

const out2 = validateStringifiedCssRule('[data-foo:other] { color: red; }');
expect(out2).toEqual('[data-foo\\:other] { color: red; }');

const out3 = validateStringifiedCssRule(
'[data-aa\\:other] { color: red; }',
);
expect(out3).toEqual('[data-aa\\:other] { color: red; }');
});
});

0 comments on commit c6600e7

Please sign in to comment.