📜 ⬆️ ⬇️

CSS parser for .NET written in C #

It took me once to @import the CSS to remove @import , url() . But for .NET there were only varying degrees of crooked crafts. The best library was ExCSS, but it was bent on such trivial things as media queries. So I decided to fill the gap.

There were options: unzip Chrome, pick Firefox, pick the left library. It was necessary to guarantee the quality and regular updates, so the last option disappeared. In Chrome, CSS and HTML parsing was generated on the basis of grammars, and a cursory examination of the variety of tools for .NET was disheartening, let alone the compatibility of tools, so Chrome was dropped. Firefox remained with manually written parsers.

(...)
')
Long or shortly, the library turned out as a result, which fully parses the CSS files fed to it. Tests in Firefox turned out to be too problematic to convert, and they are not unit tests at all: written in JS, lie inside HTML. Therefore, to fully check the work of the library proved problematic. If anyone has any suggestions on where to get good unit tests for CSS, I’ll take all the attention. I really hope that the library will be useful to anyone, and if problems are discovered, I will be informed about it.

What is there




What is not available



Encoding, modification, serialization, .NET 4.0 and the NuGet package I plan to add. How soon it will happen depends on whether someone needs it.

Usage example


 //  CSS,  URL  ( )   URL (   ) CssStyleSheet css = new CssLoader().ParseSheet("h1, h2 { color: #123; }", "http://example.com/sheet.css", "http://example.com/"); Console.WriteLine(css.SheetUri); // http://example.com/sheet.css //   ( ) Console.WriteLine(css.StyleRules.Single().Declaration.Color.Color.R); // 17 Console.WriteLine(css.Rules.OfType<CssStyleRule>.Single().Declaration .Data.Single().Color.R); // 17 Console.WriteLine(css.Rules.OfType<CssStyleRule>.Single().Declaration .GetValue(CssProperty.Color).Color.R); // 17 //      Console.WriteLine(css.StyleRules.Single().SelectorGroups.First().Selectors.Single().Tag); // h1 


Build project




License


Mozilla Public License . BSD cross with GPL. Virus, like the GPL, but infects only individual files of the sorts with the code MPL. All the rest of the license does not care. There are no restrictions on use with projects under other licenses, including commercial ones with closed code - no.

Understandably, I would prefer to release the library under a more liberal and understandable BSD / MIT license, but the MPL “infected” most of the files, so there were no options left.

Results


The library is written. Whether it will develop depends on whether it will be used. I myself only need a small part. I hope for bug reports, and maybe even pull requests, if there are bold ones.

If you need a CSS parser, then please write how you are going to use it. It is very important to understand the future path of development and priorities.

Links



PS How I converted the library, what goals I pursued, etc. - truncated in ReadMe on GitHub. My attempt to tell about the hardships Habr did not appreciate, so only dry facts. All good.

Source: https://habr.com/ru/post/190820/


All Articles