Escapes and unescapes based on the W3C HTML 4.01 recommendation, handling character entity references.
Reference: https://www.w3.org/TR/html4/charset.html
For a comprehensive set of String escaping utilities, consider
Apache Commons Text
and its StringEscapeUtils class. We do not use that class here in order
to avoid a runtime dependency on Commons Text just for HTML escaping. Furthermore,
Spring's HTML escaping is more flexible and 100% HTML 4.0 compliant.
| Modifier and Type | Field and Description |
|---|---|
| private static final HtmlCharacterEntityReferences | characterEntityReferences
Shared instance of pre-parsed HTML character entity references. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static String | Returns: the escaped stringthe (unescaped) input string input)Turn special characters into HTML character references. |
| public static String | Returns: the escaped stringthe (unescaped) input string input, String the name of a supported encoding)charsetTurn special characters into HTML character references. |
| public static String | Returns: the escaped stringthe (unescaped) input string input)Turn special characters into HTML character references. |
| public static String | Returns: the escaped stringthe (unescaped) input string input, String the name of a supported encoding)charsetTurn special characters into HTML character references. |
| public static String | Returns: the escaped stringthe (unescaped) input string input)Turn special characters into HTML character references. |
| public static String | Returns: the escaped stringthe (unescaped) input string input, String the name of a supported encoding)charsetTurn special characters into HTML character references. |
| public static String | Returns: the unescaped stringthe (escaped) input string input)Turn HTML character references into their plain text UNICODE equivalent. |
| characterEntityReferences | back to summary |
|---|---|
| private static final HtmlCharacterEntityReferences characterEntityReferences Shared instance of pre-parsed HTML character entity references. | |
| HtmlUtils | back to summary |
|---|---|
| public HtmlUtils() | |
| htmlEscape | back to summary |
|---|---|
| public static String htmlEscape(String input) Turn special characters into HTML character references. Handles the complete character set defined in the HTML 4.01 recommendation. Escapes all special characters to their corresponding
entity reference (e.g. | |
| htmlEscape | back to summary |
|---|---|
| public static String htmlEscape(String input, String encoding) Turn special characters into HTML character references. Handles the complete character set defined in the HTML 4.01 recommendation. Escapes all special characters to their corresponding
entity reference (e.g. | |
| htmlEscapeDecimal | back to summary |
|---|---|
| public static String htmlEscapeDecimal(String input) Turn special characters into HTML character references. Handles the complete character set defined in the HTML 4.01 recommendation. Escapes all special characters to their corresponding numeric reference in decimal format (&#Decimal;). | |
| htmlEscapeDecimal | back to summary |
|---|---|
| public static String htmlEscapeDecimal(String input, String encoding) Turn special characters into HTML character references. Handles the complete character set defined in the HTML 4.01 recommendation. Escapes all special characters to their corresponding numeric reference in decimal format (&#Decimal;) at least as required by the specified encoding. In other words, if a special character does not have to be escaped for the given encoding, it may not be. | |
| htmlEscapeHex | back to summary |
|---|---|
| public static String htmlEscapeHex(String input) Turn special characters into HTML character references. Handles the complete character set defined in the HTML 4.01 recommendation. Escapes all special characters to their corresponding numeric reference in hex format (&#xHex;). | |
| htmlEscapeHex | back to summary |
|---|---|
| public static String htmlEscapeHex(String input, String encoding) Turn special characters into HTML character references. Handles the complete character set defined in the HTML 4.01 recommendation. Escapes all special characters to their corresponding numeric reference in hex format (&#xHex;) at least as required by the specified encoding. In other words, if a special character does not have to be escaped for the given encoding, it may not be. | |
| htmlUnescape | back to summary |
|---|---|
| public static String htmlUnescape(String input) Turn HTML character references into their plain text UNICODE equivalent. Handles complete character set defined in HTML 4.01 recommendation and all reference types (decimal, hex, and entity). Correctly converts the following formats: &#Entity; - (Example: &) case sensitive &#Decimal; - (Example: D) Gracefully handles malformed character references by copying original characters as is when encountered. | |