forked from nhn/tui.editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug: escape XML-entities in codeSyntaxHighlight plugin when language …
…is unspecified (fix nhn#3220)
- Loading branch information
1 parent
0c5c11b
commit 3dc4ab7
Showing
2 changed files
with
30 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,28 @@ | ||
export function flatten<T>(arr: T[]): T[] { | ||
return arr.reduce<T[]>((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []); | ||
} | ||
|
||
const XMLSPECIAL = '[&<>"]'; | ||
const reXmlSpecial = new RegExp(XMLSPECIAL, 'g'); | ||
|
||
function replaceUnsafeChar(char: string) { | ||
switch (char) { | ||
case '&': | ||
return '&'; | ||
case '<': | ||
return '<'; | ||
case '>': | ||
return '>'; | ||
case '"': | ||
return '"'; | ||
default: | ||
return char; | ||
} | ||
} | ||
|
||
export function escapeXml(text: string) { | ||
if (reXmlSpecial.test(text)) { | ||
return text.replace(reXmlSpecial, replaceUnsafeChar); | ||
} | ||
return text; | ||
} |