@@ -53,16 +53,26 @@ import assignWithDepth from './assignWithDepth.js';
5353vi . mock ( './styles.js' , ( ) => {
5454 return {
5555 addStylesForDiagram : vi . fn ( ) ,
56- default : vi . fn ( ) . mockReturnValue ( ' .userStyle { font-weight:bold; }' ) ,
56+ default : vi . fn ( ) . mockImplementation (
57+ ( _type , userStyles , _options ) => `
58+ & .edge-pattern-dashed{
59+ stroke-dasharray: 3;
60+ }
61+
62+ ${ userStyles }
63+ `
64+ ) ,
5765 } ;
5866} ) ;
5967import getStyles from './styles.js' ;
6068
61- vi . mock ( 'stylis' , ( ) => {
69+ vi . mock ( 'stylis' , async ( importOriginal ) => {
70+ // eslint-disable-next-line @typescript-eslint/consistent-type-imports
71+ const original : typeof import ( 'stylis' ) = await importOriginal ( ) ;
6272 return {
63- stringify : vi . fn ( ) ,
64- compile : vi . fn ( ) ,
65- serialize : vi . fn ( ) . mockReturnValue ( 'stylis serialized css' ) ,
73+ stringify : vi . fn ( ) . mockImplementation ( original . stringify ) ,
74+ compile : vi . fn ( ) . mockImplementation ( original . compile ) ,
75+ serialize : vi . fn ( ) . mockImplementation ( original . serialize ) ,
6676 } ;
6777} ) ;
6878import { compile , serialize } from 'stylis' ;
@@ -455,7 +465,29 @@ describe('mermaidAPI', () => {
455465 const result = createUserStyles ( mockConfig , 'someDiagram' , { } , 'someId' ) ;
456466 expect ( compile ) . toHaveBeenCalled ( ) ;
457467 expect ( serialize ) . toHaveBeenCalled ( ) ;
458- expect ( result ) . toEqual ( 'stylis serialized css' ) ;
468+ expect ( result ) . toEqual ( 'someId .edge-pattern-dashed{stroke-dasharray:3;}' ) ;
469+ } ) ;
470+
471+ it ( 'should sanitize CSS to avoid unbalanced braces' , ( ) => {
472+ const result = createUserStyles (
473+ mockConfig ,
474+ 'someDiagram' ,
475+ Object . fromEntries (
476+ Object . entries ( {
477+ classDef1 : {
478+ styles : [ '}*{ background-image: url("https://example.test")}' ] ,
479+ textStyles : [ ] ,
480+ } ,
481+ classDef2 : {
482+ styles : [ 'color: purple;' ] ,
483+ } ,
484+ } ) . map ( ( [ id , value ] ) => [ id , { ...value , id } ] )
485+ ) ,
486+ 'someId'
487+ ) ;
488+ expect ( result ) . toEqual (
489+ 'someId .edge-pattern-dashed{stroke-dasharray:3;}someId .classDef2>*{color:purple;}someId .classDef2 span{color:purple;}'
490+ ) ;
459491 } ) ;
460492 } ) ;
461493
0 commit comments