• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mermaid-js / mermaid / 5071601472

pending completion
5071601472

push

github

Knut Sveidqvist
Merge branch 'release/10.2.0'

1633 of 2064 branches covered (79.12%)

Branch coverage included in aggregate %.

2701 of 2701 new or added lines in 128 files covered. (100.0%)

19402 of 34929 relevant lines covered (55.55%)

418.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.26
/packages/mermaid/src/diagrams/flowchart/flowRenderer-v2.js
1
import * as graphlib from 'dagre-d3-es/src/graphlib/index.js';
1✔
2
import { select, curveLinear, selectAll } from 'd3';
1✔
3

1✔
4
import flowDb from './flowDb.js';
1✔
5
import { getConfig } from '../../config.js';
1✔
6
import utils from '../../utils.js';
1✔
7

1✔
8
import { render } from '../../dagre-wrapper/index.js';
1✔
9
import { addHtmlLabel } from 'dagre-d3-es/src/dagre-js/label/add-html-label.js';
1✔
10
import { log } from '../../logger.js';
1✔
11
import common, { evaluate } from '../common/common.js';
1✔
12
import { interpolateToCurve, getStylesFromArray } from '../../utils.js';
1✔
13
import { setupGraphViewbox } from '../../setupGraphViewbox.js';
1✔
14

1✔
15
const conf = {};
1✔
16
export const setConf = function (cnf) {
1✔
17
  const keys = Object.keys(cnf);
4✔
18
  for (const key of keys) {
4✔
19
    conf[key] = cnf[key];
44✔
20
  }
44✔
21
};
4✔
22

1✔
23
/**
1✔
24
 * Function that adds the vertices found during parsing to the graph to be rendered.
1✔
25
 *
1✔
26
 * @param vert Object containing the vertices.
1✔
27
 * @param g The graph that is to be drawn.
1✔
28
 * @param svgId
1✔
29
 * @param root
1✔
30
 * @param doc
1✔
31
 * @param diagObj
1✔
32
 */
