-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtml.java
More file actions
38 lines (34 loc) · 1.08 KB
/
Copy pathHtml.java
File metadata and controls
38 lines (34 loc) · 1.08 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
package com.example;
/**
* Minimal HTML escaping. turismo's {@code print()} writes text as-is, so
* any request data written into an HTML response must be escaped first.
*/
public final class Html {
private Html() {
}
/**
* Escapes {@code & < > " '} so the value is safe inside HTML text and
* quoted attributes. Returns an empty string for {@code null}.
*
* @param s the value to escape
* @return the escaped value
*/
public static String htmlEscape(String s) {
if (s == null) {
return "";
}
StringBuilder sb = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '&': sb.append("&"); break;
case '<': sb.append("<"); break;
case '>': sb.append(">"); break;
case '"': sb.append("""); break;
case '\'': sb.append("'"); break;
default: sb.append(c);
}
}
return sb.toString();
}
}