All Projects → Athari → Cscss

Athari / Cscss

Licence: mpl-2.0
CSS parsing library for C# based on Mozilla Firefox code [MPL]

Projects that are alternatives of or similar to Cscss

Bubbles
⚡️A library for adding messenger style floating bubbles to any android application 📲
Stars: ✭ 66 (-5.71%)
Mutual labels:  library
Hazelcast Cpp Client
Hazelcast IMDG C++ Client
Stars: ✭ 67 (-4.29%)
Mutual labels:  library
Androidaudioconverter
Convert audio files inside your Android app easily. Supported formats: AAC, MP3, M4A, WMA, WAV and FLAC.
Stars: ✭ 1,156 (+1551.43%)
Mutual labels:  library
Dmxusb
DMXUSB emulates an ENTTEC-compatible DMXKing USB to DMX serial device with one, two, or n universes.
Stars: ✭ 66 (-5.71%)
Mutual labels:  library
Evalne
Source code for EvalNE, a Python library for evaluating Network Embedding methods.
Stars: ✭ 67 (-4.29%)
Mutual labels:  library
Timberlorry
Periodical log collector framework.
Stars: ✭ 67 (-4.29%)
Mutual labels:  library
Poi
Mirror of Apache POI
Stars: ✭ 1,136 (+1522.86%)
Mutual labels:  library
Tanya
GC-free, high-performance D library: Containers, networking, metaprogramming, memory management, utilities
Stars: ✭ 70 (+0%)
Mutual labels:  library
Markdown Builder
1kb Markdown builder for Node.js
Stars: ✭ 67 (-4.29%)
Mutual labels:  library
Anglesharp.js
👼 Extends AngleSharp with a .NET-based JavaScript engine.
Stars: ✭ 68 (-2.86%)
Mutual labels:  library
Stereo
A Flutter plugin for playing music on iOS and Android.
Stars: ✭ 66 (-5.71%)
Mutual labels:  library
Cordova Plugin Test Framework
Apache Cordova
Stars: ✭ 66 (-5.71%)
Mutual labels:  library
React Native X Bar
🎩 A flexible, lightweight bar component for common UI patterns in React Native
Stars: ✭ 68 (-2.86%)
Mutual labels:  library
P5.clickable
Event driven, easy-to-use button library for P5.js 👆
Stars: ✭ 66 (-5.71%)
Mutual labels:  library
Egeo
EGEO is the open-source UI library used to build Stratio's UI. It includes UI Components, Utilities, Services and much more to build user interfaces quickly and with ease. The library is distributed in AoT mode.
Stars: ✭ 69 (-1.43%)
Mutual labels:  library
Litter
Litter is a pretty printer library for Go data structures to aid in debugging and testing.
Stars: ✭ 1,137 (+1524.29%)
Mutual labels:  library
Notifier
Notifications library made with VanillaJS.
Stars: ✭ 67 (-4.29%)
Mutual labels:  library
Retry
A Rust library to retry some code until its return value satisfies a condition.
Stars: ✭ 70 (+0%)
Mutual labels:  library
Bitcoin Elixir
Bitcoin tools and full node implementation in Elixir.
Stars: ✭ 70 (+0%)
Mutual labels:  library
Andes
Python toolbox / library for power system transient dynamics simulation with symbolic modeling and numerical analysis 🔥
Stars: ✭ 68 (-2.86%)
Mutual labels:  library

CsCss: .NET CSS Parser

https://github.com/Athari/CsCss

CsCss is a CSS Parser written in C#, based on Mozilla Firefox 22 code.

At the time of writing, it is the only C# CSS Parser capable of parsing all varieties of CSS supported by modern browsers. However, the choice of the library is not clear-cut, as some important features are missing. See below for details.

The library should be considered alpha-quality, as most of its features are not tested neither by tests no in real world. If you notice any issues, please report them!

Features

  • Supports parsing of all CSS rules, properties, values etc. supported by Mozilla Firefox. Mozilla-specific extensions (-moz-*) are supported too.
  • Two modes of compatibility: FullStandards (default) and Quirks (units are optional etc.).
  • Values are parsed into complex structures. For example, shortcut background property is converted into multiple longhand properties, one of which, background-image, is represented by a list of values corresponding to multiple backgrounds; each value in the list can be a URL or a gradient structure; in case of a gradient it contains a list of gradient stops.
  • Error detection and handling according to CSS specification. Parsing will not stop if any unsupported feature is used in the CSS file, parser will just skip to the next declaration.
  • Detailed error logging. All warning messages about unsupported or invalid CSS properties or syntax are logged into a TraceSource named Alba.CsCss.CssParser and are reported as event on CssLoader.ParsingError.
  • Installation via NuGet with debugging symbols and sources via SymbolSource.

Not supported (ToDo)

  • Different encodings. You need to convert string to Unicode before passing it to CssLoader.
  • Modifying of a parsed style sheet and serializing it back into string.
  • Document Object Model CSS. DOM methods do not comply with C# coding standards, so the necessity is questionable.
  • Prefixed and vendor-specific CSS properties (-webkit-, -ms-, -o-*, zoom etc.) are ignored.
  • Unit tests are almost non-existent. Tests for CSS from Firefox are written in JavaScript and as such are very problematic to convert to C#.