1✔
33
export const addVertices = function (vert, g, svgId, root, doc, diagObj) {
1✔
34
  const svg = root.select(`[id="${svgId}"]`);
×
35
  const keys = Object.keys(vert);
×
36

×
37
  // Iterate through each item in the vertex object (containing all the vertices found) in the graph definition
×
38
  keys.forEach(function (id) {
×
39
    const vertex = vert[id];
×
40

×
41
    /**
×
42
     * Variable for storing the classes for the vertex
×
43
     *
×
44
     * @type {string}
×
45
     */
×
46
    let classStr = 'default';
×
47
    if (vertex.classes.length > 0) {
×
48
      classStr = vertex.classes.join(' ');
×
49
    }
×
50
    classStr = classStr + ' flowchart-label';
×
51
    const styles = getStylesFromArray(vertex.styles);
×
52

×
53
    // Use vertex id as text in the box if no text is provided by the graph definition
×
54
    let vertexText = vertex.text !== undefined ? vertex.text : vertex.id;
×
55

×
56
    // We create a SVG label, either by delegating to addHtmlLabel or manually
×
57
    let vertexNode;
×
58
    log.info('vertex', vertex, vertex.labelType);
×
59
    if (vertex.labelType === 'markdown') {
×
60
      log.info('vertex', vertex, vertex.labelType);
×
61
    } else {
×
62
      if (evaluate(getConfig().flowchart.htmlLabels)) {
×
63
        // TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?
×
64
        const node = {
×
65
          label: vertexText.replace(
×
66
            /fa[blrs]?:fa-[\w-]+/g,
×
67
            (s) => `<i class='${s.replace(':', ' ')}'></i>`
×
68
          ),
×
69
        };
×
70
        vertexNode = addHtmlLabel(svg, node).node();
×
71
        vertexNode.parentNode.removeChild(vertexNode);
×
72
      } else {
×
73
        const svgLabel = doc.createElementNS('http://www.w3.org/2000/svg', 'text');
×
74
        svgLabel.setAttribute('style', styles.labelStyle.replace('color:', 'fill:'));
×
75

×
76
        const rows = vertexText.split(common.lineBreakRegex);
×
77

×
78
        for (const row of rows) {
×
79
          const tspan = doc.createElementNS('http://www.w3.org/2000/svg', 'tspan');
×
80
          tspan.setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:space', 'preserve');
×
81
          tspan.setAttribute('dy', '1em');
×
82
          tspan.setAttribute('x', '1');
×
83
          tspan.textContent = row;
×
84
          svgLabel.appendChild(tspan);
×
85
        }
×
86
        vertexNode = svgLabel;
×
87
      }
×
88
    }
×
89

×
90
    let radious = 0;
×
91
    let _shape = '';
×
92
    // Set the shape based parameters
×
93
    switch (vertex.type) {
×
94
      case 'round':
×
95
        radious = 5;
×
96
        _shape = 'rect';
×
97
        break;
×
98
      case 'square':
×
99
        _shape = 'rect';
×
100
        break;
×
101
      case 'diamond':
×
102
        _shape = 'question';
×
103
        break;
×
104
      case 'hexagon':
×
105
        _shape = 'hexagon';
×
106
        break;
×
107
      case 'odd':
×
108
        _shape = 'rect_left_inv_arrow';
×
109
        break;
×
110
      case 'lean_right':
×
111
        _shape = 'lean_right';
×
112
        break;
×
113
      case 'lean_left':
×
114
        _shape = 'lean_left';
×
115
        break;
×
116
      case 'trapezoid':
×
117
        _shape = 'trapezoid';
×
118
        break;
×
119
      case 'inv_trapezoid':
×
120
        _shape = 'inv_trapezoid';
×
121
        break;
×
122
      case 'odd_right':
×
123
        _shape = 'rect_left_inv_arrow';
×
124
        break;
×
125
      case 'circle':
×
126
        _shape = 'circle';
×
127
        break;
×
128
      case 'ellipse':
×
129
        _shape = 'ellipse';
×
130
        break;
×
131
      case 'stadium':
×
132
        _shape = 'stadium';
×
133
        break;
×
134
      case 'subroutine':
×
135
        _shape = 'subroutine';
×
136
        break;
×
137
      case 'cylinder':
×
138
        _shape = 'cylinder';
×
139
        break;
×
140
      case 'group':
×
141
        _shape = 'rect';
×
142
        break;
×
143
      case 'doublecircle':
×
144
        _shape = 'doublecircle';
×
145
        break;
×
146
      default:
×
147
        _shape = 'rect';
×
148
    }
×
149
    // Add the node
×
150
    g.setNode(vertex.id, {
×
151
      labelStyle: styles.labelStyle,
×
152
      shape: _shape,
×
153
      labelText: vertexText,
×
154
      labelType: vertex.labelType,
×
155
      rx: radious,
×
156
      ry: radious,
×
157
      class: classStr,
×
158
      style: styles.style,
×
159
      id: vertex.id,
×
160
      link: vertex.link,
×
161
      linkTarget: vertex.linkTarget,
×
162
      tooltip: diagObj.db.getTooltip(vertex.id) || '',
×
163
      domId: diagObj.db.lookUpDomId(vertex.id),
×
164
      haveCallback: vertex.haveCallback,
×
165
      width: vertex.type === 'group' ? 500 : undefined,
×
166
      dir: vertex.dir,
×
167
      type: vertex.type,
×
168
      props: vertex.props,
×
169
      padding: getConfig().flowchart.padding,
×
170
    });
×
171

×
172
    log.info('setNode', {
×
173
      labelStyle: styles.labelStyle,
×
174
      labelType: vertex.labelType,
×
175
      shape: _shape,
×
176
      labelText: vertexText,
×
177
      rx: radious,
×
178
      ry: radious,
×
179
      class: classStr,
×
180
      style: styles.style,
×
181
      id: vertex.id,
×
182
      domId: diagObj.db.lookUpDomId(vertex.id),
×
183
      width: vertex.type === 'group' ? 500 : undefined,
×
184
      type: vertex.type,
×
185
      dir: vertex.dir,
×
186
      props: vertex.props,
×
187
      padding: getConfig().flowchart.padding,
×
188
    });
×
189
  });
×
190
};
×
191

1✔
192
/**
1✔
193
 * Add edges to graph based on parsed graph definition
1✔
194
 *
1✔
195
 * @param {object} edges The edges to add to the graph
1✔
196
 * @param {object} g The graph object
1✔
197
 * @param diagObj
1✔
198
 */
