diff --git a/js/id/core/graph.js b/js/id/core/graph.js index a3fb8e823..a595f0df6 100644 --- a/js/id/core/graph.js +++ b/js/id/core/graph.js @@ -93,6 +93,7 @@ iD.Graph.prototype = { rebase: function(entities) { var base = this.base(), i, k, child, id, keys; + // Merging of data only needed if graph is the base graph if (!this.inherited) { for (i in entities) { @@ -110,7 +111,7 @@ iD.Graph.prototype = { if (base.parentWays[child]) { for (k = 0; k < base.parentWays[child].length; k++) { id = base.parentWays[child][k]; - if (this.entity(id) && !_.contains(this._parentWays[child], id)) { + if (!this.entities.hasOwnProperty(id) && !_.contains(this._parentWays[child], id)) { this._parentWays[child].push(id); } } @@ -123,7 +124,7 @@ iD.Graph.prototype = { if (base.parentRels[child]) { for (k = 0; k < base.parentRels[child].length; k++) { id = base.parentRels[child][k]; - if (this.entity(id) && !_.contains(this._parentRels[child], id)) { + if (!this.entities.hasOwnProperty(id) && !_.contains(this._parentRels[child], id)) { this._parentRels[child].push(id); } } diff --git a/test/spec/core/graph.js b/test/spec/core/graph.js index a099d8b61..ef6741bb9 100644 --- a/test/spec/core/graph.js +++ b/test/spec/core/graph.js @@ -107,19 +107,36 @@ describe('iD.Graph', function() { w3 = iD.Way({id: 'w3', nodes: ['n']}), graph = iD.Graph([n, w1]), graph2 = graph.replace(w2); + graph.rebase({ 'w3': w3 }); graph2.rebase({ 'w3': w3 }); expect(graph2.parentWays(n)).to.eql([w1, w2, w3]); }); - it("avoids re-adding removed parentWays", function() { + it("avoids re-adding a modified way as a parent way", function() { + var n1 = iD.Node({id: 'n1'}), + n2 = iD.Node({id: 'n2'}), + w1 = iD.Way({id: 'w1', nodes: ['n1', 'n2']}), + w2 = w1.removeNode('n2'), + graph = iD.Graph([n1, n2, w1]), + graph2 = graph.replace(w2); + + graph.rebase({ 'w1': w1 }); + graph2.rebase({ 'w1': w1 }); + + expect(graph2.parentWays(n2)).to.eql([]); + }); + + it("avoids re-adding a deleted way as a parent way", function() { var n = iD.Node({id: 'n'}), w1 = iD.Way({id: 'w1', nodes: ['n']}), graph = iD.Graph([n, w1]), graph2 = graph.remove(w1); + graph.rebase({ 'w1': w1 }); graph2.rebase({ 'w1': w1 }); + expect(graph2.parentWays(n)).to.eql([]); }); @@ -135,14 +152,29 @@ describe('iD.Graph', function() { expect(graph._parentRels.hasOwnProperty('n')).to.be.false; }); - it("avoids re-adding removed parentRels", function() { + it("avoids re-adding a modified relation as a parent relation", function() { + var n = iD.Node({id: 'n'}), + r1 = iD.Relation({id: 'r1', members: [{id: 'n'}]}), + r2 = r1.removeMember('n'), + graph = iD.Graph([n, r1]), + graph2 = graph.replace(r2); + + graph.rebase({ 'r1': r1 }); + graph2.rebase({ 'r1': r1 }); + + expect(graph2.parentRelations(n)).to.eql([]); + }); + + it("avoids re-adding a deleted relation as a parent relation", function() { var n = iD.Node({id: 'n'}), r1 = iD.Relation({id: 'r1', members: [{id: 'n'}]}), graph = iD.Graph([n, r1]), graph2 = graph.remove(r1); - graph.rebase({ 'w1': r1 }); - graph2.rebase({ 'w1': r1 }); - expect(graph2.parentWays(n)).to.eql([]); + + graph.rebase({ 'r1': r1 }); + graph2.rebase({ 'r1': r1 }); + + expect(graph2.parentRelations(n)).to.eql([]); }); it("updates parentRels for nodes with modified parentWays", function () {