Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/SVGPathData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export class SVGPathData extends TransformableSVG {
return SVGPathData.encode(this.commands);
}

/**
* Copies every command, so transforming the copy leaves this path data
* unchanged
*/
clone() {
return new SVGPathData(this.commands.map(SVGPathDataTransformer.CLONE()));
}

getBounds() {
const boundsTransform = SVGPathDataTransformer.CALCULATE_BOUNDS();

Expand Down
30 changes: 30 additions & 0 deletions src/tests/clone.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, test, expect } from '@jest/globals';
import { SVGPathData } from '../index.js';

const PATH = 'm10 10q10 0 10 10t10 10s10 10 20 0a5 5 0 0 1 10 0z';

describe('Cloning path data', () => {
test('encodes the same path', () => {
const pathData = new SVGPathData(PATH);

expect(pathData.clone().encode()).toEqual(pathData.encode());
});

test('transforming the clone leaves the original unchanged', () => {
const pathData = new SVGPathData(PATH);
const before = pathData.encode();

pathData.clone().toAbs().normalizeST().qtToC().aToC().translate(5, 5);

expect(pathData.encode()).toEqual(before);
});

test('transforming the original leaves the clone unchanged', () => {
const pathData = new SVGPathData(PATH);
const clone = pathData.clone();

pathData.toAbs().normalizeST().qtToC().aToC().translate(5, 5);

expect(clone.encode()).toEqual(new SVGPathData(PATH).encode());
});
});