
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
- All rules, properties, values, etc. supported by Mozilla Firefox are supported. FF-specific extensions (
-moz-*
) including. - Two compatibility modes: Full Standards (strict adherence to standards) and Quirks (you can omit the unit of measurement and allow other similar liberties).
- All values ​​are parsed into complex structures. The brief
background
property will expand into several properties, including background-image
, which contains a list of background images, each of which is a URL or gradient; in the latter case, the gradient will contain individual points and all parameters. - Error handling in accordance with all specifications. If something does not recognize, then the parser will just skip an incomprehensible piece.
- Detailed error logging. All warnings about the wrong syntax and properties are dumped into the
TraceSource "Alba.CsCss.CssParser"
and are thrown by the event.
What is not available
- Encoding support. In unicode, you need to throw it yourself.
- Modification and conversion back to string.
- DOM CSS. The interface in terms of C # coding standards is very dubious, so the benefits are in question.
- Properties with vendor prefixes of other browsers (-webkit-, -ms-, -o-) are ignored.
- .NET 4.0 and below. From .NET 4.5, perhaps
IReadOnlyList
, but for now it's a little lazy to mess with the versions. - Package NuGet. Syrovata is a library.
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
Build project
Alba.CsCss
- the library itself. No dependencies on other projects. If you want to use the library in your solution, it is enough to include this project.Alba.CsCss.Tests
- unit tests. The number is such that in a decent society it is customary to be silent.Alba.Framework
- personal collection of bicycles framework. Simplifies code in T4 transformations. To run these, you need to build a debug version.Alba.Framework.CodeGeneration
- T4 part of the framework. It should be collected under the admin account to install the custom tool "AttachT4" (related to T4 Toolbox, only without a ton of unnecessary features). It is necessary, if you want to work comfortably with T4 in the project.Alba.Framework.Testing
- used in tests.
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.