From c93926bc4bd1a9ca0e2301af8b7ebf74d318be39 Mon Sep 17 00:00:00 2001 From: Olli Mannevaara Date: Thu, 7 Mar 2024 15:06:06 +0200 Subject: [PATCH] Escape the last quotation mark when uneven number --- .../publications2/publications2.component.ts | 3 --- src/app/portal/services/publication2.service.ts | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/app/portal/components/results/publications2/publications2.component.ts b/src/app/portal/components/results/publications2/publications2.component.ts index 3f411dde1..5c4c30774 100644 --- a/src/app/portal/components/results/publications2/publications2.component.ts +++ b/src/app/portal/components/results/publications2/publications2.component.ts @@ -629,9 +629,6 @@ function splitFields(obj: Record): Record = {}; for (const key in obj) { - // console log key and value - // console.log(key, obj[key]); - if (key === 'q') { result[key] = [obj[key] as string]; } else { diff --git a/src/app/portal/services/publication2.service.ts b/src/app/portal/services/publication2.service.ts index c7becc27e..2c36f64e3 100644 --- a/src/app/portal/services/publication2.service.ts +++ b/src/app/portal/services/publication2.service.ts @@ -591,8 +591,23 @@ export class Publication2Service { } } +function escapeLastQuoteIfOdd(inputString: string): string { + // Count the number of quotation marks in the string + const quoteCount = (inputString.match(/"/g) || []).length; + + // If the count is odd, escape the last quotation mark + if (quoteCount % 2 !== 0) { + const lastQuoteIndex = inputString.lastIndexOf('"'); + // Replace the last quotation mark with an escaped version + inputString = inputString.substring(0, lastQuoteIndex) + '\\"' + inputString.substring(lastQuoteIndex + 1); + } + + return inputString; +} + function matchingTerms(searchParams: SearchParams) { - const q = (searchParams.q ?? [""])[0]; + let q = (searchParams.q ?? [""])[0]; + q = escapeLastQuoteIfOdd(q); if (q === "") { return {