Table of Contents

Interface ICssElement

Namespace
Egami.Css
Assembly
Egami.Css.dll

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

string

Properties

Attributes

Gets every attribute of the element, for attribute selectors with any namespace ([*|name]).

IEnumerable<CssAttribute> Attributes { get; }

Property Value

IEnumerable<CssAttribute>

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

string

FirstChild

Gets the first child element, or null.

ICssElement? FirstChild { get; }

Property Value

ICssElement

Id

Gets the element's ID, or null.

string? Id { get; }

Property Value

string

IsEmpty

Gets whether the element has no child elements and no text other than an empty string, for :empty.

bool IsEmpty { get; }

Property Value

bool

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

bool

IsRightToLeft

Gets whether the element's directionality is right-to-left, for :dir(). Defaults to false.

bool IsRightToLeft { get; }

Property Value

bool

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

string

LocalName

Gets the element's local name, such as rect.

string LocalName { get; }

Property Value

string

NamespaceUri

Gets the element's namespace URI, or null for none.

string? NamespaceUri { get; }

Property Value

string

NextSibling

Gets the next sibling element (skipping text and other nodes), or null.

ICssElement? NextSibling { get; }

Property Value

ICssElement

Parent

Gets the parent element, or null for the root element.

ICssElement? Parent { get; }

Property Value

ICssElement

PreviousSibling

Gets the previous sibling element (skipping text and other nodes), or null.

ICssElement? PreviousSibling { get; }

Property Value

ICssElement

Methods

GetAttribute(string, string?)

Gets an attribute's value.

string? GetAttribute(string localName, string? namespaceUri)

Parameters

localName string

The attribute's local name.

namespaceUri string

The attribute's namespace URI, or null for an attribute in no namespace.

Returns

string

The value, or null when the element has no such attribute.

HasClass(string)

Gets whether the element has the class name (compared case-sensitively).

bool HasClass(string name)

Parameters

name string

The 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

name string

The pseudo-class name in lowercase, without the colon.

Returns

bool

True when the element is in that state.