Looks like you have done your research on this well. It is not possible by accessing the CSSStyleDeclaration
or the CSSRuleList
object as unindentified properties get filtered out, which is a an implementation bug in most major browsers as they are supposed to list all properties - even ones they don't understand. See a related question for more on this.
The remaining alternatives require a bit too much work as @Sarfaraz already mentioned. It all boils down to parsing everything by hand and re-doing the work browsers have already done for us. Assuming your style declarations are inline, either in <style>
tags, or as an element's style attribute, then you would have to parse the text and construct a map of all interesting CSS3 properties. With an inline-attribute, it's basically parsing text somewhat similar to:
`prop1: value; prop2: value2; ..`
See @Nick Craver's answer to a similar question, where he has linked to a regex approach for doing this.
With inline tags, the text to be parsed will be of the form:
[selector] {
prop1: value1;
prop2: value2;
...
}
...
This is an oversimplification ignoring all grammar production rules, and there are loads of issues to be handled here.
If the stylesheets are external, and are referenced inside the page, then you would have to lookup the <link>
elements having an attribute rel
equal to "stylesheet"
, and get their href
property. Also, parse the url for imported stylesheets. Once you have these urls, you can make an AJAX call to fetch the actual contents of these stylesheets assuming nothing is blocking the call on the server side. Once you have the text from each file, it's basically a parsing step same as above, and adding necessary rules using insertRule
or addRule
depending on the browser.
There may be open-source CSS parsers that could help you in this.
This may be a long way of saying that I think @jrista's solution is a pretty good one. If you don't want to write all permutations of vendor-specific code, then an easier and better approach would be to have a CSS generation layer that's part of some build process which generates all permutations and spits out CSS files.
This layer could also contain optimizations by inspecting the User-Agent
header and only generate the required styles, which is essentially what GWT does (but it generates all possible permutations beforehand, instead of doing it at runtime).
This may be a serious undertaking, so at the end of the day, you have to evaluate your options and see if the effort of achieving code-purity outweighs the benefits of having a little redundancy.