feat: adds text sanitization, refactors function

This commit is contained in:
Lindsey Wild 2022-04-07 14:58:36 -04:00
parent 7a5149e39e
commit e2ec7fd653

View File

@ -25,12 +25,14 @@ let version; // As in graph
// Functions to be run after graph rendering // Functions to be run after graph rendering
let funs = []; let funs = [];
const sanitizeText = (txt) => common.sanitizeText(txt, config);
export const parseDirective = function (statement, context, type) { export const parseDirective = function (statement, context, type) {
mermaidAPI.parseDirective(this, statement, context, type); mermaidAPI.parseDirective(this, statement, context, type);
}; };
const setTitle = function (txt) { const setTitle = function (txt) {
title = txt; title = sanitizeText(txt);
}; };
const getTitle = function () { const getTitle = function () {
@ -38,7 +40,7 @@ const getTitle = function () {
}; };
const setAccDescription = function (txt) { const setAccDescription = function (txt) {
description = txt; description = sanitizeText(txt);
}; };
const getAccDescription = function () { const getAccDescription = function () {
@ -95,7 +97,7 @@ export const addVertex = function (_id, text, type, style, classes, dir, props =
vertexCounter++; vertexCounter++;
if (typeof text !== 'undefined') { if (typeof text !== 'undefined') {
config = configApi.getConfig(); config = configApi.getConfig();
txt = common.sanitizeText(text.trim(), config); txt = sanitizeText(text.trim());
// 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] === '"') {
@ -150,7 +152,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 = common.sanitizeText(linktext.trim(), config); edge.text = sanitizeText(linktext.trim());
// 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] === '"') {
@ -273,7 +275,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[version === 'gen-1' ? lookUpDomId(id) : id] = common.sanitizeText(tooltip, config); tooltips[version === 'gen-1' ? lookUpDomId(id) : id] = sanitizeText(tooltip);
} }
}); });
}; };
@ -506,7 +508,7 @@ export const addSubGraph = function (_id, list, _title) {
id = id || 'subGraph' + subCount; id = id || 'subGraph' + subCount;
// if (id[0].match(/\d/)) id = lookUpDomId(id); // if (id[0].match(/\d/)) id = lookUpDomId(id);
title = title || ''; title = title || '';
title = common.sanitizeText(title, config); title = sanitizeText(title);
subCount = subCount + 1; subCount = subCount + 1;
const subGraph = { id: id, nodes: nodeList, title: title.trim(), classes: [], dir }; const subGraph = { id: id, nodes: nodeList, title: title.trim(), classes: [], dir };