API Reference 0.3.24dart_web_toolkit_utilSafeHtmlUtils

SafeHtmlUtils class

Utility class containing static methods for escaping and sanitizing strings.

class SafeHtmlUtils {

 /**
  * An empty String.
  */
 static final SafeHtml EMPTY_SAFE_HTML = new SafeHtmlString("");
 
 /**
  * Returns a {@link SafeHtml} containing the escaped string.
  *
  * @param s the input String
  * @return a {@link SafeHtml} instance
  */
 static SafeHtml fromString(String s) {
   return new SafeHtmlString(htmlEscape(s));
 }

 /**
  * Returns a {@link SafeHtml} constructed from a trusted string, i.e., without
  * escaping the string. No checks are performed. The calling code should be
  * carefully reviewed to ensure the argument meets the {@link SafeHtml} contract.
  *
  * @param s the input String
  * @return a {@link SafeHtml} instance
  */
 static SafeHtml fromTrustedString(String s) {
   return new SafeHtmlString(s);
 }

 static String htmlEscape(String text) {
   return text.replaceAll("&", "&")
       .replaceAll("<", "&lt;")
       .replaceAll(">", "&gt;")
       .replaceAll('"', "&quot;")
       .replaceAll("'", "&apos;");
 }
}

Static Properties

final SafeHtml EMPTY_SAFE_HTML #

An empty String.

static final SafeHtml EMPTY_SAFE_HTML = new SafeHtmlString("")

Static Methods

SafeHtml fromString(String s) #

Returns a {@link SafeHtml} containing the escaped string.

@param s the input String @return a {@link SafeHtml} instance

static SafeHtml fromString(String s) {
 return new SafeHtmlString(htmlEscape(s));
}

SafeHtml fromTrustedString(String s) #

Returns a {@link SafeHtml} constructed from a trusted string, i.e., without escaping the string. No checks are performed. The calling code should be carefully reviewed to ensure the argument meets the {@link SafeHtml} contract.

@param s the input String @return a {@link SafeHtml} instance

static SafeHtml fromTrustedString(String s) {
 return new SafeHtmlString(s);
}

String htmlEscape(String text) #

static String htmlEscape(String text) {
 return text.replaceAll("&", "&amp;")
     .replaceAll("<", "&lt;")
     .replaceAll(">", "&gt;")
     .replaceAll('"', "&quot;")
     .replaceAll("'", "&apos;");
}