Table of Contents

Class Element

Namespace
AngleSharp.Dom
Assembly
AngleSharp.dll

Represents an element node.

public abstract class Element : Node, IEquatable<INode>, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode
Inheritance
Element
Implements
Derived
Inherited Members
Extension Methods

Constructors

Element(Document, string, string?, string?, NodeFlags)

public Element(Document owner, string localName, string? prefix, string? namespaceUri, NodeFlags flags = NodeFlags.None)

Parameters

owner Document
localName string
prefix string
namespaceUri string
flags NodeFlags

Element(Document, string, string, string?, string, NodeFlags)

public Element(Document owner, string name, string localName, string? prefix, string namespaceUri, NodeFlags flags = NodeFlags.None)

Parameters

owner Document
name string
localName string
prefix string
namespaceUri string
flags NodeFlags

Properties

AssignedSlot

Gets the assigned slot of the current element, if any.

public IElement? AssignedSlot { get; }

Property Value

IElement

ChildElementCount

Gets the number of child elements.

public int ChildElementCount { get; }

Property Value

int

Children

Gets the child elements.

public IHtmlCollection<IElement> Children { get; }

Property Value

IHtmlCollection<IElement>

ClassList

Gets the list of class names.

public ITokenList ClassList { get; }

Property Value

ITokenList

ClassName

Gets or sets the value of the class attribute.

public string? ClassName { get; set; }

Property Value

string

FirstElementChild

Gets the first child element of this element.

public IElement? FirstElementChild { get; }

Property Value

IElement

GivenNamespaceUri

Gets the given namespace URI of this element. This one will not be resolved via its parent, but only yields the provided namespace, if any.

public string? GivenNamespaceUri { get; }

Property Value

string

Id

Gets or sets the id value of the element.

public string? Id { get; set; }

Property Value

string

InnerHtml

Gets or sets the inner HTML (excluding the current element) of the element.

public string InnerHtml { get; set; }

Property Value

string

IsFocused

Gets if the element is currently focused.

public bool IsFocused { get; protected set; }

Property Value

bool

LastElementChild

Gets the last child element of this element.

public IElement? LastElementChild { get; }

Property Value

IElement

LocalName

Gets the local part of the qualified name of this element.

public string LocalName { get; }

Property Value

string

NamespaceUri

Gets the namespace URI of this element.

public string? NamespaceUri { get; }

Property Value

string

NextElementSibling

Gets the Element immediately following this ChildNode in its parent's children list, or null if there is no Element in the list following this ChildNode.

public IElement? NextElementSibling { get; }

Property Value

IElement

OuterHtml

Gets or sets the outer HTML (including the current element) of the element.

public string OuterHtml { get; set; }

Property Value

string

Prefix

Gets the namespace prefix of this element.

public string? Prefix { get; }

Property Value

string

PreviousElementSibling

Gets the Element immediately prior to this ChildNode in its parent's children list, or null if there is no Element in the list prior to this ChildNode.

public IElement? PreviousElementSibling { get; }

Property Value

IElement

ShadowRoot

Gets the shadow root of the current element, if any.

public IShadowRoot? ShadowRoot { get; }

Property Value

IShadowRoot

Slot

Gets the value of the slot attribute.

public string? Slot { get; set; }

Property Value

string

SourceReference

Gets the source reference if available.

public ISourceReference? SourceReference { get; set; }

Property Value

ISourceReference

TagName

Gets the name of the tag that represents the current element.

public string TagName { get; }

Property Value

string

TextContent

Gets or sets the textual content of an element and all its descendants.

public override string TextContent { get; set; }

Property Value

string

Methods

AddAttribute(Attr)

Adds an attribute.

public void AddAttribute(Attr attr)

Parameters

attr Attr

The attribute to add.

After(params INode[])

Inserts nodes just after the current node.

public void After(params INode[] nodes)

Parameters

nodes INode[]

The nodes to insert.

Append(params INode[])

Appends nodes to current document.

public void Append(params INode[] nodes)

Parameters

nodes INode[]

The nodes to append.

AttachShadow(ShadowRootMode)

Creates a new shadow root for the current element, if there is none already.

public IShadowRoot AttachShadow(ShadowRootMode mode = ShadowRootMode.Open)

Parameters

mode ShadowRootMode

The mode of the shadow root.

Returns

IShadowRoot

The new shadow root.

Before(params INode[])

Inserts nodes just before the current node.

public void Before(params INode[] nodes)

Parameters

nodes INode[]

The nodes to insert.

Clone(Document, bool)

Clones the current node using the new owner.

public override Node Clone(Document owner, bool deep)

Parameters

owner Document
deep bool

True if a deep clone is wanted, otherwise false.

Returns

Node

The cloned node.

CloneElement(Element, Document, bool)

protected void CloneElement(Element element, Document owner, bool deep)

Parameters

element Element
owner Document
deep bool

Closest(string)

Returns the closest ancestor of the current element (or the current element itself) which matches the selectors given in the parameter.

public IElement? Closest(string selectorText)

Parameters

selectorText string

Returns

IElement

The closest ancestor of the current element (or the current element itself) which matches the selectors given. If there isn't such an ancestor, it returns null.

Equals(INode?)

public override bool Equals(INode? otherNode)

Parameters

otherNode INode

Returns

bool

GetAttribute(string)

Returns the value of the named attribute on the specified element.

