File tree Expand file tree Collapse file tree 4 files changed +34
-1
lines changed Expand file tree Collapse file tree 4 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ ' rrweb-snapshot ' : patch
3+ ---
4+
5+ Fix CSS rules captured in Safari
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ import {
2222 getCssRulesString ,
2323 getInputType ,
2424 toLowerCase ,
25+ validateStringifiedCssRule ,
2526} from './utils' ;
2627
2728let _id = 1 ;
@@ -53,7 +54,9 @@ function getValidTagName(element: HTMLElement): Lowercase<string> {
5354function stringifyStyleSheet ( sheet : CSSStyleSheet ) : string {
5455 return sheet . cssRules
5556 ? Array . from ( sheet . cssRules )
56- . map ( ( rule ) => rule . cssText || '' )
57+ . map ( ( rule ) =>
58+ rule . cssText ? validateStringifiedCssRule ( rule . cssText ) : '' ,
59+ )
5760 . join ( '' )
5861 : '' ;
5962}
Original file line number Diff line number Diff line change @@ -76,6 +76,17 @@ export function getCssRuleString(rule: CSSRule): string {
7676 // ignore
7777 }
7878 }
79+ return validateStringifiedCssRule ( cssStringified ) ;
80+ }
81+
82+ export function validateStringifiedCssRule ( cssStringified : string ) : string {
83+ // Safari does not escape selectors with : properly
84+ if ( cssStringified . includes ( ':' ) ) {
85+ // Replace e.g. [aa:bb] with [aa\\:bb]
86+ const regex = / ( \[ (?: [ \w - ] + ) [ ^ \\ ] ) ( : (?: [ \w - ] + ) \] ) / gm;
87+ return cssStringified . replace ( regex , '$1\\$2' ) ;
88+ }
89+
7990 return cssStringified ;
8091}
8192
Original file line number Diff line number Diff line change 11import { parse , Rule , Media } from '../src/css' ;
2+ import { validateStringifiedCssRule } from './../src/utils' ;
23
34describe ( 'css parser' , ( ) => {
45 it ( 'should save the filename and source' , ( ) => {
@@ -106,4 +107,17 @@ describe('css parser', () => {
106107 decl = rule . declarations ! [ 0 ] ;
107108 expect ( decl . parent ) . toEqual ( rule ) ;
108109 } ) ;
110+
111+ it ( 'parses : in attribute selectors correctly' , ( ) => {
112+ const out1 = validateStringifiedCssRule ( '[data-foo] { color: red; }' ) ;
113+ expect ( out1 ) . toEqual ( '[data-foo] { color: red; }' ) ;
114+
115+ const out2 = validateStringifiedCssRule ( '[data-foo:other] { color: red; }' ) ;
116+ expect ( out2 ) . toEqual ( '[data-foo\\:other] { color: red; }' ) ;
117+
118+ const out3 = validateStringifiedCssRule (
119+ '[data-aa\\:other] { color: red; }' ,
120+ ) ;
121+ expect ( out3 ) . toEqual ( '[data-aa\\:other] { color: red; }' ) ;
122+ } ) ;
109123} ) ;
You can’t perform that action at this time.
0 commit comments