Skip to content

Commit

Permalink
fix(fabric.Gradient): Guard against deep mutation on svg export for c…
Browse files Browse the repository at this point in the history
…olor exports (#8196)
  • Loading branch information
asturur authored Aug 26, 2022
1 parent 16f5bcb commit af947d6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/gradient/gradient.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ export class Gradient<S, T extends GradientType = S extends GradientType ? S : '
const markup = [],
transform = this.gradientTransform ? this.gradientTransform.concat() : iMatrix.concat(),
gradientUnits = this.gradientUnits === 'pixels' ? 'userSpaceOnUse' : 'objectBoundingBox';
// colorStops must be sorted ascending
const colorStops = this.colorStops.concat().sort((a, b) => {
// colorStops must be sorted ascending, and guarded against deep mutations
const colorStops = this.colorStops.map((colorStop) => ({ ...colorStop })).sort((a, b) => {
return a.offset - b.offset;
});

Expand Down Expand Up @@ -274,7 +274,7 @@ export class Gradient<S, T extends GradientType = S extends GradientType ? S : '
* @return {Gradient} Gradient instance
* @see http://www.w3.org/TR/SVG/pservers.html#LinearGradientElement
* @see http://www.w3.org/TR/SVG/pservers.html#RadialGradientElement
*
*
* @example
*
* <linearGradient id="linearGrad1">
Expand Down
6 changes: 5 additions & 1 deletion test/unit/gradient.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,11 @@
fabric.Object.__uid = 0;
var gradient = createRadialGradientSwapped();
var obj = new fabric.Object({ width: 100, height: 100 });
assert.equal(gradient.toSVG(obj), SVG_SWAPPED);
const gradientColorStops = JSON.stringify(gradient.colorStops);
assert.equal(gradient.toSVG(obj), SVG_SWAPPED, 'it exports as expected');
const gradientColorStopsAfterExport = JSON.stringify(gradient.colorStops);
assert.equal(gradient.toSVG(obj), SVG_SWAPPED, 'it exports as expected a second time');
assert.equal(gradientColorStops, gradientColorStopsAfterExport, 'colorstops do not change')
});

QUnit.test('toSVG linear objectBoundingBox', function(assert) {
Expand Down

0 comments on commit af947d6

Please sign in to comment.