From e54f50dbc21d434a8afe79641247ea45831f0706 Mon Sep 17 00:00:00 2001 From: Ivan Tsenilov Date: Sun, 25 Sep 2022 11:52:47 +0300 Subject: [PATCH 1/2] fix: SvgPath.parse fails on negative values --- src/svg/SvgPath.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/svg/SvgPath.ts b/src/svg/SvgPath.ts index 04a300f1..6eac827c 100644 --- a/src/svg/SvgPath.ts +++ b/src/svg/SvgPath.ts @@ -255,7 +255,7 @@ export class SvgPath { parse(path: string) { // Parsing the SVG path string into an array of arrays [['M', '10', '10'], ['L', '100', '100']] const chunks = path - .replace(/([A-Za-z])([0-9])/g, '$1 $2') + .replace(/([A-Za-z])(-?[0-9])/g, '$1 $2') .replace(/([0-9])([A-Za-z])/g, '$1 $2') .split(/[\s,]+/) .reduce((result, pathElement) => { From 715f10b5fff146c95ee29a67c6ae7f0fba21741d Mon Sep 17 00:00:00 2001 From: Ivan Tsenilov Date: Mon, 26 Sep 2022 16:44:16 +0300 Subject: [PATCH 2/2] test: add test with negative values --- src/svg/SvgPath.spec.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/svg/SvgPath.spec.ts b/src/svg/SvgPath.spec.ts index 7285e96f..e3aab8b2 100644 --- a/src/svg/SvgPath.spec.ts +++ b/src/svg/SvgPath.spec.ts @@ -191,5 +191,17 @@ describe('Svg', () => { expect(paths[1].pathElements.length).toBe(4); expect(paths[1].pathElements[0].command).toBe('M'); }); + + it('should correctly parse negative values', () => { + const paths: any = new SvgPath().parse('M-10,10L-100,-100'); + expect(paths.pathElements.length).toBe(2); + expect(paths.pathElements[0].command).toBe('M'); + expect(paths.pathElements[1].command).toBe('L'); + + expect(paths.pathElements[0].x).toBe(-10); + expect(paths.pathElements[0].y).toBe(10); + expect(paths.pathElements[1].x).toBe(-100); + expect(paths.pathElements[1].y).toBe(-100); + }); }); });