views:

211

answers:

1

I need to parse CSS files in Java, and have tried using the Batik and CSSParser libs with success. The issue I am having is that when I run into IE hacks, I loose the formatting; it appears to me that the DOM used by org.w3c.css.sac won't accommodate the IE Hacks.

e.g.-

/*   The '\' isn't retained     */

someselector {  
    padding: 10px;
    width: 200px;  
    w\idth: 180px;  
    height: 200px;  
    heigh\t: 180px;  
}


div.content { 
    width:400px; 
    voice-family: "\"}\""; 
    voice-family:inherit;
    width:300px;
}



/*  the space between 'body' and the '.' isn't retained */

html>body .content {
  width:300px;
}

Has anyone had any experience with this and can recommend a good solution?

+2  A: 

Probably not your ideal solution ... but it would be safer, albeit much less convenient, to use IE conditional comments and put all the IE code in a separate CSS file to avoid using hacks.

tedmiston
Yes, I've thought of this. Using CSSParser, on the W3C compliant CSS, and then moving the IE hacks to a separate file, and building my own deterministic parsing engine. The CSS files I need to parse have about 3000 selectors, and there are only 20-30 hack. I've already written a simple utility that parses the file, regenerates it, then compares the two, and also captures CSSParsers errors for poorly formed styles, so they can be corrected. This same utility could be help build the and test the 'new' IE hack parser. An OK solution, just not my ideal one.
Gene M.
As so much time has passed and no one has offered a solution, I'm accepting this answer. In fact, I essentially did as suggested. Put the IE (hacks) into their own file, and evened up writing a parser just for this. If nothing else, it minimised risk and reduced the scope of development and testing.
Gene M.