Skip to content
Merged
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
14 changes: 13 additions & 1 deletion packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ function getCssRuleString(rule: CSSRule): string {
// ignore
}
}

return validateStringifiedCssRule(cssStringified);
}

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

return cssStringified;
}

Expand All @@ -78,7 +90,7 @@ function isCSSImportRule(rule: CSSRule): rule is CSSImportRule {
function stringifyStyleSheet(sheet: CSSStyleSheet): string {
return sheet.cssRules
? Array.from(sheet.cssRules)
.map((rule) => rule.cssText || '')
.map((rule) => rule.cssText ? validateStringifiedCssRule(rule.cssText) : '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work now for inline css

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, could you verify this with an actual example app?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mydea Tested this out and it seems to work

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, feel free to merge then!

.join('')
: '';
}
Expand Down
12 changes: 12 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/snapshot';

describe('css parser', () => {
it('should save the filename and source', () => {
Expand Down Expand Up @@ -106,4 +107,15 @@ 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; }');
})
});
1 change: 1 addition & 0 deletions packages/rrweb-snapshot/typings/snapshot.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { serializedNodeWithId, INode, idNodeMap, MaskInputOptions, SlimDOMOptions, DataURLOptions, MaskTextFn, MaskInputFn, KeepIframeSrcFn } from './types';
export declare const IGNORED_NODE = -2;
export declare function validateStringifiedCssRule(cssStringified: string): string;
export declare function absoluteToStylesheet(cssText: string | null, href: string): string;
export declare function absoluteToDoc(doc: Document, attributeValue: string): string;
export declare function transformAttribute(doc: Document, element: HTMLElement, _tagName: string, _name: string, value: string | null, maskAllText: boolean, unmaskTextSelector: string | undefined | null, maskTextFn: MaskTextFn | undefined): string | null;
Expand Down