Table of Contents

Struct CssSpecificity

Namespace
Egami.Css
Assembly
Egami.Css.dll

A selector's specificity (Selectors 4): IDs, then classes, attributes and pseudo-classes, then types and pseudo-elements.

public readonly record struct CssSpecificity : IComparable<CssSpecificity>, IEquatable<CssSpecificity>
Implements
Inherited Members

Examples

// Selectors 4, matched right to left over ICssElement.
CssSelectorList list = CssSelectorList.Parse("g.layer > rect:first-child, :is(circle, ellipse)");
bool matches = list.Matches(rect);

CssSelector selector = list.Selectors[0];
CssSpecificity specificity = selector.Specificity; // (0, 2, 2)

// Namespace prefixes, as @namespace declares them.
var namespaces = new CssNamespaces();
namespaces.Set("svg", "http://www.w3.org/2000/svg");
bool inSvg = CssSelectorList.Parse("svg|rect", namespaces).Matches(rect);

// A pseudo-element never matches the element itself; the cascade asks for it by name.
CssSelector before = CssSelectorList.Parse("rect::before").Selectors[0];
bool beforeMatches = before.MatchesPseudoElement(rect, "before");

bool valid = CssSelectorList.TryParse("rect:nonsense", out _); // false: unknown pseudo-classes are invalid

Constructors

CssSpecificity(int, int, int)

A selector's specificity (Selectors 4): IDs, then classes, attributes and pseudo-classes, then types and pseudo-elements.

public CssSpecificity(int A, int B, int C)

Parameters

A int

The number of ID selectors.

B int

The number of class, attribute and pseudo-class selectors.

C int

The number of type and pseudo-element selectors.

Examples

// Selectors 4, matched right to left over ICssElement.
CssSelectorList list = CssSelectorList.Parse("g.layer > rect:first-child, :is(circle, ellipse)");
bool matches = list.Matches(rect);

CssSelector selector = list.Selectors[0];
CssSpecificity specificity = selector.Specificity; // (0, 2, 2)

// Namespace prefixes, as @namespace declares them.
var namespaces = new CssNamespaces();
namespaces.Set("svg", "http://www.w3.org/2000/svg");
bool inSvg = CssSelectorList.Parse("svg|rect", namespaces).Matches(rect);

// A pseudo-element never matches the element itself; the cascade asks for it by name.
CssSelector before = CssSelectorList.Parse("rect::before").Selectors[0];
bool beforeMatches = before.MatchesPseudoElement(rect, "before");

bool valid = CssSelectorList.TryParse("rect:nonsense", out _); // false: unknown pseudo-classes are invalid

Properties

A

The number of ID selectors.

public int A { get; init; }

Property Value

int

B

The number of class, attribute and pseudo-class selectors.

public int B { get; init; }

Property Value

int

C

The number of type and pseudo-element selectors.

public int C { get; init; }

Property Value

int

Zero

Gets the specificity of no selector: (0, 0, 0).

public static CssSpecificity Zero { get; }

Property Value

CssSpecificity

Methods

CompareTo(CssSpecificity)

Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

public int CompareTo(CssSpecificity other)

Parameters

other CssSpecificity

An object to compare with this instance.

Returns

int

A value that indicates the relative order of the objects being compared. The return value has these meanings:

Value Meaning
Less than zero This instance precedes other in the sort order.
Zero This instance occurs in the same position in the sort order as other.
Greater than zero This instance follows other in the sort order.

ToString()

Formats the specificity as (a, b, c).

public override string ToString()

Returns

string

The text.

Operators

operator +(CssSpecificity, CssSpecificity)

Adds two specificities component by component.

public static CssSpecificity operator +(CssSpecificity left, CssSpecificity right)

Parameters

left CssSpecificity

The first specificity.

right CssSpecificity

The second specificity.

Returns

CssSpecificity

The sum.

operator >(CssSpecificity, CssSpecificity)

Compares two specificities.

public static bool operator >(CssSpecificity left, CssSpecificity right)

Parameters

left CssSpecificity

The first specificity.

right CssSpecificity

The second specificity.

Returns

bool

True when left is more specific.

operator >=(CssSpecificity, CssSpecificity)

Compares two specificities.

public static bool operator >=(CssSpecificity left, CssSpecificity right)

Parameters

left CssSpecificity

The first specificity.

right CssSpecificity

The second specificity.

Returns

bool

True when left is at least as specific.

operator <(CssSpecificity, CssSpecificity)

Compares two specificities.

public static bool operator <(CssSpecificity left, CssSpecificity right)

Parameters

left CssSpecificity

The first specificity.

right CssSpecificity

The second specificity.

Returns

bool

True when left is less specific.

operator <=(CssSpecificity, CssSpecificity)

Compares two specificities.

public static bool operator <=(CssSpecificity left, CssSpecificity right)

Parameters

left CssSpecificity

The first specificity.

right CssSpecificity

The second specificity.

Returns

bool

True when left is at most as specific.