下面列出了怎么用org.w3c.dom.css.CSSStyleDeclaration的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Parses and creates a style declaration.
*
* @param value
* The style declaration text.
*/
public CSSStyleDeclaration parseStyleDeclaration( String value )
{
styleDeclarationBuilder.styleDeclaration = new StyleDeclaration( this );
try
{
parser.setDocumentHandler( styleDeclarationBuilder );
parser.parseStyleDeclaration( new InputSource( new StringReader(
value ) ) );
}
catch ( Exception e )
{
/** @todo: logout the error message */
}
return styleDeclarationBuilder.styleDeclaration;
}
/**
* Gets all the name/value pairs from a CSS declaration and puts them into a
* <code>LinkedHashMap</code>. All the name and value is of string type.
*
* @param declaration
* the declaration of the style rule
* @param errors
* the error list of the declaration
* @return all the supported name/value pairs
*/
LinkedHashMap<String, ? extends Object> buildProperties(
CSSStyleDeclaration declaration,
List<StyleSheetParserException> errors )
{
LinkedHashMap<String, String> properties = new LinkedHashMap<String, String>( );
for ( int i = 0; i < declaration.getLength( ); i++ )
{
String cssName = declaration.item( i );
String cssValue = declaration.getPropertyValue( cssName );
if ( StringUtil.isBlank( cssName ) || StringUtil.isBlank( cssValue ) )
continue;
properties.put( cssName, cssValue );
}
return buildProperties( properties, errors );
}
public static String escapeIFrameCss(String orig) {
String rule = "";
CSSOMParser parser = new CSSOMParser();
try {
List<String> rules = new ArrayList<>();
CSSStyleDeclaration decl = parser.parseStyleDeclaration(new InputSource(new StringReader(orig)));
for (int i = 0; i < decl.getLength(); i++) {
String property = decl.item(i);
String value = decl.getPropertyValue(property);
if (StringUtils.isBlank(property) || StringUtils.isBlank(value)) {
continue;
}
if (ALLOWED_IFRAME_CSS_RULES.contains(property) && StringUtils.containsNone(value, FORBIDDEN_CSS_RULE_CHARACTERS)) {
rules.add(property + ":" + decl.getPropertyValue(property) + ";");
}
}
rule = StringUtils.join(rules, "");
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return rule;
}
public static String escapeImgCss(String orig) {
String rule = "";
CSSOMParser parser = new CSSOMParser();
try {
List<String> rules = new ArrayList<>();
CSSStyleDeclaration decl = parser.parseStyleDeclaration(new InputSource(new StringReader(orig)));
for (int i = 0; i < decl.getLength(); i++) {
String property = decl.item(i);
String value = decl.getPropertyValue(property);
if (StringUtils.isBlank(property) || StringUtils.isBlank(value)) {
continue;
}
if (ALLOWED_IMG_CSS_RULES.contains(property) && StringUtils.containsNone(value, FORBIDDEN_CSS_RULE_CHARACTERS)) {
rules.add(property + ":" + decl.getPropertyValue(property) + ";");
}
}
rule = StringUtils.join(rules, "");
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return rule;
}
/**
* add a style definition into the report.
*
* @param style
* style definition.
*/
public void addStyle( String name, CSSStyleDeclaration style )
{
assert ( style != null );
this.styles.add( style );
this.styleTable.put( name, style );
}
private String parseStyle( String cssText )
{
try
{
CSSStyleDeclaration style = engine.parseStyleDeclaration( cssText );
return style.getCssText( );
}
catch ( Exception ex )
{
}
return "";
}
public static Color toSWTColor(Element element, String attributeName) {
Document document = element.getOwnerDocument();
ViewCSS viewCSS = (ViewCSS) document.getDocumentElement();
CSSStyleDeclaration computedStyle = viewCSS.getComputedStyle(element, null);
SVGPaint svgPaint = (SVGPaint) computedStyle.getPropertyCSSValue(attributeName);
if (svgPaint.getPaintType() == SVGPaint.SVG_PAINTTYPE_RGBCOLOR) {
RGBColor rgb = svgPaint.getRGBColor();
float red = rgb.getRed().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE);
float green = rgb.getGreen().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE);
float blue = rgb.getBlue().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE);
return new Color(Display.getCurrent(), (int) red, (int) green, (int) blue);
}
return null;
}
public CSSStyleDeclaration getStyle( )
{
return this.style;
}