This repository was archived by the owner on Jun 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlExportUtils.java
More file actions
62 lines (58 loc) · 2.27 KB
/
Copy pathHtmlExportUtils.java
File metadata and controls
62 lines (58 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package gvisual;
/**
* Shared HTML/JavaScript escaping utilities for file exporters.
*
* <p>Provides secure string escaping to prevent XSS (CWE-79) when
* embedding user-controlled data (node IDs, labels, titles) in
* generated HTML and JavaScript output.</p>
*
* @author zalenix
*/
public final class HtmlExportUtils {
private HtmlExportUtils() { /* utility class */ }
/**
* Escapes a string for safe embedding inside a JavaScript double-quoted
* string literal within an HTML {@code <script>} block.
*
* <p>Handles:</p>
* <ul>
* <li>Backslash, double-quote, single-quote</li>
* <li>Forward-slash — prevents {@code </script>} tag injection (CWE-79)</li>
* <li>Newline and carriage return</li>
* <li>Backtick and {@code $} — prevents template literal injection</li>
* <li>Unicode line/paragraph separators (U+2028, U+2029) — valid in
* JSON but break JavaScript string literals in older engines</li>
* </ul>
*
* @param s the string to escape (null-safe, returns empty string for null)
* @return escaped string safe for JS string literal embedding
*/
public static String escapeJs(String s) {
if (s == null) return "";
return s.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("'", "\\'")
.replace("/", "\\/") // prevent </script> breakout
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t")
.replace("`", "\\`") // prevent template literal injection
.replace("$", "\\$") // prevent ${} interpolation
.replace("\u2028", "\\u2028") // Unicode line separator
.replace("\u2029", "\\u2029"); // Unicode paragraph separator
}
/**
* Escapes a string for safe embedding in HTML content.
*
* @param s the string to escape (null-safe)
* @return HTML-escaped string
*/
public static String escapeHtml(String s) {
if (s == null) return "";
return s.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace("\"", """)
.replace("'", "'");
}
}