/** * Creates a {@link SafeStyleSheetProto} 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 SafeStyleSheetProto newSafeStyleSheetProtoForTest(String string) { return SafeStyleSheets.toProto(newSafeStyleSheetForTest(string)); }
/** * Creates a new SafeStyleSheet which contains, in order, the string representations of the given * {@code stylesheets}. */ public static SafeStyleSheet concat(SafeStyleSheet... stylesheets) { return concat(Arrays.asList(stylesheets)); }
/** * Deserializes a SafeStyleSheetProto into a SafeStyleSheet instance. * * <p>Protocol-message forms are intended to be opaque. The fields of the protocol message should * be considered encapsulated and are not intended for direct inspection or manipulation. Protocol * message forms of this type should be produced by {@link #toProto(SafeStyleSheet)} or its * equivalent in other implementation languages. * * <p><b>Important:</b> It is unsafe to invoke this method on a protocol message that has been * received from an entity outside the application's trust domain. Data coming from the browser * is outside the application's trust domain. */ public static SafeStyleSheet fromProto(SafeStyleSheetProto proto) { return create(proto.getPrivateDoNotAccessOrElseSafeStyleSheetWrappedValue()); }
/** Converts a {@link SafeStyleSheetProto} into a Soy {@link SanitizedContent} of kind CSS. */ public static SanitizedContent fromSafeStyleSheetProto(SafeStyleSheetProto styleSheet) { return SanitizedContent.create( SafeStyleSheets.fromProto(styleSheet).getSafeStyleSheetString(), ContentKind.CSS); }
/** Converts a {@link SafeStyleSheetProto} into a Soy {@link SanitizedContent} of kind CSS. */ public static SanitizedContent fromSafeStyleSheetProto(SafeStyleSheetProto styleSheet) { return SanitizedContent.create( SafeStyleSheets.fromProto(styleSheet).getSafeStyleSheetString(), ContentKind.CSS); }
/** * Converts a Soy {@link SanitizedContent} of kind CSS into a {@link SafeStyleSheetProto}. * * <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 SafeStyleSheetProto toSafeStyleSheetProto() { Preconditions.checkState( getContentKind() == ContentKind.CSS, "toSafeStyleSheetProto() only valid for SanitizedContent of kind CSS, is: %s", getContentKind()); return SafeStyleSheets.toProto(toSafeStyleSheet()); }
/** * Creates a SafeStyleSheet from the given compile-time constant string {@code style}. * * <p>{@code styleSheet} must not have any < characters in it so that the syntactic * structure of any surrounding CSS and HTML is not affected. * * @throws IllegalArgumentException if {@code styleSheet} contains < */ public static SafeStyleSheet fromConstant(@CompileTimeConstant final String styleSheet) { if (styleSheet.length() == 0) { return SafeStyleSheet.EMPTY; } for (int i = 0; i < styleSheet.length(); i++) { if (styleSheet.charAt(i) == '<') { throw new IllegalArgumentException( "Forbidden '<' character in style sheet string: " + styleSheet); } } return create(styleSheet); }
/** * Converts a Soy {@link SanitizedContent} of kind CSS into a {@link SafeStyleSheetProto}. * * <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 SafeStyleSheetProto toSafeStyleSheetProto() { Preconditions.checkState( getContentKind() == ContentKind.CSS, "toSafeStyleSheetProto() only valid for SanitizedContent of kind CSS, is: %s", getContentKind()); return SafeStyleSheets.toProto(toSafeStyleSheet()); }
/** * Creates a new SafeStyleSheet which contains, in order, the string representations of the given * {@code stylesheets}. */ public static SafeStyleSheet concat(Iterable<SafeStyleSheet> stylesheets) { int concatLength = 0; for (SafeStyleSheet stylesheet : stylesheets) { concatLength += stylesheet.getSafeStyleSheetString().length(); } StringBuilder result = new StringBuilder(concatLength); for (SafeStyleSheet stylesheet : stylesheets) { result.append(stylesheet.getSafeStyleSheetString()); } return create(result.toString()); }
/** * Creates a SafeStyleSheet from the given compile-time constant {@code resourceName} using * the given {@code charset}. * * <p>This performs ZERO VALIDATION of the data. We assume that resources should be safe because * they are part of the binary, and therefore not attacker controlled. * * @param contextClass Class relative to which to load the resource. */ @GwtIncompatible("Resources") public static SafeStyleSheet fromResource( Class<?> contextClass, @CompileTimeConstant final String resourceName, Charset charset) throws IOException { return create(Resources.toString(Resources.getResource(contextClass, resourceName), charset)); }