-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] Issue with special message rendering (#19817)
- Loading branch information
1 parent
ade4c64
commit 9d9312e
Showing
3 changed files
with
49 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import assert from 'assert'; | ||
|
||
import { describe, it } from 'mocha'; | ||
|
||
import { escapeHTML } from './escapeHTML'; | ||
|
||
describe('escapeHTML', () => { | ||
it('works', () => { | ||
assert.strictEqual(escapeHTML('<div>Blah & "blah" & \'blah\'</div>'), '<div>Blah & "blah" & 'blah'</div>'); | ||
assert.strictEqual(escapeHTML('<'), '&lt;'); | ||
assert.strictEqual(escapeHTML(' '), ' '); | ||
assert.strictEqual(escapeHTML('¢'), '¢'); | ||
assert.strictEqual(escapeHTML('¢ £ ¥ € © ®'), '¢ £ ¥ € © ®'); | ||
assert.strictEqual(escapeHTML(5 as unknown as string), '5'); | ||
assert.strictEqual(escapeHTML(''), ''); | ||
assert.strictEqual(escapeHTML(null as unknown as string), ''); | ||
assert.strictEqual(escapeHTML(undefined as unknown as string), ''); | ||
}); | ||
}); |
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,27 @@ | ||
const characterToHtmlEntityCode = { | ||
'¢': 'cent', | ||
'£': 'pound', | ||
'¥': 'yen', | ||
'€': 'euro', | ||
'©': 'copy', | ||
'®': 'reg', | ||
'<': 'lt', | ||
'>': 'gt', | ||
'"': 'quot', | ||
'&': 'amp', | ||
'\'': '#39', | ||
} as const; | ||
|
||
const regex = new RegExp(`[${ Object.keys(characterToHtmlEntityCode).join('') }]`, 'g'); | ||
|
||
const toString = (object: unknown): string => | ||
(object ? `${ object }` : ''); | ||
|
||
const isEscapable = (char: string): char is keyof typeof characterToHtmlEntityCode => | ||
char in characterToHtmlEntityCode; | ||
|
||
const escapeChar = (char: string): string => | ||
(isEscapable(char) ? `&${ characterToHtmlEntityCode[char] };` : ''); | ||
|
||
export const escapeHTML = (str: string): string => | ||
toString(str).replace(regex, escapeChar); |