Class CssParseOptions
Options for parsing CSS.
public sealed record CssParseOptions : IEquatable<CssParseOptions>
- Inheritance
-
CssParseOptions
- Implements
- 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}");
Properties
Default
Gets the default options: errors are not reported.
public static CssParseOptions Default { get; }
Property Value
Errors
Gets a callback that receives every parse error, or null to ignore them.
public Action<CssParseError>? Errors { get; init; }
Property Value
MaxNestingDepth
Gets the deepest nesting of blocks and functions kept. A block nested deeper is dropped with an error, which keeps hostile input from exhausting the stack. Defaults to 128.
public int MaxNestingDepth { get; init; }