Table of Contents

Class CssStyleSheet

Namespace
Egami.Css
Assembly
Egami.Css.dll

A parsed CSS style sheet (CSS Syntax 3 "parse a stylesheet", with the rules CSS defines interpreted).

public sealed class CssStyleSheet
Inheritance
CssStyleSheet
Inherited Members

Examples

// Parsing never throws on bad CSS: it recovers as CSS Syntax 3 says and reports each problem.
var errors = new List<CssParseError>();
var options = new CssParseOptions { Errors = errors.Add };
CssStyleSheet sheet = CssStyleSheet.Parse("""
    @import url(print.css) print;
    @namespace svg url(http://www.w3.org/2000/svg);
    rect.note, svg|circle { fill: gold !important; stroke: #333 }
    @media (max-width: 600px) { rect { stroke-width: 2px } }
    @supports (display: grid) { g { display: grid } }
    @font-face { font-family: "Inter"; src: url(inter.woff2) format("woff2") }
    @layer base { text { fill: gray } }
    @keyframes spin { to { rotate: 1turn } }
    p:unknown { color: red }
    """, options);

foreach (CssRule rule in sheet.Rules)
{
    switch (rule)
    {
        case CssStyleRule style:
            Console.WriteLine($"{style.SelectorText}: {style.Declarations.Count} declarations");
            break;
        case CssMediaRule media:
            Console.WriteLine($"@media {media.Media.MediaText}: {media.Rules.Count} rules");
            break;
        case CssSupportsRule supports:
            Console.WriteLine($"@supports {supports.ConditionText}");
            break;
        case CssFontFaceRule fontFace:
            Console.WriteLine($"@font-face {fontFace.FontFace.Family}");
            break;
        case CssImportRule import:
            Console.WriteLine($"@import {import.Url} for {import.Media.MediaText}");
            break;
        case CssNamespaceRule ns:
            Console.WriteLine($"@namespace {ns.Prefix} = {ns.Uri}");
            break;
        case CssLayerRule layer:
            Console.WriteLine($"@layer {string.Join(", ", layer.Names)}");
            break;
        case CssUnknownAtRule other:
            Console.WriteLine($"@{other.Name} (kept, not interpreted)");
            break;
    }
}

// The rule with ':unknown' was dropped and reported.
CssParseError dropped = errors[0];
Console.WriteLine($"{dropped.Line}:{dropped.Column} {dropped.Message}");

Remarks

Parsing never throws on bad CSS: it recovers as CSS Syntax 3 says and reports each problem to Errors. Style rules whose selector does not parse are dropped; @import and @namespace rules after other rules are dropped; rules nested in style rules (CSS Nesting) are dropped.

Properties

Namespaces

Gets the namespaces the sheet's @namespace rules declare.

public CssNamespaces Namespaces { get; }

Property Value

CssNamespaces

Rules

Gets the top-level rules, in order.

public IReadOnlyList<CssRule> Rules { get; }

Property Value

IReadOnlyList<CssRule>

Methods

GetAllStyleRules()

Enumerates every style rule, including those inside @media, @supports and @layer blocks, whether or not their conditions hold.

public IEnumerable<CssStyleRule> GetAllStyleRules()

Returns

IEnumerable<CssStyleRule>

The style rules in order.

Parse(string, CssParseOptions?)

Parses a style sheet.

public static CssStyleSheet Parse(string css, CssParseOptions? options = null)

Parameters

css string

The CSS text.

options CssParseOptions

Options for error reporting.

Returns

CssStyleSheet

The style sheet.

Examples

// Parsing never throws on bad CSS: it recovers as CSS Syntax 3 says and reports each problem.
var errors = new List<CssParseError>();
var options = new CssParseOptions { Errors = errors.Add };
CssStyleSheet sheet = CssStyleSheet.Parse("""
    @import url(print.css) print;
    @namespace svg url(http://www.w3.org/2000/svg);
    rect.note, svg|circle { fill: gold !important; stroke: #333 }
    @media (max-width: 600px) { rect { stroke-width: 2px } }
    @supports (display: grid) { g { display: grid } }
    @font-face { font-family: "Inter"; src: url(inter.woff2) format("woff2") }
    @layer base { text { fill: gray } }
    @keyframes spin { to { rotate: 1turn } }
    p:unknown { color: red }
    """, options);

foreach (CssRule rule in sheet.Rules)
{
    switch (rule)
    {
        case CssStyleRule style:
            Console.WriteLine($"{style.SelectorText}: {style.Declarations.Count} declarations");
            break;
        case CssMediaRule media:
            Console.WriteLine($"@media {media.Media.MediaText}: {media.Rules.Count} rules");
            break;
        case CssSupportsRule supports:
            Console.WriteLine($"@supports {supports.ConditionText}");
            break;
        case CssFontFaceRule fontFace:
            Console.WriteLine($"@font-face {fontFace.FontFace.Family}");
            break;
        case CssImportRule import:
            Console.WriteLine($"@import {import.Url} for {import.Media.MediaText}");
            break;
        case CssNamespaceRule ns:
            Console.WriteLine($"@namespace {ns.Prefix} = {ns.Uri}");
            break;
        case CssLayerRule layer:
            Console.WriteLine($"@layer {string.Join(", ", layer.Names)}");
            break;
        case CssUnknownAtRule other:
            Console.WriteLine($"@{other.Name} (kept, not interpreted)");
            break;
    }
}

// The rule with ':unknown' was dropped and reported.
CssParseError dropped = errors[0];
Console.WriteLine($"{dropped.Line}:{dropped.Column} {dropped.Message}");