Skip to content

Commit

Permalink
CHANGELOG has been changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Matkovskyi committed Dec 10, 2024
1 parent a8a44b2 commit 1c7df59
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Our versioning strategy is as follows:

* `[nextjs][sitecore-jss-nextjs]` Support for Component Library feature in XMCloud ([#1987](https://github.com/Sitecore/jss/pull/1987))

### 🐛 Bug Fixes

* `[sitecore-jss-nextjs]` Fixed handling of ? inside square brackets [] in regex patterns to prevent incorrect escaping ([#1999](https://github.com/Sitecore/jss/pull/1999))

## 22.3.0 / 22.3.1

### 🐛 Bug Fixes
Expand Down
38 changes: 20 additions & 18 deletions packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ export class RedirectsMiddleware extends MiddlewareBase {
? modifyRedirects.find((redirect: RedirectInfo & { matchedQueryString?: string }) => {
// Modify the redirect pattern to ignore the language prefix in the path
// And escapes non-special "?" characters in a string or regex.
redirect.pattern = this.escapeNonSpecialQuestionMarks(redirect.pattern.replace(RegExp(`^[^]?/${language}/`, 'gi'), ''));
redirect.pattern = this.escapeNonSpecialQuestionMarks(
redirect.pattern.replace(RegExp(`^[^]?/${language}/`, 'gi'), '')
);

// Prepare the redirect pattern as a regular expression, making it more flexible for matching URLs
redirect.pattern = `/^\/${redirect.pattern
Expand Down Expand Up @@ -365,9 +367,9 @@ export class RedirectsMiddleware extends MiddlewareBase {

/**
* Escapes non-special "?" characters in a string or regex.
*
*
* - For regular strings, it escapes all unescaped "?" characters by adding a backslash (`\`).
* - For regex patterns (strings enclosed in `/.../`), it analyzes each "?" to determine if it has special meaning
* - For regex patterns (strings enclosed in `/.../`), it analyzes each "?" to determine if it has special meaning
* (e.g., `?` in `(abc)?`, `.*?`) or is just a literal character. Only literal "?" characters are escaped.
* @param {string} input - The input string or regex pattern.
* @returns {string} - The modified string or regex with non-special "?" characters escaped.
Expand All @@ -377,8 +379,8 @@ export class RedirectsMiddleware extends MiddlewareBase {
const isRegex = input.startsWith('/') && input.endsWith('/'); // Check if the string is a regex

if (!isRegex) {
// If not a regex, escape all unescaped "?" characters
return input.replace(regexPattern, '\\?');
// If not a regex, escape all unescaped "?" characters
return input.replace(regexPattern, '\\?');
}

// If it's a regex, analyze each "?" character
Expand All @@ -387,21 +389,21 @@ export class RedirectsMiddleware extends MiddlewareBase {

let match;
while ((match = regexPattern.exec(input)) !== null) {
const index = match.index; // Position of "?" in the string
const before = input.slice(0, index).replace(/\s+$/, ''); // Context before "?"
const lastChar = before.slice(-1); // Last character before "?"
const index = match.index; // Position of "?" in the string
const before = input.slice(0, index).replace(/\s+$/, ''); // Context before "?"
const lastChar = before.slice(-1); // Last character before "?"

// Determine if the "?" is a special regex symbol
const isSpecialRegexSymbol = /[\.\*\+\)\[\]]$/.test(lastChar);
// Determine if the "?" is a special regex symbol
const isSpecialRegexSymbol = /[\.\*\+\)\[\]]$/.test(lastChar);

if (isSpecialRegexSymbol) {
// If it's special, keep it as is
result += input.slice(lastIndex, index + 1);
} else {
// If it's not special, escape it
result += input.slice(lastIndex, index) + '\\?';
}
lastIndex = index + 1;
if (isSpecialRegexSymbol) {
// If it's special, keep it as is
result += input.slice(lastIndex, index + 1);
} else {
// If it's not special, escape it
result += input.slice(lastIndex, index) + '\\?';
}
lastIndex = index + 1;
}

// Append the remaining part of the string
Expand Down

0 comments on commit 1c7df59

Please sign in to comment.