From 1253326768b666eb56ae432a6fffd916ef68b0c8 Mon Sep 17 00:00:00 2001 From: Maciej Barelkowski Date: Fri, 15 Oct 2021 10:54:03 +0200 Subject: [PATCH] fix(copy-paste): do not create new id if unnecessary Related to https://github.com/camunda/camunda-modeler/issues/1410 --- lib/features/copy-paste/ModdleCopy.js | 9 ++++-- .../features/copy-paste/ModdleCopySpec.js | 30 +++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/features/copy-paste/ModdleCopy.js b/lib/features/copy-paste/ModdleCopy.js index 0332be1c..30c1a840 100644 --- a/lib/features/copy-paste/ModdleCopy.js +++ b/lib/features/copy-paste/ModdleCopy.js @@ -215,8 +215,13 @@ ModdleCopy.prototype.copyProperty = function(property, parent, propertyName) { var propertyDescriptor = this._moddle.getPropertyDescriptor(parent, propertyName); - // do NOT copy Ids and references - if (propertyDescriptor.isId || propertyDescriptor.isReference) { + // do NOT copy references + if (propertyDescriptor.isReference) { + return; + } + + // disallow copying IDs if already assigned + if (propertyDescriptor.isId && this._moddle.ids.assigned(property)) { return; } diff --git a/test/spec/features/copy-paste/ModdleCopySpec.js b/test/spec/features/copy-paste/ModdleCopySpec.js index 59c9a3e8..3b2c5f38 100644 --- a/test/spec/features/copy-paste/ModdleCopySpec.js +++ b/test/spec/features/copy-paste/ModdleCopySpec.js @@ -98,18 +98,36 @@ describe('features/copy-paste/ModdleCopy', function() { )); - it('should NOT copy IDs', inject(function(moddle, moddleCopy) { + it('should NOT copy IDs if taken', inject(function(moddle, moddleCopy, canvas, modeling) { // given - var task = moddle.create('bpmn:Task', { - id: 'foo' - }); + var task = modeling.createShape({ type: 'bpmn:Task' }, + { x: 0, y: 0 }, canvas.getRootElement()); + var taskId = task.id; // when - var userTask = moddleCopy.copyElement(task, moddle.create('bpmn:UserTask')); + var userTask = moddleCopy.copyElement(task.businessObject, moddle.create('bpmn:UserTask')); // then - expect(userTask.id).not.to.equal('foo'); + expect(userTask.id).not.to.equal(taskId); + + expectNoAttrs(userTask); + })); + + + it('should copy IDs if free', inject(function(moddle, moddleCopy, canvas, modeling) { + + // given + var task = modeling.createShape({ type: 'bpmn:Task' }, + { x: 0, y: 0 }, canvas.getRootElement()); + var taskId = task.id; + + // when + modeling.removeShape(task); + var userTask = moddleCopy.copyElement(task.businessObject, moddle.create('bpmn:UserTask')); + + // then + expect(userTask.id).to.equal(taskId); expectNoAttrs(userTask); }));