Class CssSelector
One complex selector of a selector list, such as svg|g > rect.highlight:first-child.
public sealed class CssSelector
- Inheritance
-
CssSelector
- 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
Properties
PseudoElement
Gets the pseudo-element the selector ends in, in lowercase without colons (such as before), or
null. A selector with a pseudo-element never matches an element itself.
public string? PseudoElement { get; }
Property Value
Specificity
Gets the selector's specificity (Selectors 4).
public CssSpecificity Specificity { get; }
Property Value
Text
Gets the selector as CSS text.
public string Text { get; }
Property Value
Methods
Matches(ICssElement, ICssElement?)
Tests whether the selector matches an element.
public bool Matches(ICssElement element, ICssElement? scope = null)
Parameters
elementICssElementThe element.
scopeICssElementThe scoping root for
:scope, or null to make:scopematch the root element.
Returns
- bool
True when it matches; always false for a selector with a pseudo-element.
MatchesPseudoElement(ICssElement, string?)
Tests whether the selector matches a pseudo-element of an element, such as its ::before.
public bool MatchesPseudoElement(ICssElement element, string? pseudoElement)
Parameters
elementICssElementThe originating element.
pseudoElementstringThe pseudo-element in lowercase without colons, or null for the element itself.
Returns
- bool
True when the selector's pseudo-element is
pseudoElementand the rest matches the element.
ToString()
Returns the selector as CSS text.
public override string ToString()
Returns
- string
The text.