From b1a0b6de43d251b9a5a00759ea403869627d3ce7 Mon Sep 17 00:00:00 2001 From: cg-scorpio <90409381+cg-scorpio@users.noreply.github.com> Date: Wed, 27 Jul 2022 09:55:47 +0200 Subject: [PATCH] Fix: avoid to generate the same key for two different imported svg (#824) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix: avoid to generate the same key for two different imported svg * fix linter issues Co-authored-by: Cédric Godefroy Co-authored-by: JFH <20402845+jfhenon@users.noreply.github.com> --- src/svgcanvas/svg-exec.js | 6 +++--- src/svgcanvas/utilities.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/svgcanvas/svg-exec.js b/src/svgcanvas/svg-exec.js index 3586f742..e0a6fb54 100644 --- a/src/svgcanvas/svg-exec.js +++ b/src/svgcanvas/svg-exec.js @@ -17,11 +17,11 @@ import { preventClickDefault, toXml, getStrokedBBoxDefaultVisible, - encode64, createObjectURL, dataURLToObjectURL, walkTree, - getBBox as utilsGetBBox + getBBox as utilsGetBBox, + hashCode } from './utilities.js' import { transformPoint, transformListToTransform } from './math.js' import { convertUnit, shortFloat, convertToNum } from '../common/units.js' @@ -633,7 +633,7 @@ const importSvgString = (xmlString) => { let useEl try { // Get unique ID - const uid = encode64(xmlString.length + xmlString).substr(0, 32) + const uid = hashCode(xmlString) let useExisting = false // Look for symbol and make sure symbol exists in image diff --git a/src/svgcanvas/utilities.js b/src/svgcanvas/utilities.js index 68358c51..2a2d00db 100644 --- a/src/svgcanvas/utilities.js +++ b/src/svgcanvas/utilities.js @@ -144,6 +144,23 @@ export function decode64 (input) { return decodeUTF8(window.atob(input)) } +/** + * Compute a hashcode from a given string + * @param word : the string, we want to compute the hashcode + * @returns {number}: Hascode of the given string + */ +export function hashCode (word) { + let hash = 0 + let chr + if (word.length === 0) return hash + for (let i = 0; i < word.length; i++) { + chr = word.charCodeAt(i) + hash = ((hash << 5) - hash) + chr + hash |= 0 // Convert to 32bit integer + } + return hash +} + /** * @function module:utilities.decodeUTF8 * @param {string} argString