diff --git a/src/core/text/TextStyle.js b/src/core/text/TextStyle.js index c682a36..f9f8b68 100644 --- a/src/core/text/TextStyle.js +++ b/src/core/text/TextStyle.js @@ -93,7 +93,9 @@ { this.styleID = 0; - Object.assign(this, defaultStyle, style); + this.reset(); + + deepCopyProperties(this, style, style); } /** @@ -106,10 +108,7 @@ { const clonedProperties = {}; - for (const key in defaultStyle) - { - clonedProperties[key] = this[key]; - } + deepCopyProperties(clonedProperties, this, defaultStyle); return new TextStyle(clonedProperties); } @@ -119,7 +118,7 @@ */ reset() { - Object.assign(this, defaultStyle); + deepCopyProperties(this, defaultStyle, defaultStyle); } /** @@ -755,3 +754,20 @@ return true; } + +/** + * Utility function to ensure that object properties are copied by value, and not by reference + * + * @param {Object} target Target object to copy properties into + * @param {Object} source Source object for the proporties to copy + * @param {string} propertyObj Object containing properties names we want to loop over + */ +function deepCopyProperties(target, source, propertyObj) { + for (const prop in propertyObj) { + if (Array.isArray(source[prop])) { + target[prop] = source[prop].slice(); + } else { + target[prop] = source[prop]; + } + } +} diff --git a/src/core/text/TextStyle.js b/src/core/text/TextStyle.js index c682a36..f9f8b68 100644 --- a/src/core/text/TextStyle.js +++ b/src/core/text/TextStyle.js @@ -93,7 +93,9 @@ { this.styleID = 0; - Object.assign(this, defaultStyle, style); + this.reset(); + + deepCopyProperties(this, style, style); } /** @@ -106,10 +108,7 @@ { const clonedProperties = {}; - for (const key in defaultStyle) - { - clonedProperties[key] = this[key]; - } + deepCopyProperties(clonedProperties, this, defaultStyle); return new TextStyle(clonedProperties); } @@ -119,7 +118,7 @@ */ reset() { - Object.assign(this, defaultStyle); + deepCopyProperties(this, defaultStyle, defaultStyle); } /** @@ -755,3 +754,20 @@ return true; } + +/** + * Utility function to ensure that object properties are copied by value, and not by reference + * + * @param {Object} target Target object to copy properties into + * @param {Object} source Source object for the proporties to copy + * @param {string} propertyObj Object containing properties names we want to loop over + */ +function deepCopyProperties(target, source, propertyObj) { + for (const prop in propertyObj) { + if (Array.isArray(source[prop])) { + target[prop] = source[prop].slice(); + } else { + target[prop] = source[prop]; + } + } +} diff --git a/test/core/TextStyle.js b/test/core/TextStyle.js index 49b99ef..1976333 100644 --- a/test/core/TextStyle.js +++ b/test/core/TextStyle.js @@ -50,4 +50,14 @@ expect(style.toFontString()).to.have.string('"Georgia","Arial","sans-serif"'); }); + + it('should not shared array / object references between different instances', function () + { + const defaultStyle = new PIXI.TextStyle(); + const style = new PIXI.TextStyle(); + + expect(defaultStyle.fillGradientStops.length).to.equal(style.fillGradientStops.length); + style.fillGradientStops.push(0); + expect(defaultStyle.fillGradientStops.length).to.not.equal(style.fillGradientStops.length); + }); });