Table of Contents

Struct CssAttribute

Namespace
Egami.Css
Assembly
Egami.Css.dll

An attribute of an element, as Attributes lists it.

[SuppressMessage("Naming", "CA1711", Justification = "A DOM attribute, not a .NET attribute.")]
public readonly record struct CssAttribute : IEquatable<CssAttribute>
Implements
Inherited Members

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;
    }
}

Constructors

CssAttribute(string?, string, string)

An attribute of an element, as Attributes lists it.

public CssAttribute(string? NamespaceUri, string LocalName, string Value)

Parameters

NamespaceUri string

The attribute's namespace URI, or null for none.

LocalName string

The attribute's local name.

Value string

The attribute's value.

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;
    }
}

Properties

LocalName

The attribute's local name.

public string LocalName { get; init; }

Property Value

string

NamespaceUri

The attribute's namespace URI, or null for none.

public string? NamespaceUri { get; init; }

Property Value

string

Value

The attribute's value.

public string Value { get; init; }

Property Value

string

Methods

ToString()

Formats the attribute as a record does, such as CssAttribute { NamespaceUri = , LocalName = id, Value = a }.

public override string ToString()

Returns

string

The text.