Is there a CSS object model or CSS querying api for .net?
Asked Answered
I

1

9

Is there a library out there that will allow me to write the following kind of code, which parses CSS and returns a queryable object model

string input = "p, span { font-family: arial; }";
var cssRules = new Parser().Parse(input);
var rule = cssRules.Find(new Selector("p")).First();

Assert.That(rule.Attribute("font-family").Value, Is.Equal.To("arial"));

I've taken a look at dotless, downloaded their code and examined some of the relevant unit tests and fixtures. It looks promising but I can't quite work out how to use it to parse and query plain CSS.

Inflexed answered 25/7, 2011 at 10:32 Comment(3)
Assert.That(rule.Attribute("font-family").Value, Is.Equal.To("arial")); is awfully neat. What library does that come from?Hygienist
@Hygienist NUnit (the 'constraint' model )Hyperbola
You could also take a look at the Microsoft AJAX Minifier (ajaxmin.codeplex.com). It includes source code for building a complete, in-memory representation of a CSS file. No support for querying, but it could be an option for the parsing part of the problem. Another bonus is that if you hook it up to your builds, it will validate the syntax of the CSS file and emit errors and warnings just like any other compiler.Piggy
C
1

The closest I know is CssParser from jsonfx.net:

http://css-parser.googlecode.com/svn/trunk/CssParser/

You can parse any css and browse through selectors afterwards using StyleSheet property of CssParser

Choong answered 25/7, 2011 at 22:41 Comment(1)
Yep, this works. Syntax is a bit awkward for what I'm trying to do, but butter than writing my own code :) Thanks for the link.Inflexed

© 2022 - 2024 — McMap. All rights reserved.