001/*
002 * Copyright (C) 2009 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.html;
016
017import com.google.common.annotations.Beta;
018import com.google.common.annotations.GwtCompatible;
019import com.google.common.escape.Escaper;
020import com.google.common.escape.Escapers;
021
022/**
023 * {@code Escaper} instances suitable for strings to be included in HTML attribute values and
024 * <em>most</em> elements' text contents. When possible, avoid manual escaping by using templating
025 * systems and high-level APIs that provide autoescaping.
026 * One Google-authored templating system available for external use is
027 * <a href="https://developers.google.com/closure/templates/">Closure Templates</a>.
028 *
029 * <p>HTML escaping is particularly tricky: For example, <a href="http://goo.gl/5TgZb">some
030 * elements' text contents must not be HTML escaped</a>. As a result, it is impossible to escape an
031 * HTML document correctly without domain-specific knowledge beyond what {@code HtmlEscapers}
032 * provides. We strongly encourage the use of HTML templating systems.
033 *
034 * @author Sven Mawson
035 * @author David Beaumont
036 * @since 15.0
037 */
038@Beta
039@GwtCompatible
040public final class HtmlEscapers {
041  /**
042   * Returns an {@link Escaper} instance that escapes HTML metacharacters as specified by
043   * <a href="http://www.w3.org/TR/html4/">HTML 4.01</a>. The resulting strings can be used both in
044   * attribute values and in <em>most</em> elements' text contents, provided that the HTML
045   * document's character encoding can encode any non-ASCII code points in the input (as UTF-8 and
046   * other Unicode encodings can).
047   *
048   *
049   * <p><b>Note:</b> This escaper only performs minimal escaping to make content structurally
050   * compatible with HTML. Specifically, it does not perform entity replacement (symbolic or
051   * numeric), so it does not replace non-ASCII code points with character references. This escaper
052   * escapes only the following five ASCII characters: {@code '"&<>}.
053   */
054  public static Escaper htmlEscaper() {
055    return HTML_ESCAPER;
056  }
057
058  // For each xxxEscaper() method, please add links to external reference pages
059  // that are considered authoritative for the behavior of that escaper.
060
061  private static final Escaper HTML_ESCAPER =
062      Escapers.builder()
063          .addEscape('"', "&quot;")
064          // Note: "&apos;" is not defined in HTML 4.01.
065          .addEscape('\'', "&#39;")
066          .addEscape('&', "&amp;")
067          .addEscape('<', "&lt;")
068          .addEscape('>', "&gt;")
069          .build();
070
071  private HtmlEscapers() {}
072}