From 6a66786a63853efc0c11077bc22b3d9eccf2381a Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Fri, 15 Dec 2023 16:59:19 -0300 Subject: [PATCH 1/8] fix: started to fix method for calc in utils parser --- libs/shared/src/lib/utils/parser/utils.ts | 58 +++++++++++------------ 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/libs/shared/src/lib/utils/parser/utils.ts b/libs/shared/src/lib/utils/parser/utils.ts index e7b00f1419..acd766c4a4 100644 --- a/libs/shared/src/lib/utils/parser/utils.ts +++ b/libs/shared/src/lib/utils/parser/utils.ts @@ -9,7 +9,7 @@ const DATA_PREFIX = '{{data.'; /** Prefix for aggregation keys */ const AGGREGATION_PREFIX = '{{aggregation.'; /** Prefix for calc keys */ -const CALC_PREFIX = '{{calc.'; +const CALC_PREFIX = '{{calc'; /** Prefix for avatar keys */ const AVATAR_PREFIX = '{{avatars.'; /** Allowed image extensions */ @@ -83,7 +83,11 @@ export const parseHtml = ( options.aggregation ); } - return applyOperations(formattedHtml); + + console.log("formattedHtml = ", formattedHtml); + const a = applyOperations(formattedHtml); + console.log("a = ", a); + return a; } else { return applyOperations(formattedHtml); } @@ -486,26 +490,26 @@ export const getFieldsValue = (record: any) => { * @returns The html body with the calculated result of the functions */ const applyOperations = (html: string): string => { + // // Define the regex pattern to match content inside {{calc.}} expressions with a nested tag + const pattern = new RegExp( + `${CALC_PREFIX}\\.(.*?)]*>(.*?)(.*?)}}`, + 'g' + ); + // Use replace method with a callback function to handle all occurrences + const modifiedHtml = html.replace( + pattern, + (_, prefix, capturedContent, suffix) => { + return `${prefix}${capturedContent}${suffix}`; + } + ); + console.log(modifiedHtml); const regex = new RegExp( `${CALC_PREFIX}(\\w+)\\(([^\\)]+)\\)${PLACEHOLDER_SUFFIX}`, 'gm' ); - - // To identify and remove span with style elements to avoid breaking calc functions - const spanRegex = /]*>([\s\S]*?)<\/span>/g; - const spanElements: string[] = []; - // Clean HTML don't have span and styles - const cleanHtml = html.replace(spanRegex, (match, spanContent) => { - // Save the removed span element - spanElements.push(match); - // Replace the span element with its content - return spanContent; - }); - - let parsedHtml = cleanHtml; - let result = regex.exec(cleanHtml); - // Track the index of the saved span elements - let spanIndex = 0; + let parsedHtml = modifiedHtml; + let result = regex.exec(parsedHtml); + console.log("result = ", result); while (result !== null) { // get the function const calcFunc = get(calcFunctions, result[1]); @@ -515,27 +519,19 @@ const applyOperations = (html: string): string => { .split(';') .map((arg) => arg.replace(/[\s,]/gm, '')); // apply the function + console.log("args = ", args); let resultText; try { resultText = calcFunc.call(...args); + console.log("resultText = ", resultText); } catch (err: any) { resultText = ` ${err.name}`; } - if (spanElements.length > spanIndex) { - // Retrieve the saved span element - const spanElement = spanElements[spanIndex]; - // Replace the html with the calculated result inside the saved span element with the style - parsedHtml = parsedHtml.replace( - result[0], - spanElement.replace(`'>.*()`, `'>${resultText}$1`) - ); - } else { - parsedHtml = parsedHtml.replace(result[0], resultText); - } - spanIndex++; + parsedHtml = parsedHtml.replace(result[0], resultText); } - result = regex.exec(cleanHtml); + result = regex.exec(parsedHtml); } + console.log("parsedHtml = ", parsedHtml); return parsedHtml; }; From 19a8290843839df4daf751ca21c08e2be897ddd5 Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Fri, 15 Dec 2023 17:01:32 -0300 Subject: [PATCH 2/8] lint fixed --- libs/shared/src/lib/utils/parser/utils.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libs/shared/src/lib/utils/parser/utils.ts b/libs/shared/src/lib/utils/parser/utils.ts index acd766c4a4..ac2a9f07ac 100644 --- a/libs/shared/src/lib/utils/parser/utils.ts +++ b/libs/shared/src/lib/utils/parser/utils.ts @@ -84,9 +84,9 @@ export const parseHtml = ( ); } - console.log("formattedHtml = ", formattedHtml); + console.log('formattedHtml = ', formattedHtml); const a = applyOperations(formattedHtml); - console.log("a = ", a); + console.log('a = ', a); return a; } else { return applyOperations(formattedHtml); @@ -509,7 +509,7 @@ const applyOperations = (html: string): string => { ); let parsedHtml = modifiedHtml; let result = regex.exec(parsedHtml); - console.log("result = ", result); + console.log('result = ', result); while (result !== null) { // get the function const calcFunc = get(calcFunctions, result[1]); @@ -519,11 +519,11 @@ const applyOperations = (html: string): string => { .split(';') .map((arg) => arg.replace(/[\s,]/gm, '')); // apply the function - console.log("args = ", args); + console.log('args = ', args); let resultText; try { resultText = calcFunc.call(...args); - console.log("resultText = ", resultText); + console.log('resultText = ', resultText); } catch (err: any) { resultText = ` ${err.name}`; } @@ -531,7 +531,7 @@ const applyOperations = (html: string): string => { } result = regex.exec(parsedHtml); } - console.log("parsedHtml = ", parsedHtml); + console.log('parsedHtml = ', parsedHtml); return parsedHtml; }; From c1811094b3dd61db7ecd938540214260de171eed Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Mon, 18 Dec 2023 10:57:38 -0300 Subject: [PATCH 3/8] fixing regex const --- libs/shared/src/lib/utils/parser/utils.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libs/shared/src/lib/utils/parser/utils.ts b/libs/shared/src/lib/utils/parser/utils.ts index ac2a9f07ac..3188ce9140 100644 --- a/libs/shared/src/lib/utils/parser/utils.ts +++ b/libs/shared/src/lib/utils/parser/utils.ts @@ -499,17 +499,15 @@ const applyOperations = (html: string): string => { const modifiedHtml = html.replace( pattern, (_, prefix, capturedContent, suffix) => { - return `${prefix}${capturedContent}${suffix}`; + return `{{calc.${prefix}${capturedContent}${suffix}}}`; } ); - console.log(modifiedHtml); const regex = new RegExp( - `${CALC_PREFIX}(\\w+)\\(([^\\)]+)\\)${PLACEHOLDER_SUFFIX}`, + `${CALC_PREFIX}\\.(\\w+)\\(([^\\)]+)\\)${PLACEHOLDER_SUFFIX}`, 'gm' ); let parsedHtml = modifiedHtml; let result = regex.exec(parsedHtml); - console.log('result = ', result); while (result !== null) { // get the function const calcFunc = get(calcFunctions, result[1]); @@ -529,7 +527,10 @@ const applyOperations = (html: string): string => { } parsedHtml = parsedHtml.replace(result[0], resultText); } + console.log(parsedHtml); + // const teste = /{{calc\.(\w+)\(([^\)]+)\)}}/gm; result = regex.exec(parsedHtml); + console.log('result = ', result); } console.log('parsedHtml = ', parsedHtml); return parsedHtml; From 8e54439f9c262d94279cec3e980ff6b92e12e98c Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Mon, 18 Dec 2023 12:07:16 -0300 Subject: [PATCH 4/8] fix: finished fix calc method --- libs/shared/src/lib/utils/parser/utils.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/libs/shared/src/lib/utils/parser/utils.ts b/libs/shared/src/lib/utils/parser/utils.ts index 3188ce9140..1400a29949 100644 --- a/libs/shared/src/lib/utils/parser/utils.ts +++ b/libs/shared/src/lib/utils/parser/utils.ts @@ -9,7 +9,7 @@ const DATA_PREFIX = '{{data.'; /** Prefix for aggregation keys */ const AGGREGATION_PREFIX = '{{aggregation.'; /** Prefix for calc keys */ -const CALC_PREFIX = '{{calc'; +const CALC_PREFIX = '{{calc.'; /** Prefix for avatar keys */ const AVATAR_PREFIX = '{{avatars.'; /** Allowed image extensions */ @@ -492,18 +492,18 @@ export const getFieldsValue = (record: any) => { const applyOperations = (html: string): string => { // // Define the regex pattern to match content inside {{calc.}} expressions with a nested tag const pattern = new RegExp( - `${CALC_PREFIX}\\.(.*?)]*>(.*?)(.*?)}}`, + `${CALC_PREFIX}(.*?)]*>(.*?)(.*?)}}`, 'g' ); // Use replace method with a callback function to handle all occurrences const modifiedHtml = html.replace( pattern, (_, prefix, capturedContent, suffix) => { - return `{{calc.${prefix}${capturedContent}${suffix}}}`; + return `${CALC_PREFIX}${prefix}${capturedContent}${suffix}${PLACEHOLDER_SUFFIX}`; } ); const regex = new RegExp( - `${CALC_PREFIX}\\.(\\w+)\\(([^\\)]+)\\)${PLACEHOLDER_SUFFIX}`, + `${CALC_PREFIX}(\\w+)\\(([^\\)]+)\\)${PLACEHOLDER_SUFFIX}`, 'gm' ); let parsedHtml = modifiedHtml; @@ -517,22 +517,20 @@ const applyOperations = (html: string): string => { .split(';') .map((arg) => arg.replace(/[\s,]/gm, '')); // apply the function - console.log('args = ', args); let resultText; try { resultText = calcFunc.call(...args); - console.log('resultText = ', resultText); } catch (err: any) { resultText = ` ${err.name}`; } parsedHtml = parsedHtml.replace(result[0], resultText); } - console.log(parsedHtml); - // const teste = /{{calc\.(\w+)\(([^\)]+)\)}}/gm; + const regex = new RegExp( + `${CALC_PREFIX}(\\w+)\\(([^\\)]+)\\)${PLACEHOLDER_SUFFIX}`, + 'gm' + ); result = regex.exec(parsedHtml); - console.log('result = ', result); } - console.log('parsedHtml = ', parsedHtml); return parsedHtml; }; From 2ea7df7b49ce28535bd1752e11a6cd44e86b28f2 Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Mon, 18 Dec 2023 12:08:08 -0300 Subject: [PATCH 5/8] removed console.log --- libs/shared/src/lib/utils/parser/utils.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/libs/shared/src/lib/utils/parser/utils.ts b/libs/shared/src/lib/utils/parser/utils.ts index 1400a29949..a0e6dffd26 100644 --- a/libs/shared/src/lib/utils/parser/utils.ts +++ b/libs/shared/src/lib/utils/parser/utils.ts @@ -83,10 +83,7 @@ export const parseHtml = ( options.aggregation ); } - - console.log('formattedHtml = ', formattedHtml); const a = applyOperations(formattedHtml); - console.log('a = ', a); return a; } else { return applyOperations(formattedHtml); From 4717cd2af3faa91af51ad41f2eba207bca6dc12f Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Mon, 18 Dec 2023 15:15:45 -0300 Subject: [PATCH 6/8] fix: added calc inside span style if it exists --- libs/shared/src/lib/utils/parser/utils.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/libs/shared/src/lib/utils/parser/utils.ts b/libs/shared/src/lib/utils/parser/utils.ts index a0e6dffd26..291da1a718 100644 --- a/libs/shared/src/lib/utils/parser/utils.ts +++ b/libs/shared/src/lib/utils/parser/utils.ts @@ -489,13 +489,22 @@ export const getFieldsValue = (record: any) => { const applyOperations = (html: string): string => { // // Define the regex pattern to match content inside {{calc.}} expressions with a nested tag const pattern = new RegExp( - `${CALC_PREFIX}(.*?)]*>(.*?)(.*?)}}`, + `${CALC_PREFIX}(.*?)]*>(.*?)(.*?)${PLACEHOLDER_SUFFIX}`, 'g' ); // Use replace method with a callback function to handle all occurrences const modifiedHtml = html.replace( pattern, - (_, prefix, capturedContent, suffix) => { + (fullMatch, prefix, capturedContent, suffix) => { + // span regex to match span content inside fullMatch + const spanRegex = /(.*?)<\/span>/; + const match = spanRegex.exec(fullMatch); + if (match && match[0]) { + // get the span and its style attributes + const spanPart = match[0].split('>')[0] + '>'; + // return calc inside the span to apply style + return `${spanPart}${CALC_PREFIX}${prefix}${capturedContent}${suffix}${PLACEHOLDER_SUFFIX}`; + } return `${CALC_PREFIX}${prefix}${capturedContent}${suffix}${PLACEHOLDER_SUFFIX}`; } ); From ec7a58c926e735767aae1c060cd67fbc8a9beb23 Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Mon, 18 Dec 2023 16:34:50 -0300 Subject: [PATCH 7/8] fix: regex getting two calc --- libs/shared/src/lib/utils/parser/utils.ts | 35 +++++++++++------------ 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/libs/shared/src/lib/utils/parser/utils.ts b/libs/shared/src/lib/utils/parser/utils.ts index 291da1a718..c04ae252d9 100644 --- a/libs/shared/src/lib/utils/parser/utils.ts +++ b/libs/shared/src/lib/utils/parser/utils.ts @@ -487,27 +487,24 @@ export const getFieldsValue = (record: any) => { * @returns The html body with the calculated result of the functions */ const applyOperations = (html: string): string => { - // // Define the regex pattern to match content inside {{calc.}} expressions with a nested tag - const pattern = new RegExp( - `${CALC_PREFIX}(.*?)]*>(.*?)(.*?)${PLACEHOLDER_SUFFIX}`, - 'g' - ); + // // Define the regex pattern to match all {{calc. + const pattern = new RegExp(`${CALC_PREFIX}.*?${PLACEHOLDER_SUFFIX}`, 'g'); // Use replace method with a callback function to handle all occurrences - const modifiedHtml = html.replace( - pattern, - (fullMatch, prefix, capturedContent, suffix) => { - // span regex to match span content inside fullMatch - const spanRegex = /(.*?)<\/span>/; - const match = spanRegex.exec(fullMatch); - if (match && match[0]) { - // get the span and its style attributes - const spanPart = match[0].split('>')[0] + '>'; - // return calc inside the span to apply style - return `${spanPart}${CALC_PREFIX}${prefix}${capturedContent}${suffix}${PLACEHOLDER_SUFFIX}`; - } - return `${CALC_PREFIX}${prefix}${capturedContent}${suffix}${PLACEHOLDER_SUFFIX}`; + const modifiedHtml = html.replace(pattern, (fullMatch) => { + // span regex to match span content inside fullMatch + const spanRegex = /]*>(.*?)<\/span>/g; + const match = spanRegex.exec(fullMatch); + // verify if have inside {{calc. + if (match && match[0]) { + // remove the span and replace for the value inside span + const cleanedSpan = fullMatch.replace(spanRegex, match[1]); + // get the span and its style attributes + const spanPart = match[0].split('>')[0] + '>'; + // return calc inside the span to apply style + return `${spanPart}${cleanedSpan}`; } - ); + return fullMatch; + }); const regex = new RegExp( `${CALC_PREFIX}(\\w+)\\(([^\\)]+)\\)${PLACEHOLDER_SUFFIX}`, 'gm' From d22229243774df072f1a7deade5f7b115602b510 Mon Sep 17 00:00:00 2001 From: RenzoPrats Date: Mon, 18 Dec 2023 16:46:55 -0300 Subject: [PATCH 8/8] refactored the code --- libs/shared/src/lib/utils/parser/utils.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libs/shared/src/lib/utils/parser/utils.ts b/libs/shared/src/lib/utils/parser/utils.ts index c04ae252d9..e849bebea5 100644 --- a/libs/shared/src/lib/utils/parser/utils.ts +++ b/libs/shared/src/lib/utils/parser/utils.ts @@ -83,8 +83,7 @@ export const parseHtml = ( options.aggregation ); } - const a = applyOperations(formattedHtml); - return a; + return applyOperations(formattedHtml); } else { return applyOperations(formattedHtml); }