MCPcopy
hub / github.com/google/guava / HtmlEscapers

Class HtmlEscapers

guava/src/com/google/common/html/HtmlEscapers.java:38–70  ·  view source on GitHub ↗

{@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

Source from the content-addressed store, hash-verified

36 * @since 15.0
37 */
38@GwtCompatible
39public 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('"', "&quot;")
62 // Note: "&apos;" is not defined in HTML 4.01.
63 .addEscape('\'', "&#39;")
64 .addEscape('&', "&amp;")
65 .addEscape('<', "&lt;")
66 .addEscape('>', "&gt;")
67 .build();
68
69 private HtmlEscapers() {}
70}

Callers

nothing calls this directly

Calls 3

builderMethod · 0.95
buildMethod · 0.45
addEscapeMethod · 0.45

Tested by

no test coverage detected