Escaper instances suitable for strings to be included in HTML attribute values and most elements' text contents. When possible, avoid manual escaping by using templating systems and high-level APIs that provide autoescaping. One Google-authored templating system available for extern
| 38 | |
| 39 | |
| 40 | @Beta |
| 41 | @GwtCompatible |
| 42 | public final class HtmlEscapers { |
| 43 | /** |
| 44 | * Returns an {@link Escaper} instance that escapes HTML metacharacters as specified by |
| 45 | * <a href="http://www.w3.org/TR/html4/">HTML 4.01</a>. The resulting strings can be used both in |
| 46 | * attribute values and in <em>most</em> elements' text contents, provided that the HTML |
| 47 | * document's character encoding can encode any non-ASCII code points in the input (as UTF-8 and |
| 48 | * other Unicode encodings can). |
| 49 | * |
| 50 | * |
| 51 | * <p><b>Note:</b> This escaper only performs minimal escaping to make content structurally |
| 52 | * compatible with HTML. Specifically, it does not perform entity replacement (symbolic or |
| 53 | * numeric), so it does not replace non-ASCII code points with character references. This escaper |
| 54 | * escapes only the following five ASCII characters: {@code '"&<>}. |
| 55 | */ |
| 56 | |
| 57 | public static Escaper htmlEscaper() { |
| 58 | return HTML_ESCAPER; |
| 59 | } |
| 60 | |
| 61 | // For each xxxEscaper() method, please add links to external reference pages |
| 62 | // that are considered authoritative for the behavior of that escaper. |
| 63 | |
| 64 | private static final Escaper HTML_ESCAPER = Escapers.builder().addEscape('"', """) |
| 65 | // Note: "'" is not defined in HTML 4.01. |
| 66 | .addEscape('\'', "'").addEscape('&', "&").addEscape('<', "<").addEscape('>', ">").build(); |
| 67 | private HtmlEscapers() {} |
| 68 | } |