-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(next-app-router): escape injected results for hydration (#6141)
* fix(next-app-router): escape injected results for hydration * add tests * exclude tests from types build
- Loading branch information
1 parent
1b683c5
commit 2e6f0de
Showing
5 changed files
with
36 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/react-instantsearch-nextjs/src/__tests__/htmlEscape.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { htmlEscapeJsonString } from '../htmlEscape'; | ||
|
||
it('encodes HTML related characters into entities that are decoded by JSON.parse', () => { | ||
const input = { description: '<h1>Hello</h1>' }; | ||
const output = htmlEscapeJsonString(JSON.stringify(input)); | ||
|
||
expect(output).toBe( | ||
'{"description":"\\u003ch1\\u003eHello\\u003c/h1\\u003e"}' | ||
); | ||
expect(JSON.parse(output)).toEqual(input); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// This file was taken from the Next.js repo : https://github.com/vercel/next.js/blob/754fadacf30c145009506662bfbd2a4ccebb377d/packages/next/src/server/htmlescape.ts | ||
// License: https://github.com/vercel/next.js/blob/754fadacf30c145009506662bfbd2a4ccebb377d/license.md | ||
|
||
// This utility is based on https://github.com/zertosh/htmlescape | ||
// License: https://github.com/zertosh/htmlescape/blob/0527ca7156a524d256101bb310a9f970f63078ad/LICENSE | ||
|
||
const ESCAPE_LOOKUP: { [match: string]: string } = { | ||
'&': '\\u0026', | ||
'>': '\\u003e', | ||
'<': '\\u003c', | ||
'\u2028': '\\u2028', | ||
'\u2029': '\\u2029', | ||
}; | ||
|
||
export const ESCAPE_REGEX = /[&><\u2028\u2029]/g; | ||
|
||
export function htmlEscapeJsonString(str: string): string { | ||
return str.replace(ESCAPE_REGEX, (match) => ESCAPE_LOOKUP[match]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.declaration", | ||
"exclude": ["**/__tests__/e2e/**/*", "**/dist/**/*"] | ||
"exclude": ["**/__tests__/**/*", "**/dist/**/*"] | ||
} |