Table of Contents

Class CssDeclaration

Namespace
Egami.Css
Assembly
Egami.Css.dll

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

name string

The property name. It is lowercased (ASCII) unless it is a custom property such as --Main-Color, which keeps its case.

value IReadOnlyList<CssComponentValue>

The value, without the !important flag. Whitespace at either end is removed.

important bool

Whether 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

int

Important

Gets whether the declaration is !important.

public bool Important { get; }

Property Value

bool

IsCustomProperty

Gets whether this declares a custom property (a name starting with --).

public bool IsCustomProperty { get; }

Property Value

bool

Line

Gets the 1-based line the declaration starts on, or 0 when unknown.

public int Line { get; init; }

Property Value

int

Name

Gets the property name: lowercase, except for custom properties.

public string Name { get; }

Property Value

string

Value

Gets the value as component values, without whitespace at either end and without !important.

public IReadOnlyList<CssComponentValue> Value { get; }

Property Value

IReadOnlyList<CssComponentValue>

ValueText

Gets the value serialized as CSS text.

public string ValueText { get; }

Property Value

string

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

name string

The property name.

value string

The value as CSS text, without !important.

important bool

Whether 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

styleAttribute string

The declarations, such as color: red; font-size: 12px.

options CssParseOptions

Options 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.