review fixes

This commit is contained in:
Aakansha Doshi
2023-11-03 13:16:30 +05:30
parent fff25e7e2c
commit 6e6e92a1d1
6 changed files with 44 additions and 54 deletions

View File

@@ -3,9 +3,8 @@ import { log } from './logger.js';
import { getDiagram, registerDiagram } from './diagram-api/diagramAPI.js'; import { getDiagram, registerDiagram } from './diagram-api/diagramAPI.js';
import { detectType, getDiagramLoader } from './diagram-api/detectType.js'; import { detectType, getDiagramLoader } from './diagram-api/detectType.js';
import { UnknownDiagramError } from './errors.js'; import { UnknownDiagramError } from './errors.js';
import type { DetailedError } from './utils.js'; import { encodeEntities, type DetailedError } from './utils.js';
import type { DiagramDefinition, DiagramMetadata } from './diagram-api/types.js'; import type { DiagramDefinition, DiagramMetadata } from './diagram-api/types.js';
import { encodeEntities } from './mermaidAPI.js';
export type ParseErrorFunction = (err: string | DetailedError | unknown, hash?: any) => void; export type ParseErrorFunction = (err: string | DetailedError | unknown, hash?: any) => void;

View File

@@ -19,14 +19,6 @@ export interface InjectUtils {
_parseDirective: any; _parseDirective: any;
} }
export type Message = {
type: number;
to: string;
from: string;
message: string;
wrap: boolean;
};
/** /**
* Generic Diagram DB that may apply to any diagram type. * Generic Diagram DB that may apply to any diagram type.
*/ */
@@ -45,7 +37,6 @@ export interface DiagramDB {
setDisplayMode?: (title: string) => void; setDisplayMode?: (title: string) => void;
bindFunctions?: (element: Element) => void; bindFunctions?: (element: Element) => void;
getMessages?: () => Message[];
} }
// This is what is returned from getClasses(...) methods. // This is what is returned from getClasses(...) methods.

View File

@@ -74,13 +74,13 @@ Expecting 'TXT', got 'NEWLINE'"
const diagram = await getDiagramFromText(`sequenceDiagram const diagram = await getDiagramFromText(`sequenceDiagram
A->>B: I #9829; you! A->>B: I #9829; you!
B->>A: I #9829; you #infin; times more!`); B->>A: I #9829; you #infin; times more!`);
//@ts-ignore
const messages = diagram.db?.getMessages?.(); const messages = diagram.db?.getMessages?.();
if (!messages) { if (!messages) {
throw new Error('Messages not found!'); throw new Error('Messages not found!');
} }
const result = ['I fl°°9829¶ß you!', 'I fl°°9829¶ß you fl°infin¶ß times more!'];
messages.forEach((message, index: number) => { expect(messages[0].message).toBe('I fl°°9829¶ß you!');
expect(message.message).toBe(result[index]); expect(messages[1].message).toBe('I fl°°9829¶ß you fl°infin¶ß times more!');
});
}); });
}); });

View File

@@ -38,8 +38,6 @@ import type { MermaidConfig } from './config.type.js';
import mermaidAPI, { removeExistingElements } from './mermaidAPI.js'; import mermaidAPI, { removeExistingElements } from './mermaidAPI.js';
import { import {
encodeEntities,
decodeEntities,
createCssStyles, createCssStyles,
createUserStyles, createUserStyles,
appendDivSvgG, appendDivSvgG,
@@ -68,6 +66,7 @@ vi.mock('stylis', () => {
}; };
}); });
import { compile, serialize } from 'stylis'; import { compile, serialize } from 'stylis';
import { decodeEntities, encodeEntities } from './utils.js';
/** /**
* @see https://vitest.dev/guide/mocking.html Mock part of a module * @see https://vitest.dev/guide/mocking.html Mock part of a module

View File

@@ -30,6 +30,7 @@ import isEmpty from 'lodash-es/isEmpty.js';
import { setA11yDiagramInfo, addSVGa11yTitleDescription } from './accessibility.js'; import { setA11yDiagramInfo, addSVGa11yTitleDescription } from './accessibility.js';
import type { DiagramStyleClassDef } from './diagram-api/types.js'; import type { DiagramStyleClassDef } from './diagram-api/types.js';
import { preprocessDiagram } from './preprocess.js'; import { preprocessDiagram } from './preprocess.js';
import { decodeEntities } from './utils.js';
const MAX_TEXTLENGTH = 50_000; const MAX_TEXTLENGTH = 50_000;
const MAX_TEXTLENGTH_EXCEEDED_MSG = const MAX_TEXTLENGTH_EXCEEDED_MSG =
@@ -110,43 +111,6 @@ async function parse(text: string, parseOptions?: ParseOptions): Promise<boolean
return true; return true;
} }
/**
* @param text - text to be encoded
* @returns
*/
export const encodeEntities = function (text: string): string {
let txt = text;
txt = txt.replace(/style.*:\S*#.*;/g, function (s): string {
return s.substring(0, s.length - 1);
});
txt = txt.replace(/classDef.*:\S*#.*;/g, function (s): string {
return s.substring(0, s.length - 1);
});
txt = txt.replace(/#\w+;/g, function (s) {
const innerTxt = s.substring(1, s.length - 1);
const isInt = /^\+?\d+$/.test(innerTxt);
if (isInt) {
return 'fl°°' + innerTxt + '¶ß';
} else {
return 'fl°' + innerTxt + '¶ß';
}
});
return txt;
};
/**
*
* @param text - text to be decoded
* @returns
*/
export const decodeEntities = function (text: string): string {
return text.replace(/fl°°/g, '&#').replace(/fl°/g, '&').replace(/¶ß/g, ';');
};
// append !important; to each cssClass followed by a final !important, all enclosed in { } // append !important; to each cssClass followed by a final !important, all enclosed in { }
// //
/** /**

View File

@@ -888,3 +888,40 @@ export default {
parseFontSize, parseFontSize,
InitIDGenerator, InitIDGenerator,
}; };
/**
* @param text - text to be encoded
* @returns
*/
export const encodeEntities = function (text: string): string {
let txt = text;
txt = txt.replace(/style.*:\S*#.*;/g, function (s): string {
return s.substring(0, s.length - 1);
});
txt = txt.replace(/classDef.*:\S*#.*;/g, function (s): string {
return s.substring(0, s.length - 1);
});
txt = txt.replace(/#\w+;/g, function (s) {
const innerTxt = s.substring(1, s.length - 1);
const isInt = /^\+?\d+$/.test(innerTxt);
if (isInt) {
return 'fl°°' + innerTxt + '¶ß';
} else {
return 'fl°' + innerTxt + '¶ß';
}
});
return txt;
};
/**
*
* @param text - text to be decoded
* @returns
*/
export const decodeEntities = function (text: string): string {
return text.replace(/fl°°/g, '&#').replace(/fl°/g, '&').replace(/¶ß/g, ';');
};