File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import cssEscape from 'css.escape'
22
3- const headCSSRegex = needle => new RegExp ( `\\\\[${ needle } ]` , 'g' )
3+ function unescapeRegExp ( string ) {
4+ return string . replace ( / \\ ( .) / g, '$1' )
5+ }
46
57export default ( options = { } ) => root => {
6- root . walkDecls ( decl => {
7- if ( decl . parent . selector ) {
8- Object . entries ( options . replacements ) . forEach ( ( [ from , to ] ) => {
9- decl . parent . selector = decl . parent . selector
10- . replace ( headCSSRegex ( cssEscape ( from ) ) , to )
11- . replace ( '\\2c ' , '_' )
8+ const replacementsRegex = new RegExp ( '\\\\(' + Object . keys ( options . replacements ) . map ( cssEscape ) . join ( '|' ) + ')' , 'g' )
9+
10+ root . walkRules ( rule => {
11+ rule . selector = rule . selector
12+ . replace ( replacementsRegex , matched => {
13+ return options . replacements [ unescapeRegExp ( matched ) ]
1214 } )
13- }
15+ // Replace escaped commas with underscores
16+ . replaceAll ( '\\2c ' , '_' )
1417 } )
1518}
Original file line number Diff line number Diff line change @@ -81,3 +81,40 @@ test('Ignores known class name patterns', t => {
8181 ] ,
8282 } )
8383} )
84+
85+ test ( 'Works with escaped selectors in string sources' , async ( ) => {
86+ const html = `
87+ <style>
88+ .sm\\:text-left {
89+ text-align: left;
90+ }
91+ .w-1\\.5 {
92+ width: 1.5rem;
93+ }
94+ </style>
95+ <div class="sm:text-left w-1.5">foo</div>
96+ `
97+
98+ const result = await posthtml ( [
99+ plugin ( {
100+ replacements : {
101+ ':' : '__' ,
102+ '.' : '_dot_' ,
103+ }
104+ } )
105+ ] )
106+ . process ( html )
107+ . then ( result => result . html )
108+
109+ expect ( result ) . toBe ( `
110+ <style>
111+ .sm__text-left {
112+ text-align: left;
113+ }
114+ .w-1_dot_5 {
115+ width: 1.5rem;
116+ }
117+ </style>
118+ <div class="sm__text-left w-1_dot_5">foo</div>
119+ ` )
120+ } )
You can’t perform that action at this time.
0 commit comments