Interface ICssElement
An element that selectors match against: implemented by a document tree such as Egami.Svg's or an HTML DOM.
public interface ICssElement
Examples
// A document tree implements ICssElement; Egami.Svg's elements do. The members must not allocate in the common case.
public sealed class Element(string localName, string? namespaceUri = "http://www.w3.org/2000/svg") : ICssElement
{
private readonly List<Element> _children = [];
private readonly List<CssAttribute> _attributes = [];
public string LocalName { get; } = localName;
public string? NamespaceUri { get; } = namespaceUri;
public Element? ParentElement { get; private set; }
public ICssElement? Parent => ParentElement;
public ICssElement? PreviousSibling => Sibling(-1);
public ICssElement? NextSibling => Sibling(1);
public ICssElement? FirstChild => _children.Count > 0 ? _children[0] : null;
public bool IsEmpty => _children.Count == 0;
public string? Id => GetAttribute("id", null);
public IEnumerable<CssAttribute> Attributes => _attributes;
public bool HasClass(string name) => GetAttribute("class", null)?.Split(' ').Contains(name) == true;
public string? GetAttribute(string localName, string? namespaceUri) =>
_attributes.Find(a => a.LocalName == localName && a.NamespaceUri == namespaceUri) is { LocalName: not null } a ? a.Value : null;
public Element Set(string name, string value)
{
_attributes.Add(new CssAttribute(null, name, value));
return this;
}
public Element Add(Element child)
{
child.ParentElement = this;
_children.Add(child);
return this;
}
private Element? Sibling(int step)
{
if (ParentElement is null)
{
return null;
}
int index = ParentElement._children.IndexOf(this) + step;
return index >= 0 && index < ParentElement._children.Count ? ParentElement._children[index] : null;
}
}
Remarks
Matching compares elements by reference (for :scope and :has()), so a tree must return the same object for the
same element every time. The members are called often while matching; they should not allocate.
Fields
XmlNamespace
The XML namespace, used for xml:lang.
public const string XmlNamespace = "http://www.w3.org/XML/1998/namespace"
Field Value
Properties
Attributes
Gets every attribute of the element, for attribute selectors with any namespace ([*|name]).
IEnumerable<CssAttribute> Attributes { get; }
Property Value
ClassName
Gets the element's class attribute: whitespace-separated class names that must agree with HasClass(string).
The cascade reads it to find the rules that may match. Defaults to the class attribute in no namespace.
string? ClassName { get; }
Property Value
FirstChild
Gets the first child element, or null.
ICssElement? FirstChild { get; }
Property Value
Id
Gets the element's ID, or null.
string? Id { get; }
Property Value
IsEmpty
Gets whether the element has no child elements and no text other than an empty string, for :empty.
bool IsEmpty { get; }
Property Value
IsHtml
Gets whether the element is an HTML element in an HTML document. Type selectors and attribute names then match ASCII
case-insensitively, and the values of the attributes HTML lists (such as type) too. Defaults to false.
bool IsHtml { get; }
Property Value
IsRightToLeft
Gets whether the element's directionality is right-to-left, for :dir(). Defaults to false.
bool IsRightToLeft { get; }
Property Value
Language
Gets the element's language for :lang(), or null when unknown. Defaults to the nearest
xml:lang or lang attribute on the element or an ancestor, xml:lang first.
string? Language { get; }
Property Value
LocalName
Gets the element's local name, such as rect.
string LocalName { get; }
Property Value
NamespaceUri
Gets the element's namespace URI, or null for none.
string? NamespaceUri { get; }
Property Value
NextSibling
Gets the next sibling element (skipping text and other nodes), or null.
ICssElement? NextSibling { get; }
Property Value
Parent
Gets the parent element, or null for the root element.
ICssElement? Parent { get; }
Property Value
PreviousSibling
Gets the previous sibling element (skipping text and other nodes), or null.
ICssElement? PreviousSibling { get; }
Property Value
Methods
GetAttribute(string, string?)
Gets an attribute's value.
string? GetAttribute(string localName, string? namespaceUri)
Parameters
localNamestringThe attribute's local name.
namespaceUristringThe attribute's namespace URI, or null for an attribute in no namespace.
Returns
HasClass(string)
Gets whether the element has the class name (compared case-sensitively).
bool HasClass(string name)
Parameters
namestringThe class name.
Returns
- bool
True when the element has the class.
MatchesPseudoClass(string)
Gets whether the element is in a state a pseudo-class names, such as hover, focus, checked,
link, visited, any-link, target or disabled. Defaults to false for every state.
bool MatchesPseudoClass(string name)
Parameters
namestringThe pseudo-class name in lowercase, without the colon.
Returns
- bool
True when the element is in that state.