Struct CssSpecificity
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
AintThe number of ID selectors.
BintThe number of class, attribute and pseudo-class selectors.
CintThe 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
B
The number of class, attribute and pseudo-class selectors.
public int B { get; init; }
Property Value
C
The number of type and pseudo-element selectors.
public int C { get; init; }
Property Value
Zero
Gets the specificity of no selector: (0, 0, 0).
public static CssSpecificity Zero { get; }
Property Value
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
otherCssSpecificityAn 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 otherin the sort order.Zero This instance occurs in the same position in the sort order as other.Greater than zero This instance follows otherin 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
leftCssSpecificityThe first specificity.
rightCssSpecificityThe second specificity.
Returns
- CssSpecificity
The sum.
operator >(CssSpecificity, CssSpecificity)
Compares two specificities.
public static bool operator >(CssSpecificity left, CssSpecificity right)
Parameters
leftCssSpecificityThe first specificity.
rightCssSpecificityThe second specificity.
Returns
- bool
True when
leftis more specific.
operator >=(CssSpecificity, CssSpecificity)
Compares two specificities.
public static bool operator >=(CssSpecificity left, CssSpecificity right)
Parameters
leftCssSpecificityThe first specificity.
rightCssSpecificityThe second specificity.
Returns
- bool
True when
leftis at least as specific.
operator <(CssSpecificity, CssSpecificity)
Compares two specificities.
public static bool operator <(CssSpecificity left, CssSpecificity right)
Parameters
leftCssSpecificityThe first specificity.
rightCssSpecificityThe second specificity.
Returns
- bool
True when
leftis less specific.
operator <=(CssSpecificity, CssSpecificity)
Compares two specificities.
public static bool operator <=(CssSpecificity left, CssSpecificity right)
Parameters
leftCssSpecificityThe first specificity.
rightCssSpecificityThe second specificity.
Returns
- bool
True when
leftis at most as specific.