From 18781c9c28c6e404c1ee626db71abc7d3b46baa9 Mon Sep 17 00:00:00 2001 From: Maciej Barelkowski Date: Tue, 12 Nov 2024 16:54:59 +0100 Subject: [PATCH 1/2] fix: limit overly permissive regex range This fragment of code deals with letters so it should not match additional characters. --- lib/util/RenderUtil.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util/RenderUtil.js b/lib/util/RenderUtil.js index 7bb3af13e..1b375d044 100644 --- a/lib/util/RenderUtil.js +++ b/lib/util/RenderUtil.js @@ -20,7 +20,7 @@ import { * @return {string} */ export function componentsToPath(elements) { - return elements.flat().join(',').replace(/,?([A-z]),?/g, '$1'); + return elements.flat().join(',').replace(/,?([A-Za-z]),?/g, '$1'); } /** From 06db3c76c2c1729dd75b55cd3c0f80e29fbc787a Mon Sep 17 00:00:00 2001 From: Maciej Barelkowski Date: Thu, 14 Nov 2024 11:18:47 +0100 Subject: [PATCH 2/2] test: verify componentsToPath --- test/spec/util/RenderUtilSpec.js | 69 ++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/test/spec/util/RenderUtilSpec.js b/test/spec/util/RenderUtilSpec.js index 6c5be3ffa..c200f3736 100644 --- a/test/spec/util/RenderUtilSpec.js +++ b/test/spec/util/RenderUtilSpec.js @@ -1,4 +1,5 @@ import { + componentsToPath, createLine, updateLine } from 'lib/util/RenderUtil'; @@ -113,4 +114,72 @@ describe('util/RenderUtil', function() { }); + + describe('#componentsToPath', function() { + + // test cases derived from bpmn-js BpmnRenderUtil + const testCases = [ + { + name: 'circle', + components: [ + [ 'M', 0, 0 ], + [ 'm', 0, -20 ], + [ 'a', 20, 20, 0, 1, 1, 0, 2 * 20 ], + [ 'a', 20, 20, 0, 1, 1, 0, -2 * 20 ], + [ 'z' ] + ], + expected: 'M0,0m0,-20a20,20,0,1,1,0,40a20,20,0,1,1,0,-40z' + }, + { + name: 'roundRect', + components: [ + [ 'M', 100 + 5, 10 ], + [ 'l', 100 - 5 * 2, 0 ], + [ 'a', 5, 5, 0, 0, 1, 5, 5 ], + [ 'l', 0, 80 - 5 * 2 ], + [ 'a', 5, 5, 0, 0, 1, -5, 5 ], + [ 'l', 5 * 2 - 100, 0 ], + [ 'a', 5, 5, 0, 0, 1, -5, -5 ], + [ 'l', 0, 5 * 2 - 80 ], + [ 'a', 5, 5, 0, 0, 1, 5, -5 ], + [ 'z' ] + ], + expected: 'M105,10l90,0a5,5,0,0,1,5,5l0,70a5,5,0,0,1,-5,5l-90,0a5,5,0,0,1,-5,-5l0,-70a5,5,0,0,1,5,-5z' + }, + { + name: 'diamond', + components: [ + [ 'M', 100, 0 ], + [ 'l', 100, 100 ], + [ 'l', -100, 100 ], + [ 'l', -100, -100 ], + [ 'z' ] + ], + expected: 'M100,0l100,100l-100,100l-100,-100z' + }, + { + name: 'rect', + components: [ + [ 'M', 100, 0 ], + [ 'l', 100, 0 ], + [ 'l', 0, 100 ], + [ 'l', -100, 0 ], + [ 'z' ] + ], + expected: 'M100,0l100,0l0,100l-100,0z' + } + ]; + + for (const testCase of testCases) { + + it(`should handle ${testCase.name}`, function() { + + // when + const output = componentsToPath(testCase.components); + + // then + expect(output).to.eql(testCase.expected); + }); + } + }); });