Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/1066 Parse method does not return a boolean #1067

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ $(document).ready(function() {

Not doing so will most likely result in mermaid rendering graphs that have labels out of bounds. The default integration in mermaid uses the window.load event to start rendering.

If your page has other fonts in its body those might be used instead of the mermaid font. Specifying the font in your styling is a workaround for this.
If your page has other fonts in its body those might be used instead of the mermaid font. Specifying the font in your styling is a workaround for this.
```
div.mermaid {
font-family: 'trebuchet ms', verdana, arial;
Expand Down Expand Up @@ -262,8 +262,14 @@ function in order to handle the error in an application-specific way.

**Parsing text without rendering**

Text can also be parsed without rendering it. The function
**mermaid.validate(txt)** takes a text string as an argument and returns true if the text is syntactically correct and
false if it is not. The parseError function will be called when the parse function returns false.

**Validating text**

It is also possible to validate the syntax before rendering in order to streamline the user experience. The function
**mermaid.parse(txt)** takes a text string as an argument and returns true if the text is syntactically correct and
**mermaid.validate(txt)** takes a text string as an argument and returns true if the text is syntactically correct and
false if it is not. The parseError function will be called when the parse function returns false.

The code-example below in meta code illustrates how this could work:
Expand Down
1 change: 1 addition & 0 deletions src/mermaid.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ const mermaid = {

mermaidAPI,
parse: mermaidAPI.parse,
validate: mermaidAPI.validate,
render: mermaidAPI.render,

init,
Expand Down
22 changes: 18 additions & 4 deletions src/mermaid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import flowDb from './diagrams/flowchart/flowDb';
import flowParser from './diagrams/flowchart/parser/flow';
import flowRenderer from './diagrams/flowchart/flowRenderer';

describe('when using mermaid and ', function() {
describe('when detecting chart type ', function() {
describe('when using mermaid and', function() {
describe('when detecting chart type', function() {
it('should not start rendering with mermaid.startOnLoad set to false', function() {
mermaid.startOnLoad = false;
document.body.innerHTML = '<div class="mermaid">graph TD;\na;</div>';
Expand Down Expand Up @@ -38,7 +38,7 @@ describe('when using mermaid and ', function() {
});
});

describe('when calling addEdges ', function() {
describe('when calling addEdges', function() {
beforeEach(function() {
flowParser.parser.yy = flowDb;
flowDb.clear();
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('when using mermaid and ', function() {
});
});

describe('checking validity of input ', function() {
describe('parsing input', function() {
beforeEach(function() {
flowParser.parser.yy = flowDb;
flowDb.clear();
Expand Down Expand Up @@ -224,4 +224,18 @@ describe('when using mermaid and ', function() {
expect(() => mermaid.parse(text)).toThrow();
});
});
describe('checking validity of input', function() {
beforeEach(function() {
flowParser.parser.yy = flowDb;
flowDb.clear();
});
it('it should return false for an invalid definiton', function() {
const res = mermaid.validate('this is not a mermaid diagram definition');
expect(res).toBeFalsy();
});
it('it should return true for a valid definition', function() {
const res = mermaid.validate('graph TD;A--x|text including URL space|B;');
expect(res).toBeTruthy();
});
});
});
11 changes: 11 additions & 0 deletions src/mermaidAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,16 @@ function parse(text) {
parser.parse(text);
}

function validate(text) {
try {
parse(text);
return true;
} catch (e) {
// ignored
}
return false;
}

export const encodeEntities = function(text) {
let txt = text;

Expand Down Expand Up @@ -677,6 +687,7 @@ function initialize(options) {
const mermaidAPI = {
render,
parse,
validate,
initialize,
getConfig
};
Expand Down
17 changes: 14 additions & 3 deletions src/mermaidAPI.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-env jasmine */
import mermaidAPI from './mermaidAPI';

describe('when using mermaidAPI and ', function() {
describe('doing initialize ', function() {
describe('when using mermaidAPI and', function() {
describe('doing initialize', function() {
beforeEach(function() {
document.body.innerHTML = '';
});
Expand Down Expand Up @@ -34,12 +34,23 @@ describe('when using mermaidAPI and ', function() {
expect(config.testObject.test3).toBe(true);
});
});
describe('checking validity of input ', function() {
describe('parsing input', function() {
it('it should throw for an invalid definiton', function() {
expect(() => mermaidAPI.parse('this is not a mermaid diagram definition')).toThrow();
});
it('it should not throw for a valid definiton', function() {
expect(() => mermaidAPI.parse('graph TD;A--x|text including URL space|B;')).not.toThrow();
});
});

describe('checking validity of input', function() {
it('it should return false for an invalid definiton', function() {
const res = mermaidAPI.validate('this is not a mermaid diagram definition');
expect(res).toBeFalsy();
});
it('it should return true for a valid definiton', function() {
const res = mermaidAPI.validate('graph TD;A--x|text including URL space|B;');
expect(res).toBeTruthy();
});
});
});