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