/** * Creates a {@link SafeHtml} wrapping the given {@code string}. No validation is performed. * * <p>If possible please use the production API in * {@link com.google.common.html.types.SafeHtmls} * instead. */ public static SafeHtml newSafeHtmlForTest(String string) { return UncheckedConversions.safeHtmlFromStringKnownToSatisfyTypeContract(string); }
/** * Creates a {@link SafeScript} wrapping the given {@code string}. No validation is performed. * * <p>If possible please use the production API in * {@link com.google.common.html.types.SafeScripts} * instead. */ public static SafeScript newSafeScriptForTest(String string) { return UncheckedConversions.safeScriptFromStringKnownToSatisfyTypeContract(string); }
/** * Creates a {@link SafeStyle} wrapping the given {@code string}. No validation is performed. * * <p>If possible please use the production API in * {@link com.google.common.html.types.SafeStyles} * instead. */ public static SafeStyle newSafeStyleForTest(String string) { return UncheckedConversions.safeStyleFromStringKnownToSatisfyTypeContract(string); }
/** * Creates a {@link SafeUrl} wrapping the given {@code string}. No validation is performed. * * <p>If possible please use the production API in * {@link com.google.common.html.types.SafeUrls} * instead. */ public static SafeUrl newSafeUrlForTest(String string) { return UncheckedConversions.safeUrlFromStringKnownToSatisfyTypeContract(string); }
/** * Creates a {@link TrustedResourceUrl} wrapping the given {@code string}. No validation is * performed. * * <p>If possible please use the production API in * {@link com.google.common.html.types.TrustedResourceUrls} * instead. */ public static TrustedResourceUrl newTrustedResourceUrlForTest(String string) { return UncheckedConversions.trustedResourceUrlFromStringKnownToSatisfyTypeContract(string); }
/** * Creates a {@link SafeStyleSheet} wrapping the given {@code string}. No validation is performed. * * <p>If possible please use the production API in * {@link com.google.common.html.types.SafeStyleSheets} * instead. */ public static SafeStyleSheet newSafeStyleSheetForTest(String string) { return UncheckedConversions.safeStyleSheetFromStringKnownToSatisfyTypeContract(string); }
/** * Converts a Soy {@link SanitizedContent} of kind URI into a {@link SafeUrlProto}. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#URI}. */ public SafeUrlProto toSafeUrlProto() { Preconditions.checkState( getContentKind() == ContentKind.URI, "toSafeUrlProto() only valid for SanitizedContent of kind URI, is: %s", getContentKind()); return SafeUrls.toProto( UncheckedConversions.safeUrlFromStringKnownToSatisfyTypeContract(getContent())); }
/** * Converts a Soy {@link SanitizedContent} of kind TRUSTED_RESOURCE_URI into a {@link * TrustedResourceUrlProto}. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#TRUSTED_RESOURCE_URI}. */ public TrustedResourceUrlProto toTrustedResourceUrlProto() { Preconditions.checkState( getContentKind() == ContentKind.TRUSTED_RESOURCE_URI, "toTrustedResourceUrlProto() only valid for SanitizedContent of kind TRUSTED_RESOURCE_URI, " + "is: %s", getContentKind()); return TrustedResourceUrls.toProto( UncheckedConversions.trustedResourceUrlFromStringKnownToSatisfyTypeContract(getContent())); }
/** * Converts a Soy {@link SanitizedContent} of kind CSS into a {@link SafeStyleSheet}. * * <p>To ensure correct behavior and usage, the SanitizedContent object should fulfill the * contract of SafeStyleSheet - the CSS content should represent the top-level content of a style * element within HTML. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#CSS}. */ public SafeStyleSheet toSafeStyleSheet() { Preconditions.checkState( getContentKind() == ContentKind.CSS, "toSafeStyleSheet() only valid for SanitizedContent of kind CSS, is: %s", getContentKind()); // Sanity check: Try to prevent accidental misuse when this is not really a stylesheet but // instead just a declaration list (i.e. a SafeStyle). This does fail to accept a stylesheet // that is only a comment or only @imports; if you have a legitimate reason for this, it would // be fine to make this more sophisticated, but in practice it's unlikely and keeping this check // simple helps ensure it is fast. Note that this isn't a true security boundary, but a // best-effort attempt to preserve SafeStyleSheet's semantical guarantees. Preconditions.checkState( getContent().isEmpty() || getContent().indexOf('{') > 0, "Calling toSafeStyleSheet() with content that doesn't look like a stylesheet"); return UncheckedConversions.safeStyleSheetFromStringKnownToSatisfyTypeContract(getContent()); }
/** * Converts a Soy {@link SanitizedContent} of kind HTML into a {@link SafeHtml}. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#HTML}. */ public SafeHtml toSafeHtml() { Preconditions.checkState( getContentKind() == ContentKind.HTML, "toSafeHtml() only valid for SanitizedContent of kind HTML, is: %s", getContentKind()); return UncheckedConversions.safeHtmlFromStringKnownToSatisfyTypeContract(getContent()); }
/** * Converts a Soy {@link SanitizedContent} of kind CSS into a {@link SafeStyleProto}. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#CSS}. */ public SafeStyleProto toSafeStyleProto() { Preconditions.checkState( getContentKind() == ContentKind.CSS, "toSafeStyleProto() only valid for SanitizedContent of kind CSS, is: %s", getContentKind()); // Sanity check: Try to prevent accidental misuse when this is a full stylesheet rather than a // declaration list. // The error may trigger incorrectly if the content contains curly brackets inside comments or // quoted strings. // // This is a best-effort attempt to preserve SafeStyle's semantical guarantees. Preconditions.checkState( !getContent().contains("{"), "Calling toSafeStyleProto() with content that doesn't look like CSS declarations. " + "Consider using toSafeStyleSheetProto()."); return SafeStyles.toProto( UncheckedConversions.safeStyleFromStringKnownToSatisfyTypeContract(getContent())); }
/** * Converts a Soy {@link SanitizedContent} of kind URI into a {@link SafeUrlProto}. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#URI}. */ public SafeUrlProto toSafeUrlProto() { Preconditions.checkState( getContentKind() == ContentKind.URI, "toSafeUrlProto() only valid for SanitizedContent of kind URI, is: %s", getContentKind()); return SafeUrls.toProto( UncheckedConversions.safeUrlFromStringKnownToSatisfyTypeContract(getContent())); }
/** * Converts a Soy {@link SanitizedContent} of kind JS into a {@link SafeScriptProto}. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#JS}. */ public SafeScriptProto toSafeScriptProto() { Preconditions.checkState( getContentKind() == ContentKind.JS, "toSafeScriptProto() only valid for SanitizedContent of kind JS, is: %s", getContentKind()); return SafeScripts.toProto( UncheckedConversions.safeScriptFromStringKnownToSatisfyTypeContract(getContent())); }
/** * Converts a Soy {@link SanitizedContent} of kind TRUSTED_RESOURCE_URI into a {@link * TrustedResourceUrlProto}. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#TRUSTED_RESOURCE_URI}. */ public TrustedResourceUrlProto toTrustedResourceUrlProto() { Preconditions.checkState( getContentKind() == ContentKind.TRUSTED_RESOURCE_URI, "toTrustedResourceUrlProto() only valid for SanitizedContent of kind TRUSTED_RESOURCE_URI, " + "is: %s", getContentKind()); return TrustedResourceUrls.toProto( UncheckedConversions.trustedResourceUrlFromStringKnownToSatisfyTypeContract(getContent())); }
/** * Converts a Soy {@link SanitizedContent} of kind CSS into a {@link SafeStyleSheet}. * * <p>To ensure correct behavior and usage, the SanitizedContent object should fulfill the * contract of SafeStyleSheet - the CSS content should represent the top-level content of a style * element within HTML. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#CSS}. */ public SafeStyleSheet toSafeStyleSheet() { Preconditions.checkState( getContentKind() == ContentKind.CSS, "toSafeStyleSheet() only valid for SanitizedContent of kind CSS, is: %s", getContentKind()); // Sanity check: Try to prevent accidental misuse when this is not really a stylesheet but // instead just a declaration list (i.e. a SafeStyle). This does fail to accept a stylesheet // that is only a comment or only @imports; if you have a legitimate reason for this, it would // be fine to make this more sophisticated, but in practice it's unlikely and keeping this check // simple helps ensure it is fast. Note that this isn't a true security boundary, but a // best-effort attempt to preserve SafeStyleSheet's semantical guarantees. Preconditions.checkState( getContent().isEmpty() || getContent().indexOf('{') > 0, "Calling toSafeStyleSheet() with content that doesn't look like a stylesheet"); return UncheckedConversions.safeStyleSheetFromStringKnownToSatisfyTypeContract(getContent()); }
/** * Converts a Soy {@link SanitizedContent} of kind HTML into a {@link SafeHtml}. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#HTML}. */ public SafeHtml toSafeHtml() { Preconditions.checkState( getContentKind() == ContentKind.HTML, "toSafeHtml() only valid for SanitizedContent of kind HTML, is: %s", getContentKind()); return UncheckedConversions.safeHtmlFromStringKnownToSatisfyTypeContract(getContent()); }
/** * Converts a Soy {@link SanitizedContent} of kind CSS into a {@link SafeStyleProto}. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#CSS}. */ public SafeStyleProto toSafeStyleProto() { Preconditions.checkState( getContentKind() == ContentKind.CSS, "toSafeStyleProto() only valid for SanitizedContent of kind CSS, is: %s", getContentKind()); // Sanity check: Try to prevent accidental misuse when this is a full stylesheet rather than a // declaration list. // The error may trigger incorrectly if the content contains curly brackets inside comments or // quoted strings. // // This is a best-effort attempt to preserve SafeStyle's semantical guarantees. Preconditions.checkState( !getContent().contains("{"), "Calling toSafeStyleProto() with content that doesn't look like CSS declarations. " + "Consider using toSafeStyleSheetProto()."); return SafeStyles.toProto( UncheckedConversions.safeStyleFromStringKnownToSatisfyTypeContract(getContent())); }
/** * Converts a Soy {@link SanitizedContent} of kind JS into a {@link SafeScriptProto}. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#JS}. */ public SafeScriptProto toSafeScriptProto() { Preconditions.checkState( getContentKind() == ContentKind.JS, "toSafeScriptProto() only valid for SanitizedContent of kind JS, is: %s", getContentKind()); return SafeScripts.toProto( UncheckedConversions.safeScriptFromStringKnownToSatisfyTypeContract(getContent())); }
/** * Converts a Soy {@link SanitizedContent} of kind HTML into a {@link SafeHtmlProto}. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#HTML}. */ public SafeHtmlProto toSafeHtmlProto() { Preconditions.checkState( getContentKind() == ContentKind.HTML, "toSafeHtmlProto() only valid for SanitizedContent of kind HTML, is: %s", getContentKind()); return SafeHtmls.toProto( UncheckedConversions.safeHtmlFromStringKnownToSatisfyTypeContract(getContent())); }
/** * Converts a Soy {@link SanitizedContent} of kind HTML into a {@link SafeHtmlProto}. * * @throws IllegalStateException if this SanitizedContent's content kind is not {@link * ContentKind#HTML}. */ public SafeHtmlProto toSafeHtmlProto() { Preconditions.checkState( getContentKind() == ContentKind.HTML, "toSafeHtmlProto() only valid for SanitizedContent of kind HTML, is: %s", getContentKind()); return SafeHtmls.toProto( UncheckedConversions.safeHtmlFromStringKnownToSatisfyTypeContract(getContent())); }