Skip to content

Commit

Permalink
fix: Remove blank SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Sep 14, 2023
1 parent 8b8b828 commit 16c12a4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/config/setup/modules/mermaidAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mermaid.initialize(config);

#### Defined in

[mermaidAPI.ts:679](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L679)
[mermaidAPI.ts:685](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L685)

## Functions

Expand Down
26 changes: 16 additions & 10 deletions packages/mermaid/src/mermaidAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,16 @@ const render = async function (
const enclosingDivID = 'd' + id;
const enclosingDivID_selector = '#' + enclosingDivID;

const removeTempElements = () => {
// -------------------------------------------------------------------------------
// Remove the temporary HTML element if appropriate
const tmpElementSelector = isSandboxed ? iFrameID_selector : enclosingDivID_selector;
const node = select(tmpElementSelector).node();
if (node && 'remove' in node) {
node.remove();
}
};

let root: any = select('body');

const isSandboxed = config.securityLevel === SECURITY_LVL_SANDBOX;
Expand Down Expand Up @@ -479,6 +489,7 @@ const render = async function (
diag = await getDiagramFromText(text);
} catch (error) {
if (config.suppressErrorRendering) {
removeTempElements();
throw error;
}
diag = new Diagram('error');
Expand Down Expand Up @@ -510,10 +521,11 @@ const render = async function (
try {
await diag.renderer.draw(text, id, version, diag);
} catch (e) {
if (!config.suppressErrorRendering) {
errorRenderer.draw(text, id, version);
if (config.suppressErrorRendering) {
removeTempElements();
throw e;
}
throw e;
errorRenderer.draw(text, id, version);
}

// This is the d3 node for the svg element
Expand Down Expand Up @@ -549,13 +561,7 @@ const render = async function (
throw parseEncounteredException;
}

// -------------------------------------------------------------------------------
// Remove the temporary HTML element if appropriate
const tmpElementSelector = isSandboxed ? iFrameID_selector : enclosingDivID_selector;
const node = select(tmpElementSelector).node();
if (node && 'remove' in node) {
node.remove();
}
removeTempElements();

return {
svg: svgCode,
Expand Down

0 comments on commit 16c12a4

Please sign in to comment.