Examples

Getting color of H1 header style defined in a CSS file

Two URLs are specified: the first one is used for logging puposes; the second one is used as a base for resolving relative url() expressions.

CssStyleSheet css = new CssLoader().ParseSheet("h1, h2 { color: #123; }",
    new Uri("http://example.com/sheet.css"), new Uri("http://example.com/"));
Console.WriteLine(css.SheetUri); // http://example.com/sheet.css
// Get color property (equivalent code)
Console.WriteLine(css.StyleRules.Single().Declaration.Color.Color.R); // 17
Console.WriteLine(css.Rules.OfType<CssStyleRule>().Single().Declaration
                               .GetValue(CssProperty.Color).Color.R); // 17
// Get h1 selector
Console.WriteLine(css.StyleRules.Single().SelectorGroups.First().Selectors.Single().Tag);
Extracting all URLs from a CSS file (using tokenizer)

This method will not analyze wether CSS file is valid, it will merely find all url() expressions.

List<string> uris = new CssLoader().GetUris(source).ToList();
Extracting all URLs from a CSS file (using parser)

This method will extract URLs only from valid CSS expressions. It most closely matches behavior of web browsers.

CssStyleSheet css = new CssLoader().ParseSheet(source, sheetUri, baseUri);
// Get rules of CssStyleRule type on all levels (including style rules inside media rules)
List<string> uris = css.AllStyleRules
    // Get property-value pairs, both non-important and important (marked with !important)
    .SelectMany(styleRule => styleRule.Declaration.AllData)
    // A property can be a list of values (background-image, for example, contains a list of URLs)
    .SelectMany(prop => prop.Value.Unit == CssUnit.List ? prop.Value.List : new[] { prop.Value })
    // Filter values of CssUrlValue type
    .Where(val => val.Unit == CssUnit.Url)
    // Get unresolved URLs (you can use Uri property to get resolved URLs)
    .Select(val => val.OriginalUri)
    .ToList();

Requirements

  • .NET 3.5

Installation

  1. Install using NuGet:

     PM> Install-Package Alba.CsCss
    
  2. Build from sources:

    1. Download sources from GitHub
    2. Add Alba.CsCss/Alba.CsCss.csproj project to your solution.

Architecture

Conversion

The major part of the library is ported by applying numerous regular expressions to original C++ code. C++ code and T4 files with regular expressions are included in the project (but are not included in the compiled DLL). The plan is to keep the code maintainable — when new version of Firefox is released, only minor modifications should be necessary, without trying to match converted code with new original code. Time will tell wether the idea is good.

Only parts which are expected to update regularly are converted with regular expressions. Parts of code which are considered relatively stable (style sheets, value types etc.) or are unpractical to convert with regex (unions, operator new overriding etc.), are rewritten manually.

Currently the biggest file (nsCSSParser.cpp/CssParser.conv.cs) containing around 10000 lines is converted using 400 lines of regular expressions (4%). Statistics for small files are worse. It helps a lot that Mozilla naming conventions for variables are very strict and pointer arithmetic is not heavily used. However, almost all public classes had to be renamed, as well as most enum values, to comply with C# coding standards.

Building from sources

Projects:

  • Alba.CsCss — the library itself. Has no dependencies except .NET. If library is included in another solution, only this project is necessary.
  • Alba.CsCss.Tests — "unit" tests (more like integration tests). Very few.
  • Alba.Framework — personal framework. Debug version of the assembly must be built to be able to run T4 files. Necessary only if you want to modify source code transformations.
  • Alba.Framework.CodeGeneration — T4 part of the personal framework. Must be built under Administrator account (installs AttachT4 custom tool similar to T4 Toolbox's T4ScriptFileGenerator). Necessary if you want T4 transformations to be applied when original source files are updated, or want ot add new source files.
  • Alba.Framework.Testing — testing part of the personal framework. Used in tests.

License

Mozilla Public License

TL;DR: As long as you include copyright notice, you can do whatever you want, but any source code file licensed under MPL must remain under MPL and freely available in source form.

CSS icon by David Vignoni.

Links

Alba.CsCss:
Parsing CSS:
  • ExCSS — another CSS parser for .NET. Stops parsing after some expressions. Analysis of expressions is much simpler (they are respresented as a list of terms). However, it supports modification, serialization to string, and is much more lightweight.
Parsing HTML:
  • CsQuery — jQuery port for .NET (HTML parser + fluent queries). Relies on HTML parser (see below) used in Mozilla Firefox, so very reliable.
  • HtmlParserSharp — validator.nu HTML parser port for .NET. Port of validator.nu code to C++ is used in Mozilla Firefox, so parsing is as good as it can get.
  • Html Agility Pack — older HTML parser. Fails to correctly parse even valid HTML4 code (optional closing tags), has no built-in fluent queries.
  • Fizzler — jQuery port for .NET based on Html Agility Pack. Less featureful than CsQuery.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].