Updated as per PR comments

This commit is contained in:
saurabhg772244 2025-01-23 12:25:38 +05:30
parent 90bbf90a83
commit b09f2e836a
2 changed files with 16 additions and 23 deletions

View File

@ -29,6 +29,8 @@ import type { DiagramDB } from '../../diagram-api/types.js';
const MERMAID_DOM_ID_PREFIX = 'classId-'; const MERMAID_DOM_ID_PREFIX = 'classId-';
let classCounter = 0; let classCounter = 0;
const sanitizeText = (txt: string) => common.sanitizeText(txt, getConfig());
export class ClassDB implements DiagramDB { export class ClassDB implements DiagramDB {
private relations: ClassRelation[] = []; private relations: ClassRelation[] = [];
private classes = new Map<string, ClassNode>(); private classes = new Map<string, ClassNode>();
@ -41,12 +43,8 @@ export class ClassDB implements DiagramDB {
private functions: any[] = []; private functions: any[] = [];
private sanitizeText(txt: string) {
return common.sanitizeText(txt, getConfig());
}
constructor() { constructor() {
this.functions.push(this.setupToolTips); this.functions.push(this.setupToolTips.bind(this));
this.clear(); this.clear();
// Needed for JISON since it only supports direct properties // Needed for JISON since it only supports direct properties
@ -64,10 +62,6 @@ export class ClassDB implements DiagramDB {
this.defineClass = this.defineClass.bind(this); this.defineClass = this.defineClass.bind(this);
this.setDirection = this.setDirection.bind(this); this.setDirection = this.setDirection.bind(this);
this.setLink = this.setLink.bind(this); this.setLink = this.setLink.bind(this);
this.setTooltip = this.setTooltip.bind(this);
this.setClickEvent = this.setClickEvent.bind(this);
this.setCssStyle = this.setCssStyle.bind(this);
this.setClickFunc = this.setClickFunc.bind(this);
this.bindFunctions = this.bindFunctions.bind(this); this.bindFunctions = this.bindFunctions.bind(this);
this.clear = this.clear.bind(this); this.clear = this.clear.bind(this);
} }
@ -79,8 +73,8 @@ export class ClassDB implements DiagramDB {
if (id.indexOf('~') > 0) { if (id.indexOf('~') > 0) {
const split = id.split('~'); const split = id.split('~');
className = this.sanitizeText(split[0]); className = sanitizeText(split[0]);
genericType = this.sanitizeText(split[1]); genericType = sanitizeText(split[1]);
} }
return { className: className, type: genericType }; return { className: className, type: genericType };
@ -89,7 +83,7 @@ export class ClassDB implements DiagramDB {
public setClassLabel(_id: string, label: string) { public setClassLabel(_id: string, label: string) {
const id = common.sanitizeText(_id, getConfig()); const id = common.sanitizeText(_id, getConfig());
if (label) { if (label) {
label = this.sanitizeText(label); label = sanitizeText(label);
} }
const { className } = this.splitClassNameAndType(id); const { className } = this.splitClassNameAndType(id);
@ -161,7 +155,7 @@ export class ClassDB implements DiagramDB {
this.notes = []; this.notes = [];
this.interfaces = []; this.interfaces = [];
this.functions = []; this.functions = [];
this.functions.push(this.setupToolTips); this.functions.push(this.setupToolTips.bind(this));
this.namespaces = new Map(); this.namespaces = new Map();
this.namespaceCounter = 0; this.namespaceCounter = 0;
this.direction = 'TB'; this.direction = 'TB';
@ -264,9 +258,7 @@ export class ClassDB implements DiagramDB {
if (memberString.startsWith('<<') && memberString.endsWith('>>')) { if (memberString.startsWith('<<') && memberString.endsWith('>>')) {
// its an annotation // its an annotation
theClass.annotations.push( theClass.annotations.push(sanitizeText(memberString.substring(2, memberString.length - 2)));
this.sanitizeText(memberString.substring(2, memberString.length - 2))
);
} else if (memberString.indexOf(')') > 0) { } else if (memberString.indexOf(')') > 0) {
//its a method //its a method
theClass.methods.push(new ClassMember(memberString, 'method')); theClass.methods.push(new ClassMember(memberString, 'method'));
@ -296,7 +288,7 @@ export class ClassDB implements DiagramDB {
if (label.startsWith(':')) { if (label.startsWith(':')) {
label = label.substring(1); label = label.substring(1);
} }
return this.sanitizeText(label.trim()); return sanitizeText(label.trim());
} }
/** /**
@ -353,7 +345,7 @@ export class ClassDB implements DiagramDB {
public setTooltip(ids: string, tooltip?: string) { public setTooltip(ids: string, tooltip?: string) {
ids.split(',').forEach((id) => { ids.split(',').forEach((id) => {
if (tooltip !== undefined) { if (tooltip !== undefined) {
this.classes.get(id)!.tooltip = this.sanitizeText(tooltip); this.classes.get(id)!.tooltip = sanitizeText(tooltip);
} }
}); });
} }
@ -386,7 +378,7 @@ export class ClassDB implements DiagramDB {
if (config.securityLevel === 'sandbox') { if (config.securityLevel === 'sandbox') {
theClass.linkTarget = '_top'; theClass.linkTarget = '_top';
} else if (typeof target === 'string') { } else if (typeof target === 'string') {
theClass.linkTarget = this.sanitizeText(target); theClass.linkTarget = sanitizeText(target);
} else { } else {
theClass.linkTarget = '_blank'; theClass.linkTarget = '_blank';
} }
@ -464,12 +456,12 @@ export class ClassDB implements DiagramDB {
}); });
} }
public lineType = { public readonly lineType = {
LINE: 0, LINE: 0,
DOTTED_LINE: 1, DOTTED_LINE: 1,
}; };
public relationType = { public readonly relationType = {
AGGREGATION: 0, AGGREGATION: 0,
EXTENSION: 1, EXTENSION: 1,
COMPOSITION: 2, COMPOSITION: 2,
@ -592,7 +584,7 @@ export class ClassDB implements DiagramDB {
* @param type - The type to look for * @param type - The type to look for
* @returns The arrow marker * @returns The arrow marker
*/ */
private readonly getArrowMarker = (type: number) => { private getArrowMarker(type: number) {
let marker; let marker;
switch (type) { switch (type) {
case 0: case 0:
@ -614,7 +606,7 @@ export class ClassDB implements DiagramDB {
marker = 'none'; marker = 'none';
} }
return marker; return marker;
}; }
public getData() { public getData() {
const nodes: Node[] = []; const nodes: Node[] = [];

View File

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/unbound-method -- Broken for Vitest mocks, see https://github.com/vitest-dev/eslint-plugin-vitest/pull/286 */
// @ts-expect-error Jison doesn't export types // @ts-expect-error Jison doesn't export types
import { parser } from './parser/classDiagram.jison'; import { parser } from './parser/classDiagram.jison';
import { ClassDB } from './classDb.js'; import { ClassDB } from './classDb.js';