Skip to content

Commit

Permalink
Merge pull request #452 from joshuacolvin/issue416_link_style_fill_none
Browse files Browse the repository at this point in the history
Fix for #416, customizing link style with any color sets `fill` property to `black` instead of `none`
  • Loading branch information
knsv authored Jan 9, 2017
2 parents 7ff87fd + 8596eb6 commit cac960c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/diagrams/flowchart/graphDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
var Logger = require('../../logger');
var log = new Logger.Log();
var utils = require('../../utils');

var d3 = require('../../d3');
var vertices = {};
Expand Down Expand Up @@ -109,6 +110,9 @@ exports.updateLink = function (pos, style) {
if(pos === 'default'){
edges.defaultStyle = style;
}else{
if(utils.isSubstringInArray('fill', style) === -1) {
style.push('fill:none');
}
edges[pos].style = style;
}
};
Expand Down
37 changes: 35 additions & 2 deletions src/mermaid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('when using mermaid and ',function() {
expect(start).toBe('A');
expect(end).toBe('B');
expect(options.arrowhead).toBe('none');
expect(options.style).toBe('stroke:val1;stroke-width:val2;');
expect(options.style).toBe('stroke:val1;stroke-width:val2;fill:none;');
}
};

Expand Down Expand Up @@ -183,7 +183,40 @@ describe('when using mermaid and ',function() {
expect(end).toBe('B');
expect(options.arrowhead).toBe('none');
expect(options.label.match('the text')).toBeTruthy();
expect(options.style).toBe('stroke:val1;stroke-width:val2;');
expect(options.style).toBe('stroke:val1;stroke-width:val2;fill:none;');
}
};

flowRend.addEdges(edges,mockG);
});

it('should set fill to "none" by default when handling edges', function () {
flow.parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2;');
flow.parser.yy.getVertices();
var edges = flow.parser.yy.getEdges();

var mockG = {
setEdge:function(start, end,options){
expect(start).toBe('A');
expect(end).toBe('B');
expect(options.arrowhead).toBe('none');
expect(options.style).toBe('stroke:val1;stroke-width:val2;fill:none;');
}
};

flowRend.addEdges(edges,mockG);
});

it('should not set fill to none if fill is set in linkStyle', function () {
flow.parser.parse('graph TD;A---B; linkStyle 0 stroke:val1,stroke-width:val2,fill:blue;');
flow.parser.yy.getVertices();
var edges = flow.parser.yy.getEdges();
var mockG = {
setEdge:function(start, end,options){
expect(start).toBe('A');
expect(end).toBe('B');
expect(options.arrowhead).toBe('none');
expect(options.style).toBe('stroke:val1;stroke-width:val2;fill:blue;');
}
};

Expand Down
17 changes: 17 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,20 @@ var cloneCssStyles = function(svg, classes){
};

exports.cloneCssStyles = cloneCssStyles;


/**
* @function isSubstringInArray
* Detects whether a substring in present in a given array
* @param {string} str The substring to detect
* @param {array} arr The array to search
* @returns {number} the array index containing the substring or -1 if not present
**/
var isSubstringInArray = function (str, arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i].match(str)) return i;
}
return -1;
};

exports.isSubstringInArray = isSubstringInArray;
12 changes: 12 additions & 0 deletions src/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,16 @@ describe('when cloning CSS ', function () {
'#mermaid-01 .node-circle>rect, .node-circle>polygon, .node-circle>circle, .node-circle>ellipse { fill:#444444; stroke:#111111; }'
]);
});

describe('when finding substring in array ', function () {
it('should return the array index that contains the substring', function () {
var arr = ['stroke:val1', 'fill:val2'];
var result = utils.isSubstringInArray('fill', arr);
expect(result).toEqual(1);
});
it('should return -1 if the substring is not found in the array', function () {
var arr = ['stroke:val1', 'stroke-width:val2'];
var result = utils.isSubstringInArray('fill', arr);
expect(result).toEqual(-1);
});
});

0 comments on commit cac960c

Please sign in to comment.