public final class

SafeHtmlBuilder

extends Object
java.lang.Object
   ↳ com.google.gwt.safehtml.shared.SafeHtmlBuilder

Class Overview

A builder that facilitates the building up of XSS-safe HTML from text snippets. It is used essentially like a StringBuilder; unlike a StringBuilder, it automatically HTML-escapes appended input where necessary.

In addition, it supports methods that allow strings with HTML markup to be appended without escaping: One can append other SafeHtml objects, and one can append constant strings. The method that appends constant strings (appendHtmlConstant(String)) requires a convention of use to be adhered to in order for this class to adhere to the contract required by SafeHtml: The argument expression must be fully determined and known to be safe at compile time, and the value of the argument must not contain incomplete HTML tags. See appendHtmlConstant(String) for details.

The accumulated XSS-safe HTML can be obtained in the form of a SafeHtml via the toSafeHtml() method.

This class is not thread-safe.

Summary

Public Constructors
SafeHtmlBuilder()
Constructs an empty SafeHtmlBuilder.
Public Methods
SafeHtmlBuilder append(int num)
Appends the string representation of a number.
SafeHtmlBuilder append(long num)
Appends the string representation of a number.
SafeHtmlBuilder append(double num)
Appends the string representation of a number.
SafeHtmlBuilder append(boolean b)
Appends the string representation of a boolean.
SafeHtmlBuilder append(float num)
Appends the string representation of a number.
SafeHtmlBuilder append(SafeHtml html)
Appends the contents of another SafeHtml object, without applying HTML-escaping to it.
SafeHtmlBuilder append(char c)
Appends the string representation of a char.
SafeHtmlBuilder append(byte num)
Appends the string representation of a number.
SafeHtmlBuilder appendEscaped(String text)
Appends a string after HTML-escaping it.
SafeHtmlBuilder appendEscapedLines(String text)
Appends a string consisting of several newline-separated lines after HTML-escaping it.
SafeHtmlBuilder appendHtmlConstant(String html)
Appends a compile-time-constant string, which will not be escaped.
SafeHtml toSafeHtml()
Returns the safe HTML accumulated in the builder as a SafeHtml.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public SafeHtmlBuilder ()

Constructs an empty SafeHtmlBuilder.

Public Methods

public SafeHtmlBuilder append (int num)

Appends the string representation of a number.

Parameters
num the number whose string representation to append
Returns
  • a reference to this object

public SafeHtmlBuilder append (long num)

Appends the string representation of a number.

Parameters
num the number whose string representation to append
Returns
  • a reference to this object

public SafeHtmlBuilder append (double num)

Appends the string representation of a number.

Parameters
num the number whose string representation to append
Returns
  • a reference to this object

public SafeHtmlBuilder append (boolean b)

Appends the string representation of a boolean.

Parameters
b the boolean whose string representation to append
Returns
  • a reference to this object

public SafeHtmlBuilder append (float num)

Appends the string representation of a number.

Parameters
num the number whose string representation to append
Returns
  • a reference to this object

public SafeHtmlBuilder append (SafeHtml html)

Appends the contents of another SafeHtml object, without applying HTML-escaping to it.

Parameters
html the SafeHtml to append
Returns
  • a reference to this object

public SafeHtmlBuilder append (char c)

Appends the string representation of a char.

Parameters
c the character whose string representation to append
Returns
  • a reference to this object

public SafeHtmlBuilder append (byte num)

Appends the string representation of a number.

Parameters
num the number whose string representation to append
Returns
  • a reference to this object

public SafeHtmlBuilder appendEscaped (String text)

Appends a string after HTML-escaping it.

Parameters
text the string to append
Returns
  • a reference to this object

public SafeHtmlBuilder appendEscapedLines (String text)

Appends a string consisting of several newline-separated lines after HTML-escaping it. Newlines in the original string are converted to <br>.

Parameters
text the string to append
Returns
  • a reference to this object

public SafeHtmlBuilder appendHtmlConstant (String html)

Appends a compile-time-constant string, which will not be escaped.

Important: For this class to be able to honor its contract as required by SafeHtml, all uses of this method must satisfy the following constraints:

  1. The argument expression must be fully determined at compile time.
  2. The value of the argument must end in "inner HTML" context and not contain incomplete HTML tags. I.e., the following is not a correct use of this method, because the <a> tag is incomplete:
     shb.appendConstantHtml("<a href='").append(url)

The first constraint provides a sufficient condition that the appended string (and any HTML markup contained in it) originates from a trusted source. The second constraint ensures the composability of SafeHtml values.

When executing client-side in Development Mode, or server side with assertions enabled, the argument is HTML-parsed and validated to satisfy the second constraint (the server-side check can also be enabled programmatically, see maybeCheckCompleteHtml(String) for details). For performance reasons, this check is not performed in prod mode on the client, and with assertions disabled on the server.

Parameters
html the HTML snippet to be appended
Returns
  • a reference to this object
Throws
IllegalArgumentException if not running in prod mode and html violates the second constraint

public SafeHtml toSafeHtml ()

Returns the safe HTML accumulated in the builder as a SafeHtml.

Returns
  • a SafeHtml instance