public string? GetAttribute(string name)

Parameters

name string

The name of the attribute whose value you want to get.

Returns

string

If the named attribute does not exist, the value returned will be null, otherwise the attribute's value.

GetAttribute(string?, string)

Returns the value of the named attribute on the specified element.

public string? GetAttribute(string? namespaceUri, string localName)

Parameters

namespaceUri string

A string specifying the namespace of the attribute.

localName string

The name of the attribute whose value you want to get.

Returns

string

If the named attribute does not exist, the value returned will be null, otherwise the attribute's value.

GetElementsByClassName(string)

Returns a set of elements which have all the given class names.

public IHtmlCollection<IElement> GetElementsByClassName(string classNames)

Parameters

classNames string

A string representing the list of class names to match; class names are separated by whitespace.

Returns

IHtmlCollection<IElement>

A collection of elements.

GetElementsByTagName(string)

Returns a NodeList of elements with the given tag name. The complete document is searched, including the root node.

public IHtmlCollection<IElement> GetElementsByTagName(string tagName)

Parameters

tagName string

A string representing the name of the elements. The special string "*" represents all elements.

Returns

IHtmlCollection<IElement>

A collection of elements in the order they appear in the tree.

GetElementsByTagNameNS(string?, string)

Returns a list of elements with the given tag name belonging to the given namespace. The complete document is searched, including the root node.

public IHtmlCollection<IElement> GetElementsByTagNameNS(string? namespaceURI, string tagName)

Parameters

namespaceURI string
tagName string

Either the local name of elements to look for or the special value "*", which matches all elements.

Returns

IHtmlCollection<IElement>

A collection of elements in the order they appear in the tree.

HasAttribute(string)

Returns a boolean value indicating whether the specified element has the specified attribute or not.

public bool HasAttribute(string name)

Parameters

name string

The attributes name.

Returns

bool

The return value of true or false.

HasAttribute(string?, string)

Returns a boolean value indicating whether the specified element has the specified attribute or not.

public bool HasAttribute(string? namespaceUri, string localName)

Parameters

namespaceUri string

A string specifying the namespace of the attribute.

localName string

The attributes name.

Returns

bool

The return value of true or false.

Insert(AdjacentPosition, string)

Inserts new HTML elements specified by the given HTML string at a position relative to the current element specified by the position.

public void Insert(AdjacentPosition position, string html)

Parameters

position AdjacentPosition

The relation to the current element.

html string

The HTML code to generate elements for.

LocateNamespace(string)

protected override sealed string? LocateNamespace(string prefix)

Parameters

prefix string

Returns

string

LocatePrefix(string)

protected override sealed string? LocatePrefix(string namespaceUri)

Parameters

namespaceUri string

Returns

string

Matches(string)

Checks if the element is matched by the given selector.

public bool Matches(string selectorText)

Parameters

selectorText string

Returns

bool

True if the element would be selected by the specified selector, otherwise false.

ParseSubtree(string)

Takes a given string source and parses it into a subtree using the current element as context. Follows the fragment parsing strategy for the given namespace.

public abstract IElement ParseSubtree(string source)

Parameters

source string

The source to parse into a subtree.

Returns

IElement

The documentElement of the new subtree.

Prepend(params INode[])

Prepends nodes to the current document.

public void Prepend(params INode[] nodes)

Parameters

nodes INode[]

The nodes to prepend.

QuerySelector(string)

Returns the first element within the document (using depth-first pre-order traversal of the document's nodes) that matches the specified group of selectors.

public IElement? QuerySelector(string selectors)

Parameters

selectors string

The group of selectors to use.

Returns

IElement

The found element.

QuerySelectorAll(string)

Returns a list of the elements within the document (using depth-first pre-order traversal of the document's nodes) that match the specified group of selectors.

public IHtmlCollection<IElement> QuerySelectorAll(string selectors)

Parameters

selectors string

The group of selectors to use.

Returns

IHtmlCollection<IElement>

A non-live NodeList of element objects.

Remove()

Removes the current node.

public void Remove()

RemoveAttribute(string)

Removes an attribute from the specified element.

public bool RemoveAttribute(string name)

Parameters

name string

Is a string that names the attribute to be removed.

Returns

bool

True if an attribute was removed, otherwise false.

RemoveAttribute(string?, string)

Removes an attribute from the specified element.

public bool RemoveAttribute(string? namespaceUri, string localName)

Parameters

namespaceUri string

A string specifying the namespace of the attribute.

localName string

Is a string that names the attribute to be removed.

Returns

bool

True if an attribute was removed, otherwise false.

Replace(params INode[])

Replaces the current node with nodes.

public void Replace(params INode[] nodes)

Parameters

nodes INode[]

The nodes to insert.

SetAttribute(string, string?)

Adds a new attribute or changes the value of an existing attribute on the specified element.

public void SetAttribute(string name, string? value)

Parameters

name string

The name of the attribute as a string.

value string

The desired new value of the attribute.

SetAttribute(string?, string, string?)

Adds a new attribute or changes the value of an existing attribute on the specified element.

public void SetAttribute(string? namespaceUri, string name, string? value)

Parameters

namespaceUri string

A string specifying the namespace of the attribute.

name string

The name of the attribute as a string.

value string

The desired new value of the attribute.

UpdateAttribute(string, string)

protected void UpdateAttribute(string name, string value)

Parameters

name string
value string