diff --git a/src/diagrams/flowchart/parser/flow-vertice-chaining.spec.js b/src/diagrams/flowchart/parser/flow-vertice-chaining.spec.js new file mode 100644 index 000000000..e2247418c --- /dev/null +++ b/src/diagrams/flowchart/parser/flow-vertice-chaining.spec.js @@ -0,0 +1,38 @@ +import flowDb from '../flowDb' +import flow from './flow' +import { setConfig } from '../../../config' + +setConfig({ + securityLevel: 'strict', +}) + +describe('when parsing flowcharts', function () { + beforeEach(function () { + flow.parser.yy = flowDb + flow.parser.yy.clear() + }) + + it('should handle chaining of vertices', function () { + const res = flow.parser.parse(` + graph TD + A-->B-->C; + `); + + const vert = flow.parser.yy.getVertices() + const edges = flow.parser.yy.getEdges() + + expect(vert['A'].id).toBe('A') + expect(vert['B'].id).toBe('B') + expect(vert['C'].id).toBe('C') + expect(edges.length).toBe(2) + expect(edges[0].start).toBe('A') + expect(edges[0].end).toBe('B') + expect(edges[0].type).toBe('arrow') + expect(edges[0].text).toBe('') + expect(edges[1].start).toBe('B') + expect(edges[1].end).toBe('C') + expect(edges[1].type).toBe('arrow') + expect(edges[1].text).toBe('') + }) + +}) diff --git a/src/diagrams/flowchart/parser/flow.jison b/src/diagrams/flowchart/parser/flow.jison index 27ac0c9de..0cb38728e 100644 --- a/src/diagrams/flowchart/parser/flow.jison +++ b/src/diagrams/flowchart/parser/flow.jison @@ -264,18 +264,28 @@ statement separator: NEWLINE | SEMI | EOF ; -verticeStatement: - vertex link vertex - { yy.addLink($1,$3,$2);$$ = [$1,$3];} - | vertex link vertex STYLE_SEPARATOR idString - { yy.addLink($1,$3,$2);$$ = [$1,$3];yy.setClass($3,$5);} - | vertex STYLE_SEPARATOR idString link vertex - { yy.addLink($1,$5,$4);$$ = [$1,$5];yy.setClass($1,$3);} - | vertex STYLE_SEPARATOR idString link vertex STYLE_SEPARATOR idString - { yy.addLink($1,$5,$4);$$ = [$1,$5];yy.setClass($5,$7);yy.setClass($1,$3);} - |vertex +// verticeStatement: +// vertex link vertex +// { yy.addLink($1,$3,$2);$$ = [$1,$3];} +// | vertex link vertex STYLE_SEPARATOR idString +// { yy.addLink($1,$3,$2);$$ = [$1,$3];yy.setClass($3,$5);} +// | vertex STYLE_SEPARATOR idString link vertex +// { yy.addLink($1,$5,$4);$$ = [$1,$5];yy.setClass($1,$3);} +// | vertex STYLE_SEPARATOR idString link vertex STYLE_SEPARATOR idString +// { yy.addLink($1,$5,$4);$$ = [$1,$5];yy.setClass($5,$7);yy.setClass($1,$3);} +// |vertex +// {$$ = [$1];} +// |vertex STYLE_SEPARATOR idString +// {$$ = [$1];yy.setClass($1,$3)} +// ; + +verticeStatement: verticeStatement link node { yy.addLink($1[0],$3[0],$2); $$ = $3.concat($1) } + |node { $$ = $1 } + ; + +node: vertex {$$ = [$1];} - |vertex STYLE_SEPARATOR idString + | vertex STYLE_SEPARATOR idString {$$ = [$1];yy.setClass($1,$3)} ; diff --git a/src/diagrams/flowchart/parser/subgraph.spec.js b/src/diagrams/flowchart/parser/subgraph.spec.js index 66ff1fb3e..6a33c30be 100644 --- a/src/diagrams/flowchart/parser/subgraph.spec.js +++ b/src/diagrams/flowchart/parser/subgraph.spec.js @@ -17,8 +17,20 @@ describe('when parsing subgraphs', function () { expect(subgraphs.length).toBe(1) const subgraph = subgraphs[0] expect(subgraph.nodes.length).toBe(2) - expect(subgraph.nodes[0]).toBe('a1') + expect(subgraph.nodes[0]).toBe('a2') + expect(subgraph.nodes[1]).toBe('a1') + expect(subgraph.title).toBe('One') + expect(subgraph.id).toBe('One') + }) + it('should handle subgraph with chaining nodes indentation', function () { + const res = flow.parser.parse('graph TB\nsubgraph One\n\ta1-->a2-->a3\nend') + const subgraphs = flow.parser.yy.getSubGraphs() + expect(subgraphs.length).toBe(1) + const subgraph = subgraphs[0] + expect(subgraph.nodes.length).toBe(3) + expect(subgraph.nodes[0]).toBe('a3') expect(subgraph.nodes[1]).toBe('a2') + expect(subgraph.nodes[2]).toBe('a1') expect(subgraph.title).toBe('One') expect(subgraph.id).toBe('One') }) @@ -29,8 +41,8 @@ describe('when parsing subgraphs', function () { expect(subgraphs.length).toBe(1) const subgraph = subgraphs[0] expect(subgraph.nodes.length).toBe(2) - expect(subgraph.nodes[0]).toBe('a1') - expect(subgraph.nodes[1]).toBe('a2') + expect(subgraph.nodes[0]).toBe('a2') + expect(subgraph.nodes[1]).toBe('a1') expect(subgraph.title).toBe('Some Title') expect(subgraph.id).toBe('subGraph0') }); @@ -41,8 +53,8 @@ describe('when parsing subgraphs', function () { expect(subgraphs.length).toBe(1) const subgraph = subgraphs[0] expect(subgraph.nodes.length).toBe(2) - expect(subgraph.nodes[0]).toBe('a1') - expect(subgraph.nodes[1]).toBe('a2') + expect(subgraph.nodes[0]).toBe('a2') + expect(subgraph.nodes[1]).toBe('a1') expect(subgraph.title).toBe('Some Title') expect(subgraph.id).toBe('some-id') });