Visual Studio 11 CSS3 Parser
Was spelunking in the folders of my Visual Studio 11 installation a couple of weeks ago and found Microsoft.CSS.Core.dll (located in C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\Web\CSS3).
I was intrigued and needed to look deeper and what I found was a CSS parser that you can use from your application e.g. I needed a CSS unpacker (that turns a minimized CSS file into a readable format). With the built in formatter I was able to do this:
static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("cssunpack [input] [output]"); Environment.Exit(-1); } var parser = new CssParser(); var css = parser.Parse(File.ReadAllText(args[0]), insertComments: false); var formatter = new CssFormatter(); File.WriteAllText(args[1], formatter.Format(css)); }
So if we do some small tweaks to the CssFormatterOptions we can build a minimizer as well:
static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("csspack [input] [output]"); Environment.Exit(-1); } var parser = new CssParser(); var css = parser.Parse(File.ReadAllText(args[0]), insertComments: false); var formatter = new CssFormatter { Options = new CssFormattingOptions { IndentType = IndentType.Spaces, IndentSize = 0, SpacesPerTab = 0, InitialIndentString = string.Empty, QuoteType = QuoteType.Double, BlockBracePosition = BracePosition.Compact, SortProperties = false, CompactBlocks = false, CompactBlockThreshold = 0, RemoveEscapes = true, ConvertColorsToHex = true, CompressColors = true, ElementSelectorCasing = Casing.Lowercase, PropertyNameCasing = Casing.Lowercase, RemoveLastSemicolon = true, IndentRuleHierarchy = false, ForStyleBlock = true } }; File.WriteAllText(args[1], formatter.Format(css).Replace(Environment.NewLine, "")); }
There is a lot of more features so take a look for yourself.
I don’t know about the license but it’s probably ok to use on your local dev machine if you have VS11 installed.
