-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathescapeXml.ts
More file actions
executable file
·21 lines (20 loc) · 699 Bytes
/
escapeXml.ts
File metadata and controls
executable file
·21 lines (20 loc) · 699 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { requireNonEmptyString } from "../lang";
/**
* Escapes special characters in a string for safe use within XML/HTML content.
*
* @param {string} value - The string containing characters to be escaped for XML.
* @returns {string} The escaped string, safe for use in XML/HTML text content or attribute values.
* @throws {@link EmptyStringException}
* @see {@link escapeHtml}
* @see {@link escapeRegExp}
* @since 1.0.0
* @version 1.1.0
*/
export function escapeXml(value: string): string {
return requireNonEmptyString(value)
.replace(/&/g, "&")
.replace(/"/g, """)
.replace(/'/g, "'")
.replace(/</g, "<")
.replace(/>/g, ">");
}