Table of Contents

Class CssSelector

Namespace
Egami.Css
Assembly
Egami.Css.dll

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

string

Specificity

Gets the selector's specificity (Selectors 4).

public CssSpecificity Specificity { get; }

Property Value

CssSpecificity

Text

Gets the selector as CSS text.

public string Text { get; }

Property Value

string

Methods

Matches(ICssElement, ICssElement?)

Tests whether the selector matches an element.

public bool Matches(ICssElement element, ICssElement? scope = null)

Parameters

element ICssElement

The element.

scope ICssElement

The scoping root for :scope, or null to make :scope match 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

element ICssElement

The originating element.

pseudoElement string

The pseudo-element in lowercase without colons, or null for the element itself.

Returns

bool

True when the selector's pseudo-element is pseudoElement and the rest matches the element.

ToString()

Returns the selector as CSS text.

public override string ToString()

Returns

string

The text.