Make sure cloned items do not receive the same name when placed inside the same parent, use numbered versions instead.

This commit is contained in:
Jürg Lehni 2013-01-19 22:27:29 -08:00
parent 4346563fe4
commit 7756e90ff9
4 changed files with 36 additions and 24 deletions

View file

@ -252,7 +252,7 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
return this._name; return this._name;
}, },
setName: function(name) { setName: function(name, unique) {
// Note: Don't check if the name has changed and bail out if it has not, // Note: Don't check if the name has changed and bail out if it has not,
// because setName is used internally also to update internal structures // because setName is used internally also to update internal structures
// when an item is moved from one parent to another. // when an item is moved from one parent to another.
@ -261,13 +261,18 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
// parent's children object: // parent's children object:
if (this._name) if (this._name)
this._removeFromNamed(); this._removeFromNamed();
this._name = name || undefined;
if (name && this._parent) { if (name && this._parent) {
var children = this._parent._children, var children = this._parent._children,
namedChildren = this._parent._namedChildren; namedChildren = this._parent._namedChildren,
orig = name,
i = 1;
// If unique is true, make sure we're not overriding other names
while (unique && children[name])
name = orig + ' ' + (i++);
(namedChildren[name] = namedChildren[name] || []).push(this); (namedChildren[name] = namedChildren[name] || []).push(this);
children[name] = this; children[name] = this;
} }
this._name = name || undefined;
this._changed(/*#=*/ ChangeFlag.ATTRIBUTE); this._changed(/*#=*/ ChangeFlag.ATTRIBUTE);
}, },
@ -1022,10 +1027,10 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
// Copy over the selection state, use setSelected so the item // Copy over the selection state, use setSelected so the item
// is also added to Project#selectedItems if it is selected. // is also added to Project#selectedItems if it is selected.
copy.setSelected(this._selected); copy.setSelected(this._selected);
// Only set name once the copy is moved, to avoid setting and unsettting // Clone the name too, but make sure we're not overriding the original
// name related structures. // in the same parent, by passing true for the unique parameter.
if (this._name) if (this._name)
copy.setName(this._name); copy.setName(this._name, true);
return copy; return copy;
}, },

View file

