Skip to content

Commit

Permalink
Merge branch '5237-unified-layout-common-renderer' of github.com:merm…
Browse files Browse the repository at this point in the history
…aid-js/mermaid into 5237-unified-layout-common-renderer
  • Loading branch information
knsv committed Jul 18, 2024
2 parents 63850fd + f30d370 commit 09c5c62
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
8 changes: 5 additions & 3 deletions cypress/integration/rendering/flowchart-v2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('Flowchart v2', () => {
const style = svg.attr('style');
expect(style).to.match(/^max-width: [\d.]+px;$/);
const maxWidthValue = parseFloat(style.match(/[\d.]+/g).join(''));
expect(maxWidthValue).to.be.within(290 * 0.95 - 1, 290 * 1.05);
expect(maxWidthValue).to.be.within(446 * 0.95 - 1, 446 * 1.05);
});
});
it('8: should render a flowchart when useMaxWidth is false', () => {
Expand All @@ -118,7 +118,7 @@ describe('Flowchart v2', () => {
const width = parseFloat(svg.attr('width'));
// use within because the absolute value can be slightly different depending on the environment ±5%
// expect(height).to.be.within(446 * 0.95, 446 * 1.05);
expect(width).to.be.within(290 * 0.95 - 1, 290 * 1.05);
expect(width).to.be.within(446 * 0.95 - 1, 446 * 1.05);
expect(svg).to.not.have.attr('style');
});
});
Expand Down Expand Up @@ -1047,7 +1047,9 @@ end
A --lb3--> TOP --lb4--> B
B1 --lb5--> B2
`,
{ flowchart: { subGraphTitleMargin: { top: 10, bottom: 5 } } }
{
flowchart: { subGraphTitleMargin: { top: 10, bottom: 5 } },
}
);
});
});
Expand Down
13 changes: 8 additions & 5 deletions cypress/integration/rendering/flowchart.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ describe('Graph', () => {
});
it('38: should render a flowchart when useMaxWidth is true (default)', () => {
renderGraph(
`graph TD
`flowchart TD
A[Christmas] -->|Get money| B(Go shopping)
B --> C{Let me think}
C -->|One| D[Laptop]
Expand All @@ -751,7 +751,7 @@ describe('Graph', () => {
const style = svg.attr('style');
expect(style).to.match(/^max-width: [\d.]+px;$/);
const maxWidthValue = parseFloat(style.match(/[\d.]+/g).join(''));
expect(maxWidthValue).to.be.within(300 * 0.9, 300 * 1.1);
expect(maxWidthValue).to.be.within(446 * 0.9, 446 * 1.1);
});
});
it('39: should render a flowchart when useMaxWidth is false', () => {
Expand All @@ -770,7 +770,7 @@ describe('Graph', () => {
const width = parseFloat(svg.attr('width'));
// use within because the absolute value can be slightly different depending on the environment ±10%
// expect(height).to.be.within(446 * 0.95, 446 * 1.05);
expect(width).to.be.within(300 * 0.9, 300 * 1.1);
expect(width).to.be.within(446 * 0.9, 446 * 1.1);
expect(svg).to.not.have.attr('style');
});
});
Expand Down Expand Up @@ -905,13 +905,16 @@ graph TD
it('67: should be able to style default node independently', () => {
imgSnapshotTest(
`
flowchart TD
flowchart TD
classDef default fill:#a34
hello --> default
style default stroke:#000,stroke-width:4px
`,
{ htmlLabels: true, flowchart: { htmlLabels: true }, securityLevel: 'loose' }
{
flowchart: { htmlLabels: true },
securityLevel: 'loose',
}
);
});
});
14 changes: 10 additions & 4 deletions packages/mermaid/src/rendering-util/createText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ export const createText = async (
const vertexNode = await addHtmlSpan(el, node, width, classes, addSvgBackground);
return vertexNode;
} else {
const structuredText = markdownToLines(text.replace('<br>', '<br/>'), config);
//sometimes the user might add br tags with 1 or more spaces in between, so we need to replace them with <br/>
const sanitizeBR = text.replace(/<br\s*\/?>/g, '<br/>');
const structuredText = markdownToLines(sanitizeBR.replace('<br>', '<br/>'), config);
const svgLabel = createFormattedText(
width,
el,
Expand All @@ -235,9 +237,13 @@ export const createText = async (
if (/stroke:/.exec(style)) {
style = style.replace('stroke:', 'lineColor:');
}
select(svgLabel)
.select('text')
.attr('style', style.replace(/color:/g, 'fill:'));

const nodeLabelTextStyle = style
.replace(/stroke:[^;]+;?/g, '')
.replace(/stroke-width:[^;]+;?/g, '')
.replace(/fill:[^;]+;?/g, '')
.replace(/color:/g, 'fill:');
select(svgLabel).attr('style', nodeLabelTextStyle);
// svgLabel.setAttribute('style', style);
} else {
//On style, assume `stroke`, `stroke-width` are used for edge path, so remove them
Expand Down
2 changes: 2 additions & 0 deletions packages/mermaid/src/rendering-util/handle-markdown-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export function markdownToHTML(markdown: string, { markdownAutoWrap }: MermaidCo
return `<em>${node.tokens?.map(output).join('')}</em>`;
} else if (node.type === 'paragraph') {
return `<p>${node.tokens?.map(output).join('')}</p>`;
} else if (node.type === 'space') {
return '';
} else if (node.type === 'html') {
return `${node.text}`;
}
Expand Down

0 comments on commit 09c5c62

Please sign in to comment.