diff --git a/src/extensions/layout/cose.js b/src/extensions/layout/cose.js index 38e5251724..3114c4cdab 100644 --- a/src/extensions/layout/cose.js +++ b/src/extensions/layout/cose.js @@ -105,8 +105,19 @@ var defaults = { */ function CoseLayout( options ){ this.options = util.extend( {}, defaults, options ); - this.options.layout = this; + + // Exclude any edge that has a source or target node that is not in the set of passed-in nodes + const nodes = this.options.eles.nodes(); + const edges = this.options.eles.edges(); + const notEdges = edges.filter((e) => { + const sourceId = e.source().data('id'); + const targetId = e.target().data('id'); + const hasSource = nodes.some((n) => n.data('id') === sourceId); + const hasTarget = nodes.some((n) => n.data('id') === targetId); + return !hasSource || !hasTarget; + }); + this.options.eles = this.options.eles.not(notEdges); } /**