Class HtmlUtil
java.lang.Object
cloud.opencode.base.string.escape.HtmlUtil
HTML Escape Utility - Provides HTML string escaping methods.
HTML转义工具 - 提供HTML字符串转义方法。
Features | 主要功能:
- HTML entity escaping (&, <, >, ", ') - HTML实体转义
- HTML entity unescaping - HTML实体反转义
Usage Examples | 使用示例:
String escaped = HtmlUtil.escape("<script>alert('xss')</script>");
// "<script>alert('xss')</script>"
String unescaped = HtmlUtil.unescape("<b>bold</b>");
// "<b>bold</b>"
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
- Null-safe: Yes - 空值安全: 是
Performance | 性能特性:
- Time complexity: O(n) where n is the string length - 时间复杂度: O(n),n为字符串长度
- Space complexity: O(n) for the output string - 空间复杂度: O(n),存储输出字符串
- Since:
- JDK 25, opencode-base-string V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringstatic StringescapeStrict(String str) static String
-
Method Details
-
escape
-
escapeStrict
Strict HTML escape: same asescape(String)plus encoding the forward slash/as/. Implements the OWASP XSS Cheat Sheet recommendation for HTML body context, where encoding/prevents user content from materialising a closing</script>/</style>sequence and breaking out of the surrounding script or style block. 严格 HTML 转义:在escape(String)基础上额外将正斜杠/编码为/。实现 OWASP XSS Cheat Sheet 在 HTML body 上下文下的 推荐做法 — 编码/可阻止用户内容形成闭合</script>/</style>序列从而突破当前脚本/样式块。- Parameters:
str- input string, may benull| 输入字符串,可为null- Returns:
- strictly-escaped string, or
nullif input isnull - Since:
- opencode-base-string V1.0.4
-
unescape
-