Skip to content

Commit

Permalink
Merge pull request #4436 from slab/zh-mermaid-empty-text
Browse files Browse the repository at this point in the history
Fix exceptions for empty lines
  • Loading branch information
sidharthv96 committed Jun 5, 2023
2 parents 6115f69 + 2a6603b commit f20ff4d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
10 changes: 10 additions & 0 deletions cypress/integration/rendering/sequencediagram.spec.js
Expand Up @@ -88,6 +88,16 @@ context('Sequence diagram', () => {
{}
);
});
it('should handle empty lines', () => {
imgSnapshotTest(
`
sequenceDiagram
Alice->>John: Hello John<br/>
John-->>Alice: Great<br/><br/>day!
`,
{}
);
});
it('should handle line breaks and wrap annotations', () => {
imgSnapshotTest(
`
Expand Down
7 changes: 4 additions & 3 deletions packages/mermaid/src/diagrams/sequence/svgDraw.js
@@ -1,7 +1,7 @@
import common from '../common/common.js';
import * as svgDrawCommon from '../common/svgDrawCommon';
import { addFunction } from '../../interactionDb.js';
import { parseFontSize } from '../../utils.js';
import { ZERO_WIDTH_SPACE, parseFontSize } from '../../utils.js';
import { sanitizeUrl } from '@braintree/sanitize-url';

export const drawRect = function (elem, rectData) {
Expand Down Expand Up @@ -224,15 +224,16 @@ export const drawText = function (elem, textData) {
textElem.attr('dy', dy);
}

const text = line || ZERO_WIDTH_SPACE;
if (textData.tspan) {
const span = textElem.append('tspan');
span.attr('x', textData.x);
if (textData.fill !== undefined) {
span.attr('fill', textData.fill);
}
span.text(line);
span.text(text);
} else {
textElem.text(line);
textElem.text(text);
}
if (
textData.valign !== undefined &&
Expand Down
4 changes: 3 additions & 1 deletion packages/mermaid/src/utils.ts
Expand Up @@ -32,6 +32,8 @@ import assignWithDepth from './assignWithDepth.js';
import { MermaidConfig } from './config.type.js';
import memoize from 'lodash-es/memoize.js';

export const ZERO_WIDTH_SPACE = '\u200b';

// Effectively an enum of the supported curve types, accessible by name
const d3CurveTypes = {
curveBasis: curveBasis,
Expand Down Expand Up @@ -765,7 +767,7 @@ export const calculateTextDimensions: (
const dim = { width: 0, height: 0, lineHeight: 0 };
for (const line of lines) {
const textObj = getTextObj();
textObj.text = line;
textObj.text = line || ZERO_WIDTH_SPACE;
const textElem = drawSimpleText(g, textObj)
.style('font-size', _fontSizePx)
.style('font-weight', fontWeight)
Expand Down

0 comments on commit f20ff4d

Please sign in to comment.