mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-15 05:19:58 +02:00
#2560 Tetsing that the added css have balanced brackets
This commit is contained in:
@@ -1,5 +1,30 @@
|
|||||||
import { imgSnapshotTest } from '../../helpers/util.js';
|
import { imgSnapshotTest } from '../../helpers/util.js';
|
||||||
|
|
||||||
|
describe('themeCSS balancing, it', () => {
|
||||||
|
it('should not allow unbalanced CSS definitions', () => {
|
||||||
|
imgSnapshotTest(
|
||||||
|
`
|
||||||
|
%%{init: { 'themeCSS': '} * { background: red }' } }%%
|
||||||
|
flowchart TD
|
||||||
|
a --> b
|
||||||
|
`,
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
cy.get('svg');
|
||||||
|
});
|
||||||
|
it('should not allow unbalanced CSS definitions 2', () => {
|
||||||
|
imgSnapshotTest(
|
||||||
|
`
|
||||||
|
%%{init: { 'themeCSS': '\u007D * { background: red }' } }%%
|
||||||
|
flowchart TD
|
||||||
|
a2 --> b2
|
||||||
|
`,
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
cy.get('svg');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('Pie Chart', () => {
|
describe('Pie Chart', () => {
|
||||||
// beforeEach(()=>{
|
// beforeEach(()=>{
|
||||||
// cy.clock((new Date('2014-06-09')).getTime());
|
// cy.clock((new Date('2014-06-09')).getTime());
|
||||||
|
28
cypress/platform/css1.html
Normal file
28
cypress/platform/css1.html
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<title>Mermaid Quick Test Page</title>
|
||||||
|
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=">
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: 'trebuchet ms', verdana, arial;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="mermaid">
|
||||||
|
%%{init: { 'themeCSS': '} * { background: lightblue }' } }%%
|
||||||
|
flowchart TD
|
||||||
|
a --> b
|
||||||
|
</div>
|
||||||
|
<script src="./mermaid.js"></script>
|
||||||
|
<script>
|
||||||
|
function showFullFirstSquad(elemName) {
|
||||||
|
console.log('show ' + elemName);
|
||||||
|
}
|
||||||
|
mermaid.initialize({ startOnLoad: true, securityLevel: 'loose', logLevel: 0 });
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
@@ -60,7 +60,7 @@ import { attachFunctions } from './interactionDb';
|
|||||||
import { log, setLogLevel } from './logger';
|
import { log, setLogLevel } from './logger';
|
||||||
import getStyles from './styles';
|
import getStyles from './styles';
|
||||||
import theme from './themes';
|
import theme from './themes';
|
||||||
import utils, { directiveSanitizer, assignWithDepth } from './utils';
|
import utils, { directiveSanitizer, assignWithDepth, sanitizeCss } from './utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param text
|
* @param text
|
||||||
@@ -223,6 +223,7 @@ const render = function (id, _txt, cb, container) {
|
|||||||
let txt = _txt;
|
let txt = _txt;
|
||||||
const graphInit = utils.detectInit(txt);
|
const graphInit = utils.detectInit(txt);
|
||||||
if (graphInit) {
|
if (graphInit) {
|
||||||
|
directiveSanitizer(graphInit);
|
||||||
configApi.addDirective(graphInit);
|
configApi.addDirective(graphInit);
|
||||||
}
|
}
|
||||||
let cnf = configApi.getConfig();
|
let cnf = configApi.getConfig();
|
||||||
@@ -533,6 +534,9 @@ const handleDirective = function (p, directive, type) {
|
|||||||
p.setWrap(directive.type === 'wrap');
|
p.setWrap(directive.type === 'wrap');
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'themeCss':
|
||||||
|
log.warn('themeCss encountered');
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
log.warn(
|
log.warn(
|
||||||
`Unhandled directive: source: '%%{${directive.type}: ${JSON.stringify(
|
`Unhandled directive: source: '%%{${directive.type}: ${JSON.stringify(
|
||||||
|
16
src/utils.js
16
src/utils.js
@@ -974,6 +974,11 @@ export const directiveSanitizer = (args) => {
|
|||||||
log.debug('sanitize deleting constr option', key);
|
log.debug('sanitize deleting constr option', key);
|
||||||
delete args[key];
|
delete args[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (key.indexOf('themeCSS') >= 0) {
|
||||||
|
log.debug('sanitizing themeCss option');
|
||||||
|
args[key] = sanitizeCss(args[key]);
|
||||||
|
}
|
||||||
if (configKeys.indexOf(key) < 0) {
|
if (configKeys.indexOf(key) < 0) {
|
||||||
log.debug('sanitize deleting option', key);
|
log.debug('sanitize deleting option', key);
|
||||||
delete args[key];
|
delete args[key];
|
||||||
@@ -987,6 +992,16 @@ export const directiveSanitizer = (args) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
export const sanitizeCss = (str) => {
|
||||||
|
const stringsearch = 'o';
|
||||||
|
const startCnt = (str.match(/\{/g) || []).length;
|
||||||
|
const endCnt = (str.match(/\}/g) || []).length;
|
||||||
|
if (startCnt !== endCnt) {
|
||||||
|
return '{ /* ERROR: Unbalanced CSS */ }';
|
||||||
|
}
|
||||||
|
// Todo add more checks here
|
||||||
|
return str;
|
||||||
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
assignWithDepth,
|
assignWithDepth,
|
||||||
@@ -1013,4 +1028,5 @@ export default {
|
|||||||
entityDecode,
|
entityDecode,
|
||||||
initIdGeneratior,
|
initIdGeneratior,
|
||||||
directiveSanitizer,
|
directiveSanitizer,
|
||||||
|
sanitizeCss,
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user