-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TextStyle.setStyle mutates the given style parameter object #6863
Comments
For internal use onlyTo resolve the issue of
var Clone = require('../../utils/object/Clone');
setStyle: function (style, updateText, setDefaults) {
if (updateText === undefined) { updateText = true; }
if (setDefaults === undefined) { setDefaults = false; }
// Clone the style object to avoid mutation
style = Clone(style);
// Avoid type mutation
if (style && style.hasOwnProperty('fontSize') && typeof style.fontSize === 'number') {
style.fontSize = style.fontSize.toString() + 'px';
}
for (var key in propertyMap) {
var value = (setDefaults) ? propertyMap[key][1] : this[key];
if (key === 'wordWrapCallback' || key === 'wordWrapCallbackScope') {
this[key] = GetValue(style, propertyMap[key][0], value);
} else {
this[key] = GetAdvancedValue(style, propertyMap[key][0], value);
}
}
var font = GetValue(style, 'font', null);
if (font !== null) {
this.setFont(font, false);
}
this._font = [ this.fontStyle, this.fontSize, this.fontFamily ].join(' ').trim();
var fill = GetValue(style, 'fill', null);
if (fill !== null) {
this.color = fill;
}
var metrics = GetValue(style, 'metrics', false);
if (metrics) {
this.metrics = {
ascent: GetValue(metrics, 'ascent', 0),
descent: GetValue(metrics, 'descent', 0),
fontSize: GetValue(metrics, 'fontSize', 0)
};
} else if (updateText || !this.metrics) {
this.metrics = MeasureText(this);
}
if (updateText) {
return this.parent.updateText();
} else {
return this.parent;
}
} This change ensures that the original style object is not mutated. References/src/gameobjects/text/TextStyle.js
|
Good bot. |
No need to waste CPU/mem cloning the object, can just handle it a different way. Thank you for submitting this issue. We have fixed this and the fix has been pushed to the |
Wow, that was quick! Thanks a lot :) Looks good to me and works as expected 👍 |
Version
Description
https://github.com/phaserjs/phaser/blob/master/src/gameobjects/text/TextStyle.js
TextStyle.setStyle normalizes the given fontSize by adding "px" if it is just a number. However, the function will mutate the original given "style" parameter, which is a counterintuitive side-effect that is also unnecessary. This makes hard to define global font styles for reuse, since after the first setStyle call a font size of for example 32 (number) will be "32px" (string) afterwards, meaning it cannot be used for calculation of relative font sizes anymore.
The "style" parameter should not be mutated. Cloning it before changing it would fix the problem, I guess a shallow copy would also suffice.
Example Test Code
https://phaser.io/sandbox/zvb8G5ux
The text was updated successfully, but these errors were encountered: