Skip to content
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

Avoid unwanted mutation to passed objects array to Group constructor #9151

Merged
merged 3 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- patch(): Avoid unwanted mutation to passed objects array to Group constructor [#9151](https://github.com/fabricjs/fabric.js/pull/9151)
- patch(): ActiveSelection initialization + types [#9143](https://github.com/fabricjs/fabric.js/pull/9143)
- chore(TS): BREAKING remove canvas.interactive, added typings for canvas options [#9140](https://github.com/fabricjs/fabric.js/pull/9140)
- chore(TS): BREAKING PREVIOUS BETA mv + rename `TProps` => `TOptions` [#9139](https://github.com/fabricjs/fabric.js/pull/9139)
Expand Down
15 changes: 15 additions & 0 deletions src/shapes/Group.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Group } from './Group';
import { FabricObject } from './Object/FabricObject';

describe('Group', () => {
it('avoid mutations to passed objects array', () => {
const objs = [new FabricObject(), new FabricObject()];
const group = new Group(objs);

group.add(new FabricObject());

expect(group._objects).not.toBe(objs);
expect(objs).toHaveLength(2);
expect(group._objects).toHaveLength(3);
});
});
2 changes: 1 addition & 1 deletion src/shapes/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class Group extends createCollectionMixin(
objectsRelativeToGroup?: boolean
) {
super();
this._objects = objects;
this._objects = objects.slice(); // Avoid unwanted mutations of Collection to affect the caller
ShaMan123 marked this conversation as resolved.
Show resolved Hide resolved
this.__objectMonitor = this.__objectMonitor.bind(this);
this.__objectSelectionTracker = this.__objectSelectionMonitor.bind(
this,
Expand Down