com.google.gwt.resources.client.CssResource Maven / Gradle / Ivy
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.resources.client;
import com.google.gwt.resources.ext.DefaultExtensions;
import com.google.gwt.resources.ext.ResourceGeneratorType;
import com.google.gwt.resources.rg.CssResourceGenerator;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Aggregates and minifies CSS stylesheets. A CssResource represents a regular
* CSS file with GWT-specific at-rules.
*
* Currently-supported accessor functions:
*
*
* - {@code String someClassName();} will allow the css class
*
.someClassName
to be obfuscated at runtime. The function will
* return the obfuscated class name.
* - {@code
someDefName();} will allow
* access to the values defined by {@literal @def} rules within the CSS file.
* The defined value must be a raw number, a CSS length, or a percentage value
* if it is to be returned as a numeric type.
*
*
*
* Currently-supported rules:
*
*
* - {@code @def NAME replacement-expression; .myClass background: NAME;}
* Define a static constant. The replacement expression may be any CSS that
* would be valid in a property value context. A {@code @def} may refer to
* previously-defined rules, but no forward-references will be honored.
* - {@code @eval NAME Java-expression; .myClass background: NAME;} Define a
* constant based on a Java expression.
* - {@code @external class-name, class-name, ...;} Disable obfuscation for
* specific class selectors and exclude those class selectors from strictness
* requirements.
* {@literal @if} [!]property list of values {ruleBlock}
Include or
* exclude CSS rules based on the value of a deferred-binding property. Also
* {@code @elif} and {@code @else} follow the same pattern.
* This might look like {@code @if user.agent ie6 safari ...}.
* {@literal @if} (Java-expression) {ruleBlock}
Include or exclude
* CSS rules based on a boolean Java expression.
* {@literal @noflip} { rules }
will suppress the automatic
* right-to-left transformation applied to the CSS when the module is compiled
* for an RTL language.
* -
*
{@literal @}sprite .any .selector {gwt-image: "imageResourceFunction";}
* . The appearance, size, and height of the sprite will be affected by any
* {@link ImageResource.ImageOptions} annotations present on the related
* {@link ImageResource} accessor function. Additional properties may be
* specified in the rule block.
* - {@code @url NAME siblingDataResource; .myClass background: NAME
* repeat-x;} Use a {@link DataResource} to generate a
url('...'}
value.
*
*
*
* Currently-supported CSS functions:
*
*
* - {@code literal("expression")} substitutes a property value that does not
* conform to CSS2 parsing rules. The escape sequences {@code \"} and {@code \\}
* will be replaced with {@code "} and {@code \} respectively.
*
- {@code value("bundleFunction.someFunction[.other[...]]" [, "suffix"])}
* substitute the value of a sequence of named zero-arg function invocations. An
* optional suffix will be appended to the return value of the function. The
* first name is resolved relative to the bundle interface passed to
* {@link com.google.gwt.core.client.GWT#create(Class)}. An example:
*
*
* .bordersTheSizeOfAnImage {
* border-left: value('leftBorderImageResource.getWidth', 'px') solid blue;
* }
*
*
*
*
*
* Any class selectors that do not correspond with a String accessor method in
* the return type will trigger a compilation error. This ensures that the
* CssResource does not contribute any unobfuscated class selectors into the
* global CSS namespace. Strict mode can be disabled by annotating the
* ClientBundle method declaration with {@link NotStrict}, however this is only
* recommended for interacting with legacy CSS.
*
*
* Given these interfaces:
*
*
* interface MyCss extends CssResource {
* String someClass();
* }
*
* interface MyBundle extends ClientBundle {
* {@literal @Source("my.css")}
* MyCss css();
* }
*
*
* the source CSS will fail to compile if it does not contain exactly the one
* class selector defined in the MyCss type.
*
* The {@code @external} at-rule can be used in strict mode to indicate that
* certain class selectors are exempt from the strict semantics. Class selectors
* marked as external will not be obfuscated and are not required to have string
* accessor functions. Consider the following example in conjunction with the
* above MyCss
interface:
*
*
* {@literal @external} .foo, .bar;
* .foo .someClass .bar { .... }
*
*
* The resulting CSS would look like:
*
*
* .foo .A1234 .bar { .... }
*
*
* If a String foo()
method were defined in MyCss
, it
* would return the string value "foo
".
*
* The utility tool com.google.gwt.resources.css.InterfaceGenerator
* can be used to automatically generate a Java interface from a
* CssResource-compatible CSS file.
*
* @see CssResource design doc
*/
@DefaultExtensions(value = {".css"})
@ResourceGeneratorType(CssResourceGenerator.class)
public interface CssResource extends ResourcePrototype {
/**
* The original CSS class name specified in the resource. This allows CSS
* classes that do not correspond to Java identifiers to be mapped onto
* obfuscated class accessors.
*
*
* .some-non-java-ident { background: blue; }
*
* interface MyCssResource extends CssResource {
* {@literal @}ClassName("some-non-java-ident")
* String classAccessor();
* }
*
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ClassName {
String value();
}
/**
* Makes class selectors from other CssResource types available in the raw
* source of a CssResource. String accessor methods can be referred to using
* the value of the imported type's {@link ImportedWithPrefix} value.
*
* This is an example of creating a descendant selector with two unrelated
* types:
*
*
*{@literal @ImportedWithPrefix}("some-prefix")
* interface ToImport extends CssResource {
* String widget();
* }
*
*{@literal @ImportedWithPrefix}("other-import")
* interface OtherImport extends CssResource {
* String widget();
* }
*
* interface Resources extends ClientBundle {
* {@literal @Import}(value = {ToImport.class, OtherImport.class})
* {@literal @Source}("my.css")
* CssResource usesImports();
* }
*
* my.css:
* // Now I can refer to these classes defined elsewhere with no
* // fear of name collisions
* .some-prefix-widget .other-import-widget {...}
*
*
* If the imported CssResource type is lacking an {@link ImportedWithPrefix}
* annotation, the simple name of the type will be used instead. In the above
* example, without the annotation on ToImport
, the class
* selector would have been .ToImport-widget
. Notice also that
* both interfaces defined a method called widget()
, which would
* prevent meaningful composition of the original interfaces.
*
* It is an error to import multiple classes with the same prefix into one
* CssResource.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Import {
Class extends CssResource>[] value();
}
/**
* Specifies the string prefix to use when one CssResource is imported into
* the scope of another CssResource.
*
* @see Import
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ImportedWithPrefix {
String value();
}
/**
* The presence of this annotation on a CssResource accessor method indicates
* that any class selectors that do not correspond with a String accessor
* method in the return type or an {@code @external} declaration should not
* trigger a compilation error. This annotation is not recommended for new
* code.
*
*
* interface Resources extends ClientBundle {
* {@literal @NotStrict}
* {@literal @Source}("legacy.css")
* CssResource css();
* }
*
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface NotStrict {
}
/**
* Indicates that the String accessor methods defined in a CssResource will
* return the same values across all implementations of that type.
*
* This is an example of "stateful" class selectors being used:
*
*
*{@literal @Shared}
* interface FocusCss extends CssResource {
* String focused();
* String unfocused();
* }
*
* interface PanelCss extends CssResource, FocusCss {
* String widget();
* }
*
* interface InputCss extends CssResource, FocusCss {
* String widget();
* }
*
* input.css:
* *.focused .widget {border: thin solid blue;}
*
* Application.java:
* myPanel.add(myInputWidget);
* myPanel.addStyleName(instanceOfPanelCss.focused());
*
*
* Because the FocusCss
interface is tagged with {@code @Shared},
* the focused()
method on the instance of PanelCss
* will match the .focused
parent selector in
* input.css
.
*
* The effect of inheriting an {@code Shared} interface can be replicated by
* use use of the {@link Import} annotation (e.g. {@code .FocusCss-focused
* .widget}), however the use of state-bearing descendant selectors is common
* enough to warrant an easier use-case.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Shared {
}
/**
* This annotation is a no-op.
*
* @deprecated Strict mode is now the default behavior for CssResource
*/
@Deprecated
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Strict {
}
/**
* Calls
* {@link com.google.gwt.dom.client.StyleInjector#injectStylesheet(String)} to
* inject the contents of the CssResource into the DOM. Repeated calls to this
* method on an instance of a CssResources will have no effect.
*
* @return true
if this method mutated the DOM.
*/
boolean ensureInjected();
/**
* Provides the contents of the CssResource.
*/
String getText();
}