Class CssPropertyRegistry
The properties the cascade knows. CreateDefault() gives the common text properties of CSS; consumers add their
own, such as Egami.Svg's fill and stroke. Unregistered properties cascade too, as non-inherited properties
without an initial value. Do not change a registry while a cascade built from it is in use.
public sealed class CssPropertyRegistry
- Inheritance
-
CssPropertyRegistry
- 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
Properties
Gets the registered longhand properties.
public IEnumerable<CssPropertyDefinition> Properties { get; }
Property Value
Shorthands
Gets the registered shorthands.
public IEnumerable<CssShorthandDefinition> Shorthands { get; }
Property Value
Methods
CreateDefault()
Creates a registry with the CSS properties SVG and HTML documents share, each with its value grammar, so an invalid
declaration is dropped. Text: color, the font- longhands and the font and font-variant
shorthands, line-height, letter-spacing, word-spacing, text-decoration and its longhands,
text-align, text-indent, text-transform, white-space, direction, unicode-bidi,
writing-mode, text-orientation, visibility, display and opacity. Boxes: the
margin-, padding- and border- longhands with the margin, padding, border,
border-width, border-style, border-color, border-top (and the other sides) and
border-radius shorthands; width, height, their min- and max- forms and
box-sizing. Backgrounds: the background- longhands, one comma-separated item per layer, and the
background shorthand. Lists: list-style-type, list-style-position, list-style-image and
list-style. Tables: border-collapse, border-spacing, caption-side, empty-cells,
table-layout and vertical-align. Flow: float, clear, overflow-x, overflow-y
and overflow. Fragmentation: break-before, break-after, break-inside, orphans,
widows, box-decoration-break, page and the legacy page-break- shorthands. Pages (for
CascadePage(CssPage)): size, page-orientation, marks and bleed. Generated
content: content, counter-reset, counter-increment and counter-set. The all
shorthand is always understood.
public static CssPropertyRegistry CreateDefault()
Returns
- CssPropertyRegistry
A new registry the caller may extend.
Register(CssPropertyDefinition)
Registers a longhand property, replacing one of the same name.
public void Register(CssPropertyDefinition definition)
Parameters
definitionCssPropertyDefinitionThe property.
RegisterShorthand(CssShorthandDefinition)
Registers a shorthand, replacing one of the same name.
public void RegisterShorthand(CssShorthandDefinition definition)
Parameters
definitionCssShorthandDefinitionThe shorthand.
TryGetProperty(string, out CssPropertyDefinition?)
Looks up a longhand property.
public bool TryGetProperty(string name, out CssPropertyDefinition? definition)
Parameters
namestringThe name in lowercase.
definitionCssPropertyDefinitionThe property when registered.
Returns
- bool
True when registered.
TryGetShorthand(string, out CssShorthandDefinition?)
Looks up a shorthand.
public bool TryGetShorthand(string name, out CssShorthandDefinition? definition)
Parameters
namestringThe name in lowercase.
definitionCssShorthandDefinitionThe shorthand when registered.
Returns
- bool
True when registered.