Enum CssOrigin
The origin of a style sheet in the cascade.
public enum CssOrigin
Fields
Author = 2The document's style sheets, presentation hints and style attributes.
User = 1The user's style sheet.
UserAgent = 0The user agent's default style sheet.
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.
}