CSS4J
FAQ

CSS4J FAQ

Click on the question to see the answer.

Are W3C's CSSValue and CSSPrimitiveValue interfaces supported?

That API was deprecated by W3C but version 1.0 does implement them. Version 2.0 uses those names but the actual interfaces are different and not backwards-compatible.

How do I access the document style sheets?

The document style sheets are a collection of style sheets associated with the document. W3C's DOM Level 2 Style standard provides the DocumentStyle interface for that, see also Extensions to the Document Interface in the more modern CSSOM. That is, just use document.getStyleSheets() to get the list of style sheets.

Can I get the merged style sheet that applies to the document?

The document.getStyleSheet() method does that. Although you can add style rules to it, you may consider using overriden styles or the user-defined style sheet (neither are included in the sheet returned by that method) instead.

How can I remove all the rules in a style sheet?

Use sheet.getCssRules().clear().

What is the difference between CSSParser and CSSOMParser? [2.0]

When parsing media queries, the CSSOMParser class has an object-model internal factory that directly produces CSSOM-enabled media queries, while CSSParser has a -less useful- factory based on NSAC lexical units.

If you want to parse media queries you probably want to use CSSOMParser, otherwise CSSParser should be adequate.

Does css4j depend on dom4j?

No, dom4j is just an available DOM backend.

Does css4j work with dom4j 1.x or 2.x?

Although the provided Maven build requires dom4j 2.x, the resulting jar file can be used in runtime with 1.x too. Css4j makes no use of the methods that dom4j implements in 2.x but not in 1.6, so it is safe to use with 1.6. Recent versions aren't tested with 1.6 however.

Using the dom4j backend, I find that it does not support using the document's uri to retrieve relative urls. How can I work around?

You can manually set the base url, doing something like:

if (document.getBaseURL() == null) {
	document.setBaseURL(baseUrl);
}

Does the DOM4J back-end make use of the DOM wrapper, internally?

No, they are independent back-ends.

How do I set up my document for computing paged-media styles?

Use document.setTargetMedium("print") and then compute the styles. setTargetMedium rebuilds the style cascade for the new medium.

I have set up a style database, but the library complains about missing it.

Be sure to call document.setTargetMedium("medium"), where the medium must be the same for which the style database was set.

Are the minification methods recommended for production use?

This library's support for minification (getMinifiedCssText() and related methods) is not intended as a general-purpose minification solution, but could be useful as part of a wider CSS toolchain, perhaps with a specialized minifier at its end.

If you decide to use the minification methods in 1.0 be sure to use this library's internal parser, and not the other SAC parsers. And if you use any kind of Internet Explorer hack, use a sheet factory with the proper NSAC parser flags set. Some IE hacks are supported, but others may give unexpected results so you should be very careful about that.