spring-framework / org.springframework.web.util / HtmlUtils

HtmlUtils

abstract class HtmlUtils

Utility class for HTML escaping. Escapes and unescapes based on the W3C HTML 4.01 recommendation, handling character entity references.

Reference: http://www.w3.org/TR/html4/charset.html

For a comprehensive set of String escaping utilities, consider Apache Commons Lang and its StringEscapeUtils class. We are not using that class here to avoid a runtime dependency on Commons Lang just for HTML escaping. Furthermore, Spring's HTML escaping is more flexible and 100% HTML 4.0 compliant.

Author
Juergen Hoeller

Author
Martin Kersten

Author
Craig Andrews

Since
01.03.2003

Constructors

<init>

HtmlUtils()

Utility class for HTML escaping. Escapes and unescapes based on the W3C HTML 4.01 recommendation, handling character entity references.

Reference: http://www.w3.org/TR/html4/charset.html

For a comprehensive set of String escaping utilities, consider Apache Commons Lang and its StringEscapeUtils class. We are not using that class here to avoid a runtime dependency on Commons Lang just for HTML escaping. Furthermore, Spring's HTML escaping is more flexible and 100% HTML 4.0 compliant.

Functions

htmlEscape

open static fun htmlEscape(input: String): String

Turn special characters into HTML character references. Handles complete character set defined in HTML 4.01 recommendation.

Escapes all special characters to their corresponding entity reference (e.g. &lt;).

Reference: http://www.w3.org/TR/html4/sgml/entities.html

open static fun htmlEscape(input: String, encoding: String): String

Turn special characters into HTML character references. Handles complete character set defined in HTML 4.01 recommendation.

Escapes all special characters to their corresponding entity reference (e.g. &lt;) 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.

Reference: http://www.w3.org/TR/html4/sgml/entities.html

htmlEscapeDecimal

open static fun htmlEscapeDecimal(input: String): String

Turn special characters into HTML character references. Handles complete character set defined in HTML 4.01 recommendation.

Escapes all special characters to their corresponding numeric reference in decimal format (&#Decimal;).

Reference: http://www.w3.org/TR/html4/sgml/entities.html

open static fun htmlEscapeDecimal(input: String, encoding: String): String

Turn special characters into HTML character references. Handles complete character set defined in 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.

Reference: http://www.w3.org/TR/html4/sgml/entities.html

htmlEscapeHex

open static fun htmlEscapeHex(input: String): String

Turn special characters into HTML character references. Handles complete character set defined in HTML 4.01 recommendation.

Escapes all special characters to their corresponding numeric reference in hex format (&#xHex;).

Reference: http://www.w3.org/TR/html4/sgml/entities.html

open static fun htmlEscapeHex(input: String, encoding: String): String

Turn special characters into HTML character references. Handles complete character set defined in 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.

Reference: http://www.w3.org/TR/html4/sgml/entities.html

htmlUnescape

open static fun htmlUnescape(input: String): String

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: &amp;) case sensitive &#Decimal; - (Example: &#68;) &#xHex; - (Example: &#xE5;) case insensitive Gracefully handles malformed character references by copying original characters as is when encountered.

Reference: http://www.w3.org/TR/html4/sgml/entities.html