1✔
199
export const addEdges = function (edges, g, diagObj) {
1✔
200
  log.info('abc78 edges = ', edges);
×
201
  let cnt = 0;
×
202
  let linkIdCnt = {};
×
203

×
204
  let defaultStyle;
×
205
  let defaultLabelStyle;
×
206

×
207
  if (edges.defaultStyle !== undefined) {
×
208
    const defaultStyles = getStylesFromArray(edges.defaultStyle);
×
209
    defaultStyle = defaultStyles.style;
×
210
    defaultLabelStyle = defaultStyles.labelStyle;
×
211
  }
×
212

×
213
  edges.forEach(function (edge) {
×
214
    cnt++;
×
215

×
216
    // Identify Link
×
217
    const linkIdBase = 'L-' + edge.start + '-' + edge.end;
×
218
    // count the links from+to the same node to give unique id
×
219
    if (linkIdCnt[linkIdBase] === undefined) {
×
220
      linkIdCnt[linkIdBase] = 0;
×
221
      log.info('abc78 new entry', linkIdBase, linkIdCnt[linkIdBase]);
×
222
    } else {
×
223
      linkIdCnt[linkIdBase]++;
×
224
      log.info('abc78 new entry', linkIdBase, linkIdCnt[linkIdBase]);
×
225
    }
×
226
    let linkId = linkIdBase + '-' + linkIdCnt[linkIdBase];
×
227
    log.info('abc78 new link id to be used is', linkIdBase, linkId, linkIdCnt[linkIdBase]);
×
228
    const linkNameStart = 'LS-' + edge.start;
×
229
    const linkNameEnd = 'LE-' + edge.end;
×
230

×
231
    const edgeData = { style: '', labelStyle: '' };
×
232
    edgeData.minlen = edge.length || 1;
×
233
    //edgeData.id = 'id' + cnt;
×
234

×
235
    // Set link type for rendering
×
236
    if (edge.type === 'arrow_open') {
×
237
      edgeData.arrowhead = 'none';
×
238
    } else {
×
239
      edgeData.arrowhead = 'normal';
×
240
    }
×
241

×
242
    // Check of arrow types, placed here in order not to break old rendering
×
243
    edgeData.arrowTypeStart = 'arrow_open';
×
244
    edgeData.arrowTypeEnd = 'arrow_open';
×
245

×
246
    /* eslint-disable no-fallthrough */
×
247
    switch (edge.type) {
×
248
      case 'double_arrow_cross':
×
249
        edgeData.arrowTypeStart = 'arrow_cross';
×
250
      case 'arrow_cross':
×
251
        edgeData.arrowTypeEnd = 'arrow_cross';
×
252
        break;
×
253
      case 'double_arrow_point':
×
254
        edgeData.arrowTypeStart = 'arrow_point';
×
255
      case 'arrow_point':
×
256
        edgeData.arrowTypeEnd = 'arrow_point';
×
257
        break;
×
258
      case 'double_arrow_circle':
×
259
        edgeData.arrowTypeStart = 'arrow_circle';
×
260
      case 'arrow_circle':
×
261
        edgeData.arrowTypeEnd = 'arrow_circle';
×
262
        break;
×
263
    }
×
264

×
265
    let style = '';
×
266
    let labelStyle = '';
×
267

×
268
    switch (edge.stroke) {
×
269
      case 'normal':
×
270
        style = 'fill:none;';
×
271
        if (defaultStyle !== undefined) {
×
272
          style = defaultStyle;
×
273
        }
×
274
        if (defaultLabelStyle !== undefined) {
×
275
          labelStyle = defaultLabelStyle;
×
276
        }
×
277
        edgeData.thickness = 'normal';
×
278
        edgeData.pattern = 'solid';
×
279
        break;
×
280
      case 'dotted':
×
281
        edgeData.thickness = 'normal';
×
282
        edgeData.pattern = 'dotted';
×
283
        edgeData.style = 'fill:none;stroke-width:2px;stroke-dasharray:3;';
×
284
        break;
×
285
      case 'thick':
×
286
        edgeData.thickness = 'thick';
×
287
        edgeData.pattern = 'solid';
×
288
        edgeData.style = 'stroke-width: 3.5px;fill:none;';
×
289
        break;
×
290
      case 'invisible':
×
291
        edgeData.thickness = 'invisible';
×
292
        edgeData.pattern = 'solid';
×
293
        edgeData.style = 'stroke-width: 0;fill:none;';
×
294
        break;
×
295
    }
×
296
    if (edge.style !== undefined) {
×
297
      const styles = getStylesFromArray(edge.style);
×
298
      style = styles.style;
×
299
      labelStyle = styles.labelStyle;
×
300
    }
×
301

×
302
    edgeData.style = edgeData.style += style;
×
303
    edgeData.labelStyle = edgeData.labelStyle += labelStyle;
×
304

×
305
    if (edge.interpolate !== undefined) {
×
306
      edgeData.curve = interpolateToCurve(edge.interpolate, curveLinear);
×
307
    } else if (edges.defaultInterpolate !== undefined) {
×
308
      edgeData.curve = interpolateToCurve(edges.defaultInterpolate, curveLinear);
×
309
    } else {
×
310
      edgeData.curve = interpolateToCurve(conf.curve, curveLinear);
×
311
    }
×
312

×
313
    if (edge.text === undefined) {
×
314
      if (edge.style !== undefined) {
×
315
        edgeData.arrowheadStyle = 'fill: #333';
×
316
      }
×
317
    } else {
×
318
      edgeData.arrowheadStyle = 'fill: #333';
×
319
      edgeData.labelpos = 'c';
×
320
    }
×
321

×
322
    edgeData.labelType = edge.labelType;
×
323
    edgeData.label = edge.text.replace(common.lineBreakRegex, '\n');
×
324

×
325
    if (edge.style === undefined) {
×
326
      edgeData.style = edgeData.style || 'stroke: #333; stroke-width: 1.5px;fill:none;';
×
327
    }
×
328

×
329
    edgeData.labelStyle = edgeData.labelStyle.replace('color:', 'fill:');
×
330

×
331
    edgeData.id = linkId;
×
332
    edgeData.classes = 'flowchart-link ' + linkNameStart + ' ' + linkNameEnd;
×
333

×
334
    // Add the edge to the graph
×
335
    g.setEdge(edge.start, edge.end, edgeData, cnt);
×
336
  });
×
337
};
×
338

