File tree Expand file tree Collapse file tree 2 files changed +55
-4
lines changed Expand file tree Collapse file tree 2 files changed +55
-4
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ const {
10
10
logger,
11
11
getRelativePath,
12
12
isFileOrInDirectory,
13
+ escapeClassName
13
14
} = require ( "./utils" ) ;
14
15
const path = require ( "path" ) ;
15
16
@@ -172,15 +173,15 @@ module.exports = (options = {}) => {
172
173
newClassName = getRandomName ( length ) ;
173
174
}
174
175
newClassName = `.${ classPrefix } ${ newClassName } ${ classSuffix } ` ;
175
-
176
+ validCssClassName = '.' + escapeClassName ( oldClassName . slice ( 1 ) ) ;
176
177
// If ClassName already exist replace with its value else generate new : the should have same name.
177
178
if ( jsonData . hasOwnProperty ( oldClassName ) ) {
178
179
selector = selector . replace (
179
- oldClassName ,
180
+ validCssClassName ,
180
181
jsonData [ oldClassName ]
181
182
) ;
182
183
} else {
183
- selector = selector . replace ( oldClassName , newClassName ) ;
184
+ selector = selector . replace ( validCssClassName , newClassName ) ;
184
185
jsonData [ oldClassName ] = newClassName ;
185
186
}
186
187
singleFileData [ oldClassName ] = newClassName ;
Original file line number Diff line number Diff line change @@ -208,13 +208,62 @@ function extractClassNames(obj) {
208
208
return classNames ;
209
209
}
210
210
211
+ function escapeClassName ( className ) {
212
+ // CSS escapes for some special characters
213
+ const escapes = {
214
+ '!' : '\\!' ,
215
+ '"' : '\\"' ,
216
+ '#' : '\\#' ,
217
+ '$' : '\\$' ,
218
+ '%' : '\\%' ,
219
+ '&' : '\\&' ,
220
+ '\'' : '\\\'' ,
221
+ '(' : '\\(' ,
222
+ ')' : '\\)' ,
223
+ '*' : '\\*' ,
224
+ '+' : '\\+' ,
225
+ ',' : '\\,' ,
226
+ '.' : '\\.' ,
227
+ '/' : '\\/' ,
228
+ ':' : '\\:' ,
229
+ ';' : '\\;' ,
230
+ '<' : '\\<' ,
231
+ '=' : '\\=' ,
232
+ '>' : '\\>' ,
233
+ '?' : '\\?' ,
234
+ '@' : '\\@' ,
235
+ '[' : '\\[' ,
236
+ '\\' : '\\\\' ,
237
+ ']' : '\\]' ,
238
+ '^' : '\\^' ,
239
+ '`' : '\\`' ,
240
+ '{' : '\\{' ,
241
+ '|' : '\\|' ,
242
+ '}' : '\\}' ,
243
+ '~' : '\\~' ,
244
+ ' ' : '\\ ' ,
245
+ } ;
246
+
247
+ // Special handling for class names starting with a digit
248
+ if ( / ^ \d / . test ( className ) ) {
249
+ // Convert the first digit to its hexadecimal escape code
250
+ const firstCharCode = className . charCodeAt ( 0 ) . toString ( 16 ) ;
251
+ const rest = className . slice ( 1 ) ;
252
+
253
+ // Use the hexadecimal escape for the first character, followed by the rest of the class name
254
+ // Note: A trailing space is added after the escape sequence to ensure separation
255
+ return `\\${ firstCharCode } ${ rest . split ( '' ) . map ( char => escapes [ char ] || char ) . join ( '' ) } ` ;
256
+ }
257
+ // Replace each special character with its escaped version for the rest of the class name
258
+ return className . split ( '' ) . map ( char => escapes [ char ] || char ) . join ( '' ) ;
259
+ }
260
+
211
261
function getClassNames ( selectorStr ) {
212
262
const parse = createParser ( { syntax : 'progressive' } ) ;
213
263
const ast = parse ( selectorStr ) ;
214
264
return extractClassNames ( ast ) ;
215
265
}
216
266
217
-
218
267
function getIdNames ( selectorStr ) {
219
268
let ids = selectorStr . replace ( ".#" , " " ) . replace ( "." , " " ) . trim ( ) . split ( " " ) ;
220
269
ids = ids . filter ( ( id ) => id . charAt ( 0 ) == "#" ) ;
@@ -294,4 +343,5 @@ module.exports = {
294
343
logger,
295
344
getRelativePath,
296
345
isFileOrInDirectory,
346
+ escapeClassName,
297
347
} ;
You can’t perform that action at this time.
0 commit comments