Class CssDeclaration
A CSS declaration: a property name, its value as component values, and whether it is !important.
public sealed class CssDeclaration
- Inheritance
-
CssDeclaration
- Inherited Members
Examples
// A style attribute, and a declaration made from a presentation attribute such as SVG's fill="red".
IReadOnlyList<CssDeclaration> inline = CssDeclaration.ParseList("color: Red; --Accent: #0af; font-size: 12px !important");
CssDeclaration size = inline[2];
Console.WriteLine($"{size.Name} = {size.ValueText}, important: {size.Important}"); // font-size = 12px, important: True
CssDeclaration fill = CssDeclaration.Create("fill", "red");
Constructors
CssDeclaration(string, IReadOnlyList<CssComponentValue>, bool)
Creates a declaration.
public CssDeclaration(string name, IReadOnlyList<CssComponentValue> value, bool important = false)
Parameters
namestringThe property name. It is lowercased (ASCII) unless it is a custom property such as
--Main-Color, which keeps its case.valueIReadOnlyList<CssComponentValue>The value, without the
!importantflag. Whitespace at either end is removed.importantboolWhether the declaration is
!important.
Properties
Column
Gets the 1-based column the declaration starts at, or 0 when unknown.
public int Column { get; init; }
Property Value
Important
Gets whether the declaration is !important.
public bool Important { get; }
Property Value
IsCustomProperty
Gets whether this declares a custom property (a name starting with --).
public bool IsCustomProperty { get; }
Property Value
Line
Gets the 1-based line the declaration starts on, or 0 when unknown.
public int Line { get; init; }
Property Value
Name
Gets the property name: lowercase, except for custom properties.
public string Name { get; }
Property Value
Value
Gets the value as component values, without whitespace at either end and without !important.
public IReadOnlyList<CssComponentValue> Value { get; }
Property Value
ValueText
Gets the value serialized as CSS text.
public string ValueText { get; }
Property Value
Methods
Create(string, string, bool)
Creates a declaration from a property name and value text, such as an SVG presentation attribute.
public static CssDeclaration Create(string name, string value, bool important = false)
Parameters
namestringThe property name.
valuestringThe value as CSS text, without
!important.importantboolWhether the declaration is
!important.
Returns
- CssDeclaration
The declaration.
ParseList(string, CssParseOptions?)
Parses a list of declarations, such as the content of a style attribute (CSS Style Attributes). Invalid
declarations are skipped and reported; nested rules and at-rules are dropped and reported.
public static IReadOnlyList<CssDeclaration> ParseList(string styleAttribute, CssParseOptions? options = null)
Parameters
styleAttributestringThe declarations, such as
color: red; font-size: 12px.optionsCssParseOptionsOptions for error reporting.
Returns
- IReadOnlyList<CssDeclaration>
The declarations, in order.
Examples
// A style attribute, and a declaration made from a presentation attribute such as SVG's fill="red".
IReadOnlyList<CssDeclaration> inline = CssDeclaration.ParseList("color: Red; --Accent: #0af; font-size: 12px !important");
CssDeclaration size = inline[2];
Console.WriteLine($"{size.Name} = {size.ValueText}, important: {size.Important}"); // font-size = 12px, important: True
CssDeclaration fill = CssDeclaration.Create("fill", "red");
ToString()
Serializes the declaration as CSS text, such as color: red !important.
public override string ToString()
Returns
- string
The CSS text.