@@ -28,8 +28,8 @@ describe('transform', async () => {
2828 safelist : [ safelistClassToSkip ] ,
2929 } )
3030
31- async function transform ( content : string , { combine = true , format = true } = { } ) {
32- const transformed = ( await transformClasses ( { content, filename : 'Foo.svelte' , uno, options : { combine } } ) ) ?. code
31+ async function transform ( content : string , { combine = true , format = true , hashSafelistClasses } : { combine ?: boolean , format ?: boolean , hashSafelistClasses ?: boolean } = { } ) {
32+ const transformed = ( await transformClasses ( { content, filename : 'Foo.svelte' , uno, options : { combine, hashSafelistClasses } } ) ) ?. code
3333 if ( transformed && format ) {
3434 return prettier ( transformed , {
3535 parser : 'svelte' ,
@@ -403,3 +403,56 @@ describe('transform', async () => {
403403 ` )
404404 } )
405405} )
406+
407+ describe ( 'safelist shortcut handling' , async ( ) => {
408+ const uno = await createGenerator ( {
409+ presets : [ presetWind3 ( ) ] ,
410+ shortcuts : [
411+ { btn : 'px-4 py-2 font-bold' } ,
412+ ] ,
413+ safelist : [ 'btn' , 'mr-7' ] ,
414+ } )
415+
416+ async function transform ( content : string , { combine = true , hashSafelistClasses } : { combine ?: boolean , hashSafelistClasses ?: boolean } = { } ) {
417+ const result = await transformClasses ( { content, filename : 'Foo.svelte' , uno, options : { combine, hashSafelistClasses } } )
418+ return result ?. code
419+ }
420+
421+ it ( 'does not hash shortcut classes in safelist by default' , async ( ) => {
422+ const code = '<div class="btn mb-1" />'
423+ const output = await transform ( code )
424+ // btn is passed through as-is, only mb-1 gets hashed
425+ expect ( output ) . toContain ( 'btn' )
426+ expect ( output ) . toMatch ( / c l a s s = " [ ^ ] + b t n " / )
427+ } )
428+
429+ it ( 'does not hash utility classes in safelist by default' , async ( ) => {
430+ const code = '<div class="bg-red-500 mr-7" />'
431+ const output = await transform ( code )
432+ expect ( output ) . toContain ( 'mr-7' )
433+ } )
434+
435+ it ( 'does not hash shortcut classes in safelist in dev mode (combine: false)' , async ( ) => {
436+ const code = '<div class="btn mb-1" />'
437+ const output = await transform ( code , { combine : false } )
438+ // btn is passed through as-is, mb-1 gets its own hashed class
439+ expect ( output ) . toContain ( 'btn' )
440+ expect ( output ) . toMatch ( / _ m b - 1 _ \w + / )
441+ expect ( output ) . not . toMatch ( / _ b t n _ \w + / )
442+ } )
443+
444+ it ( 'hashes shortcut classes in safelist when hashSafelistClasses is true' , async ( ) => {
445+ const code = '<div class="btn mb-1" />'
446+ const output = await transform ( code , { hashSafelistClasses : true } )
447+ // btn should be hashed along with mb-1
448+ expect ( output ) . not . toMatch ( / \b b t n \b / )
449+ } )
450+
451+ it ( 'hashes shortcut classes in safelist in dev mode when hashSafelistClasses is true' , async ( ) => {
452+ const code = '<div class="btn mb-1" />'
453+ const output = await transform ( code , { combine : false , hashSafelistClasses : true } )
454+ // btn should get its own hashed class
455+ expect ( output ) . toMatch ( / _ b t n _ \w + / )
456+ expect ( output ) . not . toMatch ( / \b b t n \b / )
457+ } )
458+ } )
0 commit comments