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?

4
  • Collapsing html>body .content to html>body.content isn't just a whitespace issue, it changes the meaning of the selector entirely. Commented Dec 5, 2009 at 20:46
  • This is a very strong point against using hacks. Commented Dec 5, 2009 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. Commented Dec 5, 2009 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. Commented Dec 5, 2009 at 20:57

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.