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?

Kara
  • 6,115
  • 16
  • 50
  • 57
Gene Myers
  • 1,230
  • 1
  • 14
  • 31
  • Collapsing `html>body .content` to `html>body.content` isn't just a whitespace issue, it changes the meaning of the selector entirely. – Matthew Scharley Dec 05 '09 at 20:46
  • This is a very strong point against using hacks. – Pekka Dec 05 '09 at 20:46
  • Matthew- I realise that its not just a whitespace issue- I need to retain the whitespace so the selector will work, I know. – Gene Myers Dec 05 '09 at 20:56
  • Pekka- It's a very strong point against using IE, actually. But since I can't control that, or that people will write the hacks, I need a solution. – Gene Myers Dec 05 '09 at 20:57

2 Answers2

2

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.

Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76
  • 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 Myers Dec 07 '09 at 00:29
  • 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 Myers Feb 24 '10 at 21:51
0

I saw this question a bit too late, but it might be helpful anyway. With the ph-css library (https://github.com/phax/ph-css) you can parse CSS2 and CSS3 incl. IE hacks (*, $, etc.)

hth, Philip

Philip Helger
  • 1,814
  • 18
  • 28