Skip to content

Commit

Permalink
Merge pull request #3314 from mermaid-js/3313_error_handling_init
Browse files Browse the repository at this point in the history
Fix for unintended update to structure of thrown error from init
  • Loading branch information
knsv committed Aug 11, 2022
2 parents dce8957 + bf42721 commit 2cc88df
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 50 deletions.
44 changes: 9 additions & 35 deletions cypress/platform/knsv2.html
Expand Up @@ -41,56 +41,30 @@
<body>
<div>Security check</div>
<div class="flex">
<div id="diagram" class="mermaid"></div>
<div id="diagram" class="mermaid">
sequenceDiagram
Nothing:Valid;
</div>
<div id="res" class=""></div>
<script src="./mermaid.js"></script>
<script>
mermaid.parseError = function (err, hash) {
// console.error('Mermaid error: ', err);
};
mermaid.initialize({
theme: 'forest',
arrowMarkerAbsolute: true,
// themeCSS: '.edgePath .path {stroke: red;} .arrowheadPath {fill: red;}',
logLevel: 0,
state: {
defaultRenderer: 'dagre-wrapper',
},
flowchart: {
// defaultRenderer: 'dagre-wrapper',
nodeSpacing: 10,
curve: 'cardinal',
htmlLabels: true,
},
htmlLabels: false,
// gantt: { axisFormat: '%m/%d/%Y' },
sequence: { actorFontFamily: 'courier', actorMargin: 50, showSequenceNumbers: false },
// sequenceDiagram: { actorMargin: 300 } // deprecated
// fontFamily: '"times", sans-serif',
// fontFamily: 'courier',
fontSize: 18,
curve: 'basis',
securityLevel: 'strict',
startOnLoad: false,
// themeVariables: {relationLabelColor: 'red'}
});
function callback() {
alert('It worked');
}

var diagram = '%%{init: {"flowchart": {"htmlLabels": "false"}} }%%\n';
diagram += 'flowchart\n';
diagram += 'A["<ifra';
diagram += "me srcdoc='<scrip";
diagram += 't src=http://localhost:9000/exploit.js>';
diagram += '</scr';
diagram += 'ipt>\'></iframe>"]';

console.log(diagram);
// document.querySelector('#diagram').innerHTML = diagram;
mermaid.render('diagram', diagram, (res) => {
document.querySelector('#res').innerHTML = res;
});
try {
mermaid.initThrowsErrors(undefined, '#diagram');
} catch (err) {
console.log('Caught error:', err);
}
</script>
</body>
</html>
Expand Down
32 changes: 18 additions & 14 deletions src/mermaid.js
Expand Up @@ -34,7 +34,7 @@ const init = function () {
initThrowsErrors(...arguments);
} catch (e) {
log.warn('Syntax Error rendering');
log.warn(e);
log.warn(e.str);
if (this.parseError) {
this.parseError(e);
}
Expand Down Expand Up @@ -120,19 +120,23 @@ const initThrowsErrors = function () {
if (init) {
log.debug('Detected early reinit: ', init);
}

mermaidAPI.render(
id,
txt,
(svgCode, bindFunctions) => {
element.innerHTML = svgCode;
if (typeof callback !== 'undefined') {
callback(id);
}
if (bindFunctions) bindFunctions(element);
},
element
);
try {
mermaidAPI.render(
id,
txt,
(svgCode, bindFunctions) => {
element.innerHTML = svgCode;
if (typeof callback !== 'undefined') {
callback(id);
}
if (bindFunctions) bindFunctions(element);
},
element
);
} catch (error) {
log.warn('Catching Error (bootstrap)');
throw { error, message: error.str };
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/mermaidAPI.js
Expand Up @@ -250,7 +250,7 @@ const render = function (id, _txt, cb, container) {

txt = encodeEntities(txt);

// Imortant that we do not create the diagram until after the directives have been included
// Important that we do not create the diagram until after the directives have been included
const diag = new Diagram(txt);
// Get the tmp element containing the the svg
const element = root.select('#d' + id).node();
Expand Down

0 comments on commit 2cc88df

Please sign in to comment.