Table of Contents

Class CssCascadedStyle

Namespace
Egami.Css
Assembly
Egami.Css.dll

The cascaded style of an element: the winning declaration of each property, with its origin.

public sealed class CssCascadedStyle
Inheritance
CssCascadedStyle
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.
}

Constructors

CssCascadedStyle()

Creates an empty cascaded style (no declarations apply).

public CssCascadedStyle()

Properties

Count

Gets the number of properties with a winning declaration.

public int Count { get; }

Property Value

int

Properties

Gets the names of the properties with a winning declaration (longhands and custom properties).

public IEnumerable<string> Properties { get; }

Property Value

IEnumerable<string>

Methods

GetValueText(string)

Gets the value text of a property's winning declaration, or null.

public string? GetValueText(string name)

Parameters

name string

The property name.

Returns

string

The value text.

TryGetDeclaration(string, out CssDeclaration?)

Gets the winning declaration of a property.

public bool TryGetDeclaration(string name, out CssDeclaration? declaration)

Parameters

name string

The property name (lowercase, or a custom property name as written).

declaration CssDeclaration

The winning declaration.

Returns

bool

True when a declaration applies.

TryGetDeclaration(string, out CssDeclaration?, out CssOrigin)

Gets the winning declaration of a property and its origin.

public bool TryGetDeclaration(string name, out CssDeclaration? declaration, out CssOrigin origin)

Parameters

name string

The property name.

declaration CssDeclaration

The winning declaration.

origin CssOrigin

Its origin.

Returns

bool

True when a declaration applies.