63

My program need to parse css files into an in-memory object format. Any advice on how this should be done ?

Softlion
  • 11,311
  • 10
  • 52
  • 78
Ken
  • 4,754
  • 6
  • 27
  • 25
  • 21
    I don't get why this question was closed. It has got a lot of votes from people apparently wondering the same thing. Just because different people might have different opinions about the best solution, what a great place to list those opinions for others with the same question. – Jonathan Wood Sep 06 '13 at 15:31
  • 3
    @JonathanWood - I entirely agree, but the policy is that tool recommendations are off topic, and it is asking for a tool recommendation. So closing it is appropriate, even if frustrating. – Bobson Oct 18 '13 at 13:22
  • I've rewroten the question so it can be reopened – Softlion Oct 20 '15 at 09:56
  • @Softlion, by doing so you seem to have invalidated all six of the existing answers, none of which talk about Linq. IMO it would be preferable to rollback, post a new question, and then link to that new question in a comment on this one. – Peter Taylor Oct 21 '15 at 07:33
  • You were right, I removed "Linq" as it was not present in the initial question. – Softlion Oct 21 '15 at 13:31
  • When I searched for a css parser in C#, I stumbled upon this question so it is obviously valuable. – Anders Lindén Dec 02 '20 at 11:37

6 Answers6

27

ExCSS (supports CSS2.1 and CSS3) on GitHub: https://github.com/TylerBrinks/ExCSS.

Which is a newer version of the code project article: http://www.codeproject.com/KB/recipes/CSSParser.aspx

deerchao
  • 10,012
  • 8
  • 49
  • 59
Jon Tackabury
  • 42,888
  • 45
  • 121
  • 164
  • wish it would parse out the media query into an object model. it's not particularly hard to do so but all it gives you is a string – Simon_Weaver Dec 29 '14 at 02:57
  • 1
    it works, but found a number of things it can't parse: `calc(50vw - 23em)` becomes `calc(50vw = 23em)` / `3rem` becomes `3` / `z-index: 2147483647` becomes `z-index: 2.147484E+09` / `*display: none` becomes `display: none` – Simon_Weaver Dec 30 '14 at 01:47
  • 1
    It also does not handle `@-ms-viewport` returning `{System.Collections.Generic.List\`1[ExCSS.RuleSet]}` in output. – Herman Kan Apr 28 '15 at 07:01
8

And a slightly slower search turns up the blog post "CSS parser class in .NET" which embeds this gist on GitHub (in case the blog ever dies).

mercator
  • 27,075
  • 8
  • 59
  • 71
6

There is a CSS grammar file for GoldParser:

http://goldparser.org/grammars/files/css.zip

GoldParser is easy to include in a C# project, and generates an real LALR parser - not some regex hack.

Borislav Ivanov
  • 3,299
  • 2
  • 26
  • 48
  • LALR parser - not some regex Info on their site "GOLD grammars are based on Backus-Naur form and regular expressions" – Justin Jul 08 '09 at 21:57
  • 1
    Good point, regexs are cool. Rolling a parser by hand isn't –  Jul 10 '09 at 15:34
  • This is a very simplified version of the CSS grammar. Instead I ported the CSS reference grammer (from the CSS 2.1 spec) to work with the GOLD parser ... and I've written a C# assembly to implement the rest of it (use the parser output to compute the style for an element in a DOM, using rule specificity, inheritance, default values, etc.). – ChrisW Sep 18 '09 at 14:24
  • 1
    Goldparser is nice but it is extremely slow. Even in speed optimized C++ code it takes 10 seconds to parse a 15000 lines of code. If you compare this with the speed of the PHP parser this is extremely slow. – Elmue Sep 26 '13 at 02:54
3

Have you tried the one featured in JsonFx? It's written in C#, parses CSS3 syntax and is distributed under a MIT style license.

m0sa
  • 10,082
  • 3
  • 38
  • 85
  • 2
    Looks like the best of a bad bunch (recursion using `goto` statements - ouch) – satnhak Jun 16 '11 at 14:30
  • 4
    @B: `goto` is a great way to *avoid* recursion, because it avoids re-entering a function. It's a great alternative to risking a stack overflow when you have an potentially high number of iterations. – Triynko Aug 30 '11 at 17:29
  • ...because you risk a stack overflow when you have a potentially high number of iterations. :D – James World Mar 26 '12 at 22:51
  • 1
    Tail recursion to the rescue.... – Basic Jan 17 '15 at 21:01
1

I wrote one, using the grammar specified in the CSS 2.1 spec. I have also released it now: for details, see http://www.modeltext.com/css/

ChrisW
  • 51,820
  • 11
  • 101
  • 201
0

Here you can find another one especially for C# with sample source.

dajood
  • 3,307
  • 7
  • 41
  • 65