https://github.com/visualon/cssparser
CSS Parser for .NET
https://github.com/visualon/cssparser
csharp css dotnet
Last synced: 6 months ago
JSON representation
CSS Parser for .NET
- Host: GitHub
- URL: https://github.com/visualon/cssparser
- Owner: visualon
- License: mit
- Created: 2018-12-05T08:38:38.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2025-04-14T23:15:09.000Z (9 months ago)
- Last Synced: 2025-05-08T03:07:35.051Z (8 months ago)
- Topics: csharp, css, dotnet
- Language: C#
- Homepage:
- Size: 672 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# CSS Parser
[](https://github.com/visualon/cssparser/actions/workflows/build.yml)
[](https://www.nuget.org/packages/VisualOn.CssParser)
[](https://www.nuget.org/packages/VisualOn.CssParser.ExtendedLESSParser)
[](https://codecov.io/gh/visualon/cssparser)

This is a fork from [Dan Roberts](https://bitbucket.org/DanRoberts/cssparser).
A simple CSS and LESS parser to categorise strings of content and optionally generate a hierarchical representation of it.
## Changes
See [Releases](https://github.com/visualon/cssparser/releases)
## License
[MIT License](LICENSE.txt)
### Howto
This was written to fulfill a need I had to run quickly through a string of CSS and identify what "type" each character was. With an optional ability to parse it into hierarchical data describing nested selectors and/or media queries (most applicable to LESS rather than vanilla CSS since LESS supports nesting of selectors whereas CSS only supports single nesting of a selector within a media query).
IEnumerable ParseCSS(string content)
and
IEnumerable ParseLESS(string content)
(within the static class **CSSParser.Parser**) do the basic parsing work where **CategorisedCharacterString** has the properties
public class CategorisedCharacterString
{
///
/// This will never be null or an empty string
///
public string Value { get; }
///
/// This is the location of the start of the string in the source data
///
public int IndexInSource { get; }
public CharacterCategorisationOptions CharacterCategorisation { get; }
}
public enum CharacterCategorisationOptions
{
Comment,
CloseBrace,
OpenBrace,
SemiColon,
SelectorOrStyleProperty,
StylePropertyColon,
Value,
Whitespace
}
so calling ParseCSS on
/* Test */ .Content { color: black; }
will return **CategorisedCharacterString** instances with the data
"/* Test */" Comment (IndexInSource 0)
" " Whitespace (IndexInSource 10)
".Content" SelectorOrStyleProperty (IndexInSource 11)
" " Whitespace (IndexInSource 19)
"{" OpenBrace (IndexInSource 20)
" " Whitespace (IndexInSource 19)
"color" SelectorOrStyleProperty (IndexInSource 22)
":" StylePropertyColon (IndexInSource 27)
" " Whitespace (IndexInSource 28)
"black" Value (IndexInSource 29)
";" SemiColon (IndexInSource 34)
" " Whitespace (IndexInSource 35)
"}" CloseBrace (IndexInSource 36)
This analysis can be done very cheaply as it is only a very simple representation. It does not, for example, differentiate between the type of the ".Content" value or "color", they are both considered to be of type **SelectorOrStyleProperty**.
To arrange in a hierchical manner and to categorise more strictly, the data return from ParseCSS or ParseLess can be passed into
IEnumerable ParseIntoStructuredData(
IEnumerable segments
)
(within the static class **CSSParser.ExtendedLESSParser.LessCssHierarchicalParser**) which transforms the data again. The interface **ICSSFragment** is implemented by the classes **Import** (describing an import statement for another stylesheet), a **MediaQuery** or a **Selector** (both of which have a "ChildFragments" set as they may contain other media queries, selectors and/or properties), a **StylePropertyName** or **StylePropertyValue**. Note that comments and whitespace are not included in this data.
So content such as
// Example
html
{
h1
{
color: black;
background: white url("background.jpg") no-repeat top left;
}
p.Intro { padding: 8px; }
}
becomes something like
html
h1
color
black
background
white
url("background.jpg")
no-repat
top
left
p.Intro
padding
8px
_(Above: "html" represent a Selector instance with a ChildFragments property containing Selector instances for the "h1" and "p", each with ChildFragments data made up of StylePropertyValue and StylePropertyValue instances)._