From d26d35e06e4ed42d8ee6133c9cfc554478c6a22d Mon Sep 17 00:00:00 2001 From: Erkan Celme Date: Fri, 27 Sep 2024 13:24:10 -0500 Subject: [PATCH 1/3] Added token support for cookies --- .../src/services/tokenService/TokenService.ts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/search-parts/src/services/tokenService/TokenService.ts b/search-parts/src/services/tokenService/TokenService.ts index 7edf5406..63ddce16 100644 --- a/search-parts/src/services/tokenService/TokenService.ts +++ b/search-parts/src/services/tokenService/TokenService.ts @@ -139,6 +139,7 @@ export class TokenService implements ITokenService { inputString = this.replaceGroupTokens(inputString); inputString = this.replaceLegacyPageContextTokens(inputString); inputString = await this.replaceHubTokens(inputString); + inputString = this.replaceCookieTokens(inputString); inputString = inputString.replace(/\{TenantUrl\}/gi, `https://` + window.location.host); @@ -631,6 +632,42 @@ export class TokenService implements ITokenService { return inputString; } + /** + * Resolve current cookie related tokens + * @param inputString the input string containing tokens + */ + private replaceCookieTokens(inputString: string): string { + + const cookieRegExp = /\{(?:Cookie)\.(.*?)\}/gi; + let matches = cookieRegExp.exec(inputString); + + if (matches != null) { + + while (matches !== null) { + const cookie = matches[1]; + inputString = inputString.replace(new RegExp(matches[0], "gi"), this.pageContext ? this.getCookieValue(cookie) : ''); + matches = cookieRegExp.exec(inputString); + } + } + + return inputString; + } + + /** + * Return the cookie value or '*' if cookie is empty or doesn't exist + * @param cookieName name of the cookie + */ + private getCookieValue(cookieName: string): string { + const name = cookieName + "="; + const decodedCookie = decodeURIComponent(document.cookie); + const ca = decodedCookie.split(';'); + let val = ca.filter(cookie => cookie.indexOf(name) > 0)[0]?.split("=")[1]; + if (!val) { + val = '*'; + } + return val; + } + private replaceAndOrOperator(inputString: string) { // Example match: {|owstaxidmetadataalltagsinfo:{Page..TermID}} From 0f4b359116545a17733f29d62d03e1738a55696c Mon Sep 17 00:00:00 2001 From: Erkan Celme Date: Fri, 27 Sep 2024 15:36:20 -0500 Subject: [PATCH 2/3] Cookie token support --- .../src/services/tokenService/TokenService.ts | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/search-parts/src/services/tokenService/TokenService.ts b/search-parts/src/services/tokenService/TokenService.ts index 63ddce16..b577a0d7 100644 --- a/search-parts/src/services/tokenService/TokenService.ts +++ b/search-parts/src/services/tokenService/TokenService.ts @@ -638,32 +638,37 @@ export class TokenService implements ITokenService { */ private replaceCookieTokens(inputString: string): string { - const cookieRegExp = /\{(?:Cookie)\.(.*?)\}/gi; + const cookieRegExp = /\{(?:\?){0,1}(?:Cookie)\.(.*?)\}/gi; + let modifiedString = inputString; let matches = cookieRegExp.exec(inputString); - if (matches != null) { - - while (matches !== null) { - const cookie = matches[1]; - inputString = inputString.replace(new RegExp(matches[0], "gi"), this.pageContext ? this.getCookieValue(cookie) : ''); - matches = cookieRegExp.exec(inputString); + while (matches !== null) { + const cookieName = matches[1]; + const cookieValue = this.getCookieValue(cookieName); + if (cookieValue) { + modifiedString = modifiedString.replace(matches[0], cookieValue); + } + else if (matches[0].includes("?")) { + // If Cookie Token is specified like this, {?Cookie.CookieName}, it is removed if the Cookie doesn't exist + modifiedString = modifiedString.replace(matches[0], ""); } + matches = cookieRegExp.exec(inputString); } - return inputString; + return modifiedString; } /** - * Return the cookie value or '*' if cookie is empty or doesn't exist + * Return the cookie value * @param cookieName name of the cookie */ private getCookieValue(cookieName: string): string { const name = cookieName + "="; const decodedCookie = decodeURIComponent(document.cookie); const ca = decodedCookie.split(';'); - let val = ca.filter(cookie => cookie.indexOf(name) > 0)[0]?.split("=")[1]; + let val = ca.filter(cookie => cookie.includes(name))[0]?.split("=")[1]; if (!val) { - val = '*'; + val = ''; } return val; } From 7844dd599a6905e783921354eaf6fdb08e362114 Mon Sep 17 00:00:00 2001 From: Erkan Celme Date: Fri, 27 Sep 2024 15:41:18 -0500 Subject: [PATCH 3/3] Cookie token documentation --- docs/usage/search-results/tokens.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/usage/search-results/tokens.md b/docs/usage/search-results/tokens.md index 0174f93f..6eb062b6 100644 --- a/docs/usage/search-results/tokens.md +++ b/docs/usage/search-results/tokens.md @@ -50,6 +50,7 @@ Tokens related to connected Web Parts in the Search Results. |**{PageContext.<PropertyName>}** | Resolves current SPFx page context related tokens. You can use deep paths here to access properties. Ex: `{PageContext.site.absoluteUrl}`. Use the debug template in the web part to see all tokens available under the `context` node.
| **{LegacyPageContext.<PropertyName>}** | Resolves current SPFx legacy page context related tokens. You can use deep paths here to access properties. Ex: `{LegacyPageContext.aadTenantId}`.
|**{QueryString.<ParameterName>}**
| A value from a query string in the URL of the current page. For example, if the URL of the current page contains a query string such as ItemNumber=567, you could obtain the value 567 by specifying `{QueryString.ItemNumber}`.
Use `{?QueryString.Parameter}` if you want the part to be omitted in case the query string parameter is not present.| +|**{Cookie.<CookieName>}**
| The value from a cookie. For example, if a cookie called Current-Language contains a value such as en-us, you could obtain the value en-us by specifying `{Cookie.Current-Language}`.
Use `{?Cookie.CookieName}` if you want the part to be omitted in case the cookie is empty or is not present.| |**{CurrentDisplayLanguage}**
|The current display language based on MUI in _ll-cc format_.
| |**{CurrentDisplayLCID}**
|Numeric value of the current display language based on MUI in _ll-cc format_.
| |**{TenantUrl}**
|URL of the tenant (root site)
|