/** * Creates a new SafeHtml which contains, in order, the string representations of the given * {@code htmls}. */ public static SafeHtml concat(Iterable<SafeHtml> htmls) { int concatLength = 0; for (SafeHtml html : htmls) { concatLength += html.getSafeHtmlString().length(); } StringBuilder result = new StringBuilder(concatLength); for (SafeHtml html : htmls) { result.append(html.getSafeHtmlString()); } return create(result.toString()); }
/** Converts a {@link SafeHtml} into a Soy {@link SanitizedContent} of kind HTML. */ public static SanitizedContent fromSafeHtml(SafeHtml html) { return SanitizedContent.create(html.getSafeHtmlString(), ContentKind.HTML); }
/** Converts a {@link SafeHtml} into a Soy {@link SanitizedContent} of kind HTML. */ public static SanitizedContent fromSafeHtml(SafeHtml html) { return SanitizedContent.create(html.getSafeHtmlString(), ContentKind.HTML); }
/** * Creates a {@code data:text/html} URL whose content is populated from the given * {@code SafeHtml} object. * * <p>The resulting {@code data}-scheme URL's content is UTF-8-encoded, and further encoded using * base-64 transfer encoding. * * @see http://tools.ietf.org/html/rfc2397 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs */ public static SafeUrl createHtmlDataUrlBase64(SafeHtml html) { try { String dataUrl = "data:text/html;charset=UTF-8;base64," + BaseEncoding.base64().encode(html.getSafeHtmlString().getBytes("UTF-8")); return create(dataUrl); } catch (UnsupportedEncodingException e) { // Should never happen. We use getBytes(String) instead of getBytes(CharSet) because // there's no java.nio.charset.StandardCharsets in older Android SDKs. throw new RuntimeException(e); } }
/** * Sets the {@code srcdoc} attribute for this element. * * <p>The attribute {@code srcdoc} with a {@code SafeHtml} value is allowed on these elements: * * <ul> * <li>{@code iframe} * </ul> * * @throws IllegalArgumentException if the {@code srcdoc} attribute with a {@code SafeHtml} value * is not allowed on this element */ public SafeHtmlBuilder setSrcdoc(SafeHtml value) { if (!SRCDOC_SAFE_HTML_ELEMENT_WHITELIST.contains(elementName)) { throw new IllegalArgumentException( "Attribute \"srcdoc\" with a SafeHtml value can only be used " + "by one of the following elements: " + SRCDOC_SAFE_HTML_ELEMENT_WHITELIST); } return setAttribute("srcdoc", value.getSafeHtmlString()); }
@CheckReturnValue public SafeHtml build() { StringBuilder sb = new StringBuilder("<" + elementName); for (Map.Entry<String, String> entry : attributes.entrySet()) { sb.append(" " + entry.getKey() + "=\"" + escapeHtmlInternal(entry.getValue()) + "\""); } boolean isVoid = VOID_ELEMENTS.contains(elementName); if (isVoid && useSlashOnVoid) { sb.append("/"); } sb.append(">"); if (!isVoid) { for (SafeHtml content : contents) { sb.append(content.getSafeHtmlString()); } sb.append("</" + elementName + ">"); } return SafeHtmls.create(sb.toString()); }
/** Converts a {@link SafeHtmlProto} into a Soy {@link SanitizedContent} of kind HTML. */ public static SanitizedContent fromSafeHtmlProto(SafeHtmlProto html) { return SanitizedContent.create(SafeHtmls.fromProto(html).getSafeHtmlString(), ContentKind.HTML); }
/** Converts a {@link SafeHtmlProto} into a Soy {@link SanitizedContent} of kind HTML. */ public static SanitizedContent fromSafeHtmlProto(SafeHtmlProto html) { return SanitizedContent.create(SafeHtmls.fromProto(html).getSafeHtmlString(), ContentKind.HTML); }
/** * Creates a {@code data:text/html} URL whose content is populated from the given * {@code SafeHtml} object. * * <p>The resulting {@code data}-scheme URL's content is UTF-8-encoded, but the * encoding of non-ASCII characters is done using the standard %xx hex encoding. * * @see http://tools.ietf.org/html/rfc2397 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs */ public static SafeUrl createHtmlDataUrl(SafeHtml html) { // Use urlPathSegmentEscaper because all other Escapers convert spaces to "+" instead of "%20", // which are rendered as normal "+"s in the browser instead of being rendered as spaces. String dataUrl = "data:text/html;charset=UTF-8," + UrlEscapers.urlPathSegmentEscaper().escape(html.getSafeHtmlString()); return create(dataUrl); }
/** * Serializes a SafeHtml into its opaque protocol message representation. * * <p>Protocol message forms of this type are intended to be opaque. The fields of the returned * protocol message should be considered encapsulated and are not intended for direct inspection * or manipulation. Protocol messages can be converted back into a SafeHtml using * {@link #fromProto(SafeHtmlProto)}. */ public static SafeHtmlProto toProto(SafeHtml safeHtml) { return SafeHtmlProto.newBuilder().setPrivateDoNotAccessOrElseSafeHtmlWrappedValue( safeHtml.getSafeHtmlString()).build(); }