Table of Contents

Class CssCascadeOptions

Namespace
Egami.Css
Assembly
Egami.Css.dll

Options for building a CssCascade.

public sealed record CssCascadeOptions : IEquatable<CssCascadeOptions>
Inheritance
CssCascadeOptions
Implements
Inherited Members

Examples

// The registry says which properties inherit and their initial values; consumers add their own.
CssPropertyRegistry registry = CssPropertyRegistry.CreateDefault();
registry.Register(new CssPropertyDefinition("fill", Inherited: true, InitialValue: "black")
{
    Validate = value => CssColor.TryParse(value, out _),
});
registry.RegisterShorthand(new CssShorthandDefinition("marker", ["marker-start", "marker-end"],
    value => [new CssDeclaration("marker-start", value), new CssDeclaration("marker-end", value)]));

CssStyleSheet userAgent = CssStyleSheet.Parse("rect { fill: gray }");
CssStyleSheet author = CssStyleSheet.Parse("""
    svg { --accent: teal; font: 12px sans-serif }
    .layer { fill: var(--accent) }
    #r { fill: inherit; opacity: 0.5 }
    """);
var cascade = new CssCascade(
    [(userAgent, CssOrigin.UserAgent), (author, CssOrigin.Author)],
    new CssCascadeOptions { Registry = registry, Media = new CssMediaContext { Width = 800 } });

// Cascade each element (presentation attributes as hints, the style attribute as inline style), then compute it
// from its parent's computed style.
CssComputedStyle rootStyle = cascade.Compute(cascade.Cascade(root), null);
CssComputedStyle groupStyle = cascade.Compute(cascade.Cascade(group), rootStyle);
CssCascadedStyle cascaded = cascade.Cascade(rect,
    presentationHints: [CssDeclaration.Create("fill", "red")],
    inlineStyle: CssDeclaration.ParseList("stroke: blue"));
CssComputedStyle style = cascade.Compute(cascaded, groupStyle);

cascaded.TryGetDeclaration("fill", out CssDeclaration? winner, out CssOrigin origin); // fill: inherit, Author
string? fill = style.GetValueText("fill");           // "teal": inherited, var() substituted
string? fontSize = style.GetValueText("font-size");  // "12px": from the font shorthand, inherited
if (style.TryGetValue("opacity", out IReadOnlyList<CssComponentValue>? opacity))
{
    // The consumer parses each property by its type.
}

Properties

Default

Gets the default options: Default and CreateDefault().

public static CssCascadeOptions Default { get; }

Property Value

CssCascadeOptions

Media

Gets the media context @media rules and @import-free media lists are evaluated against.

public CssMediaContext Media { get; init; }

Property Value

CssMediaContext

Registry

Gets the property registry, or null for CreateDefault().

public CssPropertyRegistry? Registry { get; init; }

Property Value

CssPropertyRegistry

Supports

Gets a callback answering whether a declaration in an @supports condition is supported, or null to accept a declaration whose property is registered (or custom) and whose value passes the property's validation.

public Func<CssDeclaration, bool>? Supports { get; init; }

Property Value

Func<CssDeclaration, bool>