@ -92,7 +92,7 @@ function compareGradientColors(gradientColor, gradientColor2, checkIdentity) {
Base.each(['origin', 'destination', 'hilite'], function(key) { Base.each(['origin', 'destination', 'hilite'], function(key) {
if (checkIdentity) { if (checkIdentity) {
equals(function() { equals(function() {
return gradientColor[key] != gradientColor2[key]; return gradientColor[key] !== gradientColor2[key];
}, true, 'Strict compare GradientColor#' + key); }, true, 'Strict compare GradientColor#' + key);
} }
equals(gradientColor[key].toString(), gradientColor2[key].toString(), equals(gradientColor[key].toString(), gradientColor2[key].toString(),
@ -106,7 +106,7 @@ function compareGradientColors(gradientColor, gradientColor2, checkIdentity) {
function comparePathStyles(style, style2, checkIdentity) { function comparePathStyles(style, style2, checkIdentity) {
if (checkIdentity) { if (checkIdentity) {
equals(function() { equals(function() {
return style != style2; return style !== style2;
}, true); }, true);
} }
Base.each(['fillColor', 'strokeColor'], function(key) { Base.each(['fillColor', 'strokeColor'], function(key) {
@ -120,7 +120,7 @@ function comparePathStyles(style, style2, checkIdentity) {
if (style[key] instanceof GradientColor) { if (style[key] instanceof GradientColor) {
if (checkIdentity) { if (checkIdentity) {
equals(function() { equals(function() {
return style[key].gradient == style2[key].gradient; return style[key].gradient === style2[key].gradient;
}, true, 'The ' + key + '.gradient should point to the same object:'); }, true, 'The ' + key + '.gradient should point to the same object:');
} }
compareGradientColors(style[key], style2[key], checkIdentity); compareGradientColors(style[key], style2[key], checkIdentity);
@ -149,7 +149,7 @@ function comparePathStyles(style, style2, checkIdentity) {
function compareObjects(name, keys, obj, obj2, checkIdentity) { function compareObjects(name, keys, obj, obj2, checkIdentity) {
if (checkIdentity) { if (checkIdentity) {
equals(function() { equals(function() {
return obj != obj2; return obj !== obj2;
}, true); }, true);
} }
Base.each(keys, function(key) { Base.each(keys, function(key) {
@ -203,14 +203,14 @@ function compareSegmentLists(segmentList, segmentList2, checkIdentity) {
} }
} }
function compareItems(item, item2, checkIdentity) { function compareItems(item, item2, cloned, checkIdentity) {
if (checkIdentity) { if (checkIdentity) {
equals(function() { equals(function() {
return item != item2; return item !== item2;
}, true); }, true);
equals(function() { equals(function() {
return item.id != item2.id; return item.id !== item2.id;
}, true); }, true);
} }
@ -219,14 +219,21 @@ function compareItems(item, item2, checkIdentity) {
}, true); }, true);
var itemProperties = ['opacity', 'locked', 'visible', 'blendMode', 'name', var itemProperties = ['opacity', 'locked', 'visible', 'blendMode', 'name',
'selected', 'clipMask']; 'selected', 'clipMask'];
Base.each(itemProperties, function(key) { Base.each(itemProperties, function(key) {
equals(item[key], item2[key], 'compare Item#' + key); var value = item[key];
// When item was cloned and had a name, the name will be versioned
equals(
key == 'name' && cloned && value
? value + ' 1'
: value,
item2[key],
'compare Item#' + key);
}); });
if (checkIdentity) { if (checkIdentity) {
equals(function() { equals(function() {
return item.bounds != item2.bounds; return item.bounds !== item2.bounds;
}, true); }, true);
} }
@ -235,7 +242,7 @@ function compareItems(item, item2, checkIdentity) {
if (checkIdentity) { if (checkIdentity) {
equals(function() { equals(function() {
return item.position != item2.position; return item.position !== item2.position;
}, true); }, true);
} }
@ -245,7 +252,7 @@ function compareItems(item, item2, checkIdentity) {
if (item.matrix) { if (item.matrix) {
if (checkIdentity) { if (checkIdentity) {
equals(function() { equals(function() {
return item.matrix != item2.matrix; return item.matrix !== item2.matrix;
}, true); }, true);
} }
equals(item.matrix.toString(), item2.matrix.toString(), equals(item.matrix.toString(), item2.matrix.toString(),
@ -289,7 +296,7 @@ function compareItems(item, item2, checkIdentity) {
if (item._canvas) { if (item._canvas) {
if (checkIdentity) { if (checkIdentity) {
equals(function() { equals(function() {
return item._canvas != item2._canvas; return item._canvas !== item2._canvas;
}, true); }, true);
} }
} }
@ -313,7 +320,7 @@ function compareItems(item, item2, checkIdentity) {
if (item instanceof PointText) { if (item instanceof PointText) {
if (checkIdentity) { if (checkIdentity) {
equals(function() { equals(function() {
return item.point != item2.point; return item.point !== item2.point;
}, true); }, true);
} }
equals(item.point.toString(), item2.point.toString(), equals(item.point.toString(), item2.point.toString(),
@ -331,7 +338,7 @@ function compareItems(item, item2, checkIdentity) {
return item.children.length == item2.children.length; return item.children.length == item2.children.length;
}, true); }, true);
for (var i = 0, l = item.children.length; i < l; i++) { for (var i = 0, l = item.children.length; i < l; i++) {
compareItems(item.children[i], item2.children[i], checkIdentity); compareItems(item.children[i], item2.children[i], cloned, checkIdentity);
} }
} }
} }

View file

@ -27,7 +27,7 @@ function cloneAndCompare(item) {
return copy.parent.children[copy.name] == copy; return copy.parent.children[copy.name] == copy;
}, true); }, true);
} }
compareItems(copy, item, true); compareItems(item, copy, true, true);
// Remove the cloned item to restore the document: // Remove the cloned item to restore the document:
copy.remove(); copy.remove();
} }
@ -140,7 +140,7 @@ test('Symbol#clone()', function() {
path.selected = true; path.selected = true;
var symbol = new Symbol(path); var symbol = new Symbol(path);
var copy = symbol.clone(); var copy = symbol.clone();
compareItems(copy.definition, symbol.definition); compareItems(symbol.definition, copy.definition);
equals(function() { equals(function() {
return symbol.project == copy.project; return symbol.project == copy.project;
}, true); }, true);

View file

@ -21,7 +21,7 @@ module('SvgImport');
test('Import complex CompoundPath and clone', function() { test('Import complex CompoundPath and clone', function() {
var svg = createSvg('<path id="path" fill="red" d="M4,14h20v-2H4V14z M15,26h7v-2h-7V26z M15,22h9v-2h-9V22z M15,18h9v-2h-9V18z M4,26h9V16H4V26z M28,10V6H0v22c0,0,0,4,4,4 h25c0,0,3-0.062,3-4V10H28z M4,30c-2,0-2-2-2-2V8h24v20c0,0.921,0.284,1.558,0.676,2H4z"/>;'); var svg = createSvg('<path id="path" fill="red" d="M4,14h20v-2H4V14z M15,26h7v-2h-7V26z M15,22h9v-2h-9V22z M15,18h9v-2h-9V18z M4,26h9V16H4V26z M28,10V6H0v22c0,0,0,4,4,4 h25c0,0,3-0.062,3-4V10H28z M4,30c-2,0-2-2-2-2V8h24v20c0,0.921,0.284,1.558,0.676,2H4z"/>;');
var item = paper.project.importSvg(svg.getElementById('path')); var item = paper.project.importSvg(svg.getElementById('path'));
compareItems(item, item.clone()); compareItems(item, item.clone(), true, true);
}); });
test('make an svg line', function() { test('make an svg line', function() {