fix parse() to match spec (return true if valid, false if invalid (and a parseError handler is defined))

This commit is contained in:
Tim Maffett 2022-05-05 11:48:53 -07:00
parent 8210e3c80a
commit 67b6414693

View File

@ -63,12 +63,15 @@ import getStyles from './styles';
import theme from './themes'; import theme from './themes';
import utils, { directiveSanitizer, assignWithDepth, sanitizeCss } from './utils'; import utils, { directiveSanitizer, assignWithDepth, sanitizeCss } from './utils';
import DOMPurify from 'dompurify'; import DOMPurify from 'dompurify';
import mermaid from './mermaid';
/** /**
* @param text * @param text
* @returns {any} * @returns {any}
*/ */
function parse(text) { function parse(text) {
var parseEncounteredException = false;
try {
text = text + '\n'; text = text + '\n';
const cnf = configApi.getConfig(); const cnf = configApi.getConfig();
const graphInit = utils.detectInit(text, cnf); const graphInit = utils.detectInit(text, cnf);
@ -155,7 +158,25 @@ function parse(text) {
}; };
parser.parse(text); parser.parse(text);
return parser; } catch (error) {
parseEncounteredException = true;
// Is this the correct way to access mermiad's parseError()
// method ? (or global.mermaid.parseError()) ?
if (mermaid.parseError) {
if (error.str != undefined) {
// handle case where error string and hash were
// wrapped in object like`const error = { str, hash };`
mermaid.parseError(error.str, error.hash);
} else {
// assume it is just error string and pass it on
mermaid.parseError(error);
}
} else {
// No mermaid.parseError() handler defined, so re-throw it
throw error;
}
}
return !parseEncounteredException;
} }
export const encodeEntities = function (text) { export const encodeEntities = function (text) {