mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-09 18:39:41 +02:00
1169- break out getRows
Moved getRows function from `state/stateRenderer.js` and `state/shapes.js` into `common/common.js`. Broke out section into small one line functions for replacing line breaks, then moved the `sanitize` function from `utils.js` to this new module as there is shared functionality
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import * as d3 from 'd3';
|
import * as d3 from 'd3';
|
||||||
import { logger } from '../../logger';
|
import { logger } from '../../logger';
|
||||||
import { getConfig } from '../../config';
|
import { getConfig } from '../../config';
|
||||||
|
import common from '../common/common';
|
||||||
import utils from '../../utils';
|
import utils from '../../utils';
|
||||||
|
|
||||||
const MERMAID_DOM_ID_PREFIX = 'classid-';
|
const MERMAID_DOM_ID_PREFIX = 'classid-';
|
||||||
@@ -175,7 +176,7 @@ export const setLink = function(ids, linkStr, tooltip) {
|
|||||||
classes[id].link = utils.formatUrl(linkStr, config);
|
classes[id].link = utils.formatUrl(linkStr, config);
|
||||||
|
|
||||||
if (tooltip) {
|
if (tooltip) {
|
||||||
classes[id].tooltip = utils.sanitize(tooltip, config);
|
classes[id].tooltip = common.sanitizeText(tooltip, config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -207,7 +208,7 @@ const setClickFunc = function(domId, functionName, tooltip) {
|
|||||||
}
|
}
|
||||||
if (typeof classes[id] !== 'undefined') {
|
if (typeof classes[id] !== 'undefined') {
|
||||||
if (tooltip) {
|
if (tooltip) {
|
||||||
classes[id].tooltip = utils.sanitize(tooltip, config);
|
classes[id].tooltip = common.sanitizeText(tooltip, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
funs.push(function() {
|
funs.push(function() {
|
||||||
|
39
src/diagrams/common/common.js
Normal file
39
src/diagrams/common/common.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
export const getRows = s => {
|
||||||
|
if (!s) return 1;
|
||||||
|
let str = breakToPlaceholder(s);
|
||||||
|
str = str.replace(/\\n/g, '#br#');
|
||||||
|
return str.split('#br#');
|
||||||
|
};
|
||||||
|
|
||||||
|
export const sanitizeText = (text, config) => {
|
||||||
|
let txt = text;
|
||||||
|
let htmlLabels = true;
|
||||||
|
if (
|
||||||
|
config.flowchart &&
|
||||||
|
(config.flowchart.htmlLabels === false || config.flowchart.htmlLabels === 'false')
|
||||||
|
)
|
||||||
|
htmlLabels = false;
|
||||||
|
|
||||||
|
if (config.securityLevel !== 'loose' && htmlLabels) {
|
||||||
|
// eslint-disable-line
|
||||||
|
txt = breakToPlaceholder(txt);
|
||||||
|
txt = txt.replace(/</g, '<').replace(/>/g, '>');
|
||||||
|
txt = txt.replace(/=/g, '=');
|
||||||
|
txt = placeholderToBreak(txt);
|
||||||
|
}
|
||||||
|
|
||||||
|
return txt;
|
||||||
|
};
|
||||||
|
|
||||||
|
const breakToPlaceholder = s => {
|
||||||
|
return s.replace(/<br\s*\/?>/gi, '#br#');
|
||||||
|
};
|
||||||
|
|
||||||
|
const placeholderToBreak = s => {
|
||||||
|
return s.replace(/#br#/g, '<br/>');
|
||||||
|
};
|
||||||
|
|
||||||
|
export default {
|
||||||
|
getRows,
|
||||||
|
sanitizeText
|
||||||
|
};
|
@@ -2,6 +2,7 @@ import * as d3 from 'd3';
|
|||||||
import { logger } from '../../logger';
|
import { logger } from '../../logger';
|
||||||
import utils from '../../utils';
|
import utils from '../../utils';
|
||||||
import { getConfig } from '../../config';
|
import { getConfig } from '../../config';
|
||||||
|
import common from '../common/common';
|
||||||
|
|
||||||
// const MERMAID_DOM_ID_PREFIX = 'mermaid-dom-id-';
|
// const MERMAID_DOM_ID_PREFIX = 'mermaid-dom-id-';
|
||||||
const MERMAID_DOM_ID_PREFIX = '';
|
const MERMAID_DOM_ID_PREFIX = '';
|
||||||
@@ -43,7 +44,7 @@ export const addVertex = function(_id, text, type, style, classes) {
|
|||||||
vertices[id] = { id: id, styles: [], classes: [] };
|
vertices[id] = { id: id, styles: [], classes: [] };
|
||||||
}
|
}
|
||||||
if (typeof text !== 'undefined') {
|
if (typeof text !== 'undefined') {
|
||||||
txt = utils.sanitize(text.trim(), config);
|
txt = common.sanitizeText(text.trim(), config);
|
||||||
|
|
||||||
// strip quotes if string starts and ends with a quote
|
// strip quotes if string starts and ends with a quote
|
||||||
if (txt[0] === '"' && txt[txt.length - 1] === '"') {
|
if (txt[0] === '"' && txt[txt.length - 1] === '"') {
|
||||||
@@ -93,7 +94,7 @@ export const addSingleLink = function(_start, _end, type, linktext) {
|
|||||||
linktext = type.text;
|
linktext = type.text;
|
||||||
|
|
||||||
if (typeof linktext !== 'undefined') {
|
if (typeof linktext !== 'undefined') {
|
||||||
edge.text = utils.sanitize(linktext.trim(), config);
|
edge.text = common.sanitizeText(linktext.trim(), config);
|
||||||
|
|
||||||
// strip quotes if string starts and exnds with a quote
|
// strip quotes if string starts and exnds with a quote
|
||||||
if (edge.text[0] === '"' && edge.text[edge.text.length - 1] === '"') {
|
if (edge.text[0] === '"' && edge.text[edge.text.length - 1] === '"') {
|
||||||
@@ -210,7 +211,7 @@ export const setClass = function(ids, className) {
|
|||||||
const setTooltip = function(ids, tooltip) {
|
const setTooltip = function(ids, tooltip) {
|
||||||
ids.split(',').forEach(function(id) {
|
ids.split(',').forEach(function(id) {
|
||||||
if (typeof tooltip !== 'undefined') {
|
if (typeof tooltip !== 'undefined') {
|
||||||
tooltips[id] = utils.sanitize(tooltip, config);
|
tooltips[id] = common.sanitizeText(tooltip, config);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -410,7 +411,7 @@ export const addSubGraph = function(_id, list, _title) {
|
|||||||
id = id || 'subGraph' + subCount;
|
id = id || 'subGraph' + subCount;
|
||||||
if (id[0].match(/\d/)) id = MERMAID_DOM_ID_PREFIX + id;
|
if (id[0].match(/\d/)) id = MERMAID_DOM_ID_PREFIX + id;
|
||||||
title = title || '';
|
title = title || '';
|
||||||
title = utils.sanitize(title, config);
|
title = common.sanitizeText(title, config);
|
||||||
subCount = subCount + 1;
|
subCount = subCount + 1;
|
||||||
const subGraph = { id: id, nodes: nodeList, title: title.trim(), classes: [] };
|
const subGraph = { id: id, nodes: nodeList, title: title.trim(), classes: [] };
|
||||||
subGraphs.push(subGraph);
|
subGraphs.push(subGraph);
|
||||||
|
@@ -2,6 +2,7 @@ import * as d3 from 'd3';
|
|||||||
import idCache from './id-cache.js';
|
import idCache from './id-cache.js';
|
||||||
import stateDb from './stateDb';
|
import stateDb from './stateDb';
|
||||||
import utils from '../../utils';
|
import utils from '../../utils';
|
||||||
|
import common from '../common/common';
|
||||||
import { getConfig } from '../../config';
|
import { getConfig } from '../../config';
|
||||||
|
|
||||||
// let conf;
|
// let conf;
|
||||||
@@ -391,12 +392,6 @@ export const drawState = function(elem, stateDef) {
|
|||||||
return stateInfo;
|
return stateInfo;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getRows = s => {
|
|
||||||
let str = s.replace(/<br\s*\/?>/gi, '#br#');
|
|
||||||
str = str.replace(/\\n/g, '#br#');
|
|
||||||
return str.split('#br#');
|
|
||||||
};
|
|
||||||
|
|
||||||
let edgeCount = 0;
|
let edgeCount = 0;
|
||||||
export const drawEdge = function(elem, path, relation) {
|
export const drawEdge = function(elem, path, relation) {
|
||||||
const getRelationType = function(type) {
|
const getRelationType = function(type) {
|
||||||
@@ -455,7 +450,7 @@ export const drawEdge = function(elem, path, relation) {
|
|||||||
|
|
||||||
const { x, y } = utils.calcLabelPosition(path.points);
|
const { x, y } = utils.calcLabelPosition(path.points);
|
||||||
|
|
||||||
const rows = getRows(relation.title);
|
const rows = common.getRows(relation.title);
|
||||||
|
|
||||||
// console.warn(rows);
|
// console.warn(rows);
|
||||||
|
|
||||||
|
@@ -3,6 +3,7 @@ import dagre from 'dagre';
|
|||||||
import graphlib from 'graphlib';
|
import graphlib from 'graphlib';
|
||||||
import { logger } from '../../logger';
|
import { logger } from '../../logger';
|
||||||
import stateDb from './stateDb';
|
import stateDb from './stateDb';
|
||||||
|
import common from '../common/common';
|
||||||
import { parser } from './parser/stateDiagram';
|
import { parser } from './parser/stateDiagram';
|
||||||
// import idCache from './id-cache';
|
// import idCache from './id-cache';
|
||||||
import { drawState, addTitleAndBox, drawEdge } from './shapes';
|
import { drawState, addTitleAndBox, drawEdge } from './shapes';
|
||||||
@@ -99,14 +100,6 @@ const getLabelWidth = text => {
|
|||||||
return text ? text.length * conf.fontSizeFactor : 1;
|
return text ? text.length * conf.fontSizeFactor : 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* TODO: REMOVE DUPLICATION, SEE SHAPES */
|
|
||||||
const getRows = s => {
|
|
||||||
if (!s) return 1;
|
|
||||||
let str = s.replace(/<br\s*\/?>/gi, '#br#');
|
|
||||||
str = str.replace(/\\n/g, '#br#');
|
|
||||||
return str.split('#br#');
|
|
||||||
};
|
|
||||||
|
|
||||||
const renderDoc = (doc, diagram, parentId, altBkg) => {
|
const renderDoc = (doc, diagram, parentId, altBkg) => {
|
||||||
// // Layout graph, Create a new directed graph
|
// // Layout graph, Create a new directed graph
|
||||||
const graph = new graphlib.Graph({
|
const graph = new graphlib.Graph({
|
||||||
@@ -239,7 +232,7 @@ const renderDoc = (doc, diagram, parentId, altBkg) => {
|
|||||||
{
|
{
|
||||||
relation: relation,
|
relation: relation,
|
||||||
width: getLabelWidth(relation.title),
|
width: getLabelWidth(relation.title),
|
||||||
height: conf.labelHeight * getRows(relation.title).length,
|
height: conf.labelHeight * common.getRows(relation.title).length,
|
||||||
labelpos: 'c'
|
labelpos: 'c'
|
||||||
},
|
},
|
||||||
'id' + cnt
|
'id' + cnt
|
||||||
|
20
src/utils.js
20
src/utils.js
@@ -74,25 +74,6 @@ export const interpolateToCurve = (interpolate, defaultCurve) => {
|
|||||||
return d3[curveName] || defaultCurve;
|
return d3[curveName] || defaultCurve;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const sanitize = (text, config) => {
|
|
||||||
let txt = text;
|
|
||||||
let htmlLabels = true;
|
|
||||||
if (
|
|
||||||
config.flowchart &&
|
|
||||||
(config.flowchart.htmlLabels === false || config.flowchart.htmlLabels === 'false')
|
|
||||||
)
|
|
||||||
htmlLabels = false;
|
|
||||||
|
|
||||||
if (config.securityLevel !== 'loose' && htmlLabels) { // eslint-disable-line
|
|
||||||
txt = txt.replace(/<br\s*\/?>/gi, '#br#');
|
|
||||||
txt = txt.replace(/</g, '<').replace(/>/g, '>');
|
|
||||||
txt = txt.replace(/=/g, '=');
|
|
||||||
txt = txt.replace(/#br#/g, '<br/>');
|
|
||||||
}
|
|
||||||
|
|
||||||
return txt;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const formatUrl = (linkStr, config) => {
|
export const formatUrl = (linkStr, config) => {
|
||||||
let url = linkStr.trim();
|
let url = linkStr.trim();
|
||||||
|
|
||||||
@@ -225,7 +206,6 @@ export default {
|
|||||||
interpolateToCurve,
|
interpolateToCurve,
|
||||||
calcLabelPosition,
|
calcLabelPosition,
|
||||||
calcCardinalityPosition,
|
calcCardinalityPosition,
|
||||||
sanitize,
|
|
||||||
formatUrl,
|
formatUrl,
|
||||||
getStylesFromArray
|
getStylesFromArray
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user