1✔
339
/**
1✔
340
 * Returns the all the styles from classDef statements in the graph definition.
1✔
341
 *
1✔
342
 * @param text
1✔
343
 * @param diagObj
1✔
344
 * @returns {object} ClassDef styles
1✔
345
 */
1✔
346
export const getClasses = function (text, diagObj) {
1✔
347
  log.info('Extracting classes');
×
348
  diagObj.db.clear();
×
349
  try {
×
350
    // Parse the graph definition
×
351
    diagObj.parse(text);
×
352
    return diagObj.db.getClasses();
×
353
  } catch (e) {
×
354
    return;
×
355
  }
×
356
};
×
357

1✔
358
/**
1✔
359
 * Draws a flowchart in the tag with id: id based on the graph definition in text.
1✔
360
 *
1✔
361
 * @param text
1✔
362
 * @param id
1✔
363
 */
1✔
364

1✔
365
export const draw = async function (text, id, _version, diagObj) {
1✔
366
  log.info('Drawing flowchart');
×
367
  diagObj.db.clear();
×
368
  flowDb.setGen('gen-2');
×
369
  // Parse the graph definition
×
370
  diagObj.parser.parse(text);
×
371

×
372
  // Fetch the default direction, use TD if none was found
×
373
  let dir = diagObj.db.getDirection();
×
374
  if (dir === undefined) {
×
375
    dir = 'TD';
×
376
  }
×
377

×
378
  const { securityLevel, flowchart: conf } = getConfig();
×
379
  const nodeSpacing = conf.nodeSpacing || 50;
×
380
  const rankSpacing = conf.rankSpacing || 50;
×
381

×
382
  // Handle root and document for when rendering in sandbox mode
×
383
  let sandboxElement;
×
384
  if (securityLevel === 'sandbox') {
×
385
    sandboxElement = select('#i' + id);
×
386
  }
×
387
  const root =
×
388
    securityLevel === 'sandbox'
×
389
      ? select(sandboxElement.nodes()[0].contentDocument.body)
×
390
      : select('body');
×
391
  const doc = securityLevel === 'sandbox' ? sandboxElement.nodes()[0].contentDocument : document;
×
392

×
393
  // Create the input mermaid.graph
×
394
  const g = new graphlib.Graph({
×
395
    multigraph: true,
×
396
    compound: true,
×
397
  })
×
398
    .setGraph({
×
399
      rankdir: dir,
×
400
      nodesep: nodeSpacing,
×
401
      ranksep: rankSpacing,
×
402
      marginx: 0,
×
403
      marginy: 0,
×
404
    })
×
405
    .setDefaultEdgeLabel(function () {
×
406
      return {};
×
407
    });
×
408

×
409
  let subG;
×
410
  const subGraphs = diagObj.db.getSubGraphs();
×
411
  log.info('Subgraphs - ', subGraphs);
×
412
  for (let i = subGraphs.length - 1; i >= 0; i--) {
×
413
    subG = subGraphs[i];
×
414
    log.info('Subgraph - ', subG);
×
415
    diagObj.db.addVertex(
×
416
      subG.id,
×
417
      { text: subG.title, type: subG.labelType },
×
418
      'group',
×
419
      undefined,
×
420
      subG.classes,
×
421
      subG.dir
×
422
    );
×
423
  }
×
424

×
425
  // Fetch the vertices/nodes and edges/links from the parsed graph definition
×
426
  const vert = diagObj.db.getVertices();
×
427

×
428
  const edges = diagObj.db.getEdges();
×
429

×
430
  log.info('Edges', edges);
×
431
  let i = 0;
×
432
  for (i = subGraphs.length - 1; i >= 0; i--) {
×
433
    // for (let i = 0; i < subGraphs.length; i++) {
×
434
    subG = subGraphs[i];
×
435

×
436
    selectAll('cluster').append('text');
×
437

×
438
    for (let j = 0; j < subG.nodes.length; j++) {
×
439
      log.info('Setting up subgraphs', subG.nodes[j], subG.id);
×
440
      g.setParent(subG.nodes[j], subG.id);
×
441
    }
×
442
  }
×
443
  addVertices(vert, g, id, root, doc, diagObj);
×
444
  addEdges(edges, g, diagObj);
×
445

×
446
  // Add custom shapes
×
447
  // flowChartShapes.addToRenderV2(addShape);
×
448

×
449
  // Set up an SVG group so that we can translate the final graph.
×
450
  const svg = root.select(`[id="${id}"]`);
×
451

×
452
  // Run the renderer. This is what draws the final graph.
×
453
  const element = root.select('#' + id + ' g');
×
454
  await render(element, g, ['point', 'circle', 'cross'], 'flowchart', id);
×
455

×
456
  utils.insertTitle(svg, 'flowchartTitleText', conf.titleTopMargin, diagObj.db.getDiagramTitle());
×
457

×
458
  setupGraphViewbox(g, svg, conf.diagramPadding, conf.useMaxWidth);
×
459

×
460
  // Index nodes
×
461
  diagObj.db.indexNodes('subGraph' + i);
×
462

×
463
  // Add label rects for non html labels
×
464
  if (!conf.htmlLabels) {
×
465
    const labels = doc.querySelectorAll('[id="' + id + '"] .edgeLabel .label');
×
466
    for (const label of labels) {
×
467
      // Get dimensions of label
×
468
      const dim = label.getBBox();
×
469

×
470
      const rect = doc.createElementNS('http://www.w3.org/2000/svg', 'rect');
×
471
      rect.setAttribute('rx', 0);
×
472
      rect.setAttribute('ry', 0);
×
473
      rect.setAttribute('width', dim.width);
×
474
      rect.setAttribute('height', dim.height);
×
475

×
476
      label.insertBefore(rect, label.firstChild);
×
477
    }
×
478
  }
×
479

×
480
  // If node has a link, wrap it in an anchor SVG object.
×
481
  const keys = Object.keys(vert);
×
482
  keys.forEach(function (key) {
×
483
    const vertex = vert[key];
×
484

×
485
    if (vertex.link) {
×
486
      const node = select('#' + id + ' [id="' + key + '"]');
×
487
      if (node) {
×
488
        const link = doc.createElementNS('http://www.w3.org/2000/svg', 'a');
×
489
        link.setAttributeNS('http://www.w3.org/2000/svg', 'class', vertex.classes.join(' '));
×
490
        link.setAttributeNS('http://www.w3.org/2000/svg', 'href', vertex.link);
×
491
        link.setAttributeNS('http://www.w3.org/2000/svg', 'rel', 'noopener');
×
492
        if (securityLevel === 'sandbox') {
×
493
          link.setAttributeNS('http://www.w3.org/2000/svg', 'target', '_top');
×
494
        } else if (vertex.linkTarget) {
×
495
          link.setAttributeNS('http://www.w3.org/2000/svg', 'target', vertex.linkTarget);
×
496
        }
×
497

×
498
        const linkNode = node.insert(function () {
×
499
          return link;
×
500
        }, ':first-child');
×
501

×
502
        const shape = node.select('.label-container');
×
503
        if (shape) {
×
504
          linkNode.append(function () {
×
505
            return shape.node();
×
506
          });
×
507
        }
×
508

×
509
        const label = node.select('.label');
×
510
        if (label) {
×
511
          linkNode.append(function () {
×
512
            return label.node();
×
513
          });
×
514
        }
×
515
      }
×
516
    }
×
517
  });
×
518
};
×
519

1✔
520
export default {
1✔
521
  setConf,
1✔
522
  addVertices,
1✔
523
  addEdges,
1✔
524
  getClasses,
1✔
525
  draw,
1✔
526
};
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc