From 14d13dfda39e379cdf31ae008b7b121ebbc7274d Mon Sep 17 00:00:00 2001 From: Max Franz Date: Tue, 30 Apr 2019 17:15:18 -0400 Subject: [PATCH] `edge.isBundledBezier()` should return false for styleless instance - The internal style function, `ele.isBundledBezier()` will not work in a styleless instance, because `ele.pstyle()` will always return `undefined`. It should have a check like the other style functions. Most of these functions are derived state functions, which have this check implicitly. - `edge.isBundledBezier()` is only used in a few places. The only place that would affect a headless instance is `ele.remove()`. - This patch includes a fix and several test cases. Ref : Removing parallel edges in headless mode #2377 --- src/collection/style.js | 2 ++ test/collection-style.js | 38 +++++++++++++++++++++++++++++++++ test/core-graph-manipulation.js | 37 ++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/src/collection/style.js b/src/collection/style.js index f1ccdefb43..553d4613aa 100644 --- a/src/collection/style.js +++ b/src/collection/style.js @@ -415,6 +415,8 @@ elesfn.hidden = function(){ }; elesfn.isBundledBezier = cachePrototypeStyleFunction('isBundledBezier', function(){ + if( !this.cy().styleEnabled() ){ return false; } + return !this.removed() && this.pstyle('curve-style').value === 'bezier' && this.takesUpSpace(); }); diff --git a/test/collection-style.js b/test/collection-style.js index 05a976f138..dd3308e1ca 100644 --- a/test/collection-style.js +++ b/test/collection-style.js @@ -547,6 +547,44 @@ describe('Collection style', function(){ expect( cy.$('#c').visible() ).to.be.true; }); + + it('ele.isBundledBezier() true for `curve-style: bezier`', function(){ + var edges = cy.add([ + { data: { id: 'bez1', source: 'n1', target: 'n2' } }, + { data: { id: 'bez2', source: 'n1', target: 'n2' } }, + { data: { id: 'bez3', source: 'n1', target: 'n2' } } + ]); + + edges.style('curve-style', 'bezier'); + + edges.forEach(function(edge){ + expect(edge.isBundledBezier(), edge.id()).to.be.true; + }); + }); + + it('ele.isBundledBezier() false for style disabled', function(){ + var cy = cytoscape({ + headless: true, + styleEnabled: false, + elements: [ + { data: { id: 'n1' } }, + { data: { id: 'n2' } } + ] + }); + + var edges = cy.add([ + { data: { id: 'bez1', source: 'n1', target: 'n2' } }, + { data: { id: 'bez2', source: 'n1', target: 'n2' } }, + { data: { id: 'bez3', source: 'n1', target: 'n2' } } + ]); + + // this should be a nop + edges.style('curve-style', 'bezier'); + + edges.forEach(function(edge){ + expect(edge.isBundledBezier(), edge.id()).to.be.false; + }); + }); }); describe('eles.addClass() etc', function(){ diff --git a/test/core-graph-manipulation.js b/test/core-graph-manipulation.js index 0b19618d08..94e1c147df 100644 --- a/test/core-graph-manipulation.js +++ b/test/core-graph-manipulation.js @@ -172,6 +172,7 @@ describe('Core graph manipulation', function(){ expect( cy.$('#foo').numericStyle('border-width') ).to.equal(10); }); + }); describe('eles.restore()', function(){ @@ -231,6 +232,42 @@ describe('Core graph manipulation', function(){ }); + it('removes a parallel edge headlessly', function(){ + // re. headlessly calling edge.isBundledBezier() #2377 + + var cy = cytoscape({ + headless: true, + styleEnabled: false, // important + elements: [ + { data: { id: 'a' } }, + { data: { id: 'b' } }, + { + data: { + id: 'ab2', + source: 'a', + target: 'b' + } + }, + { + data: { + id: 'ab1', + source: 'a', + target: 'b' + } + }] + }); + + cy.edges()[0].remove(); + + expect(cy.edges().length, 'number of edges (A)').to.equal(1); + expect(cy.nodes().length, 'number of nodes (A)').to.equal(2); + + cy.edges()[0].remove(); + + expect(cy.edges().length, 'number of edges (B)').to.equal(0); + expect(cy.nodes().length, 'number of nodes (B)').to.equal(2); + }); + it('removes via selector', function(){ cy.remove('edge');