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
Element(Document, string, string, string?, string, NodeFlags)
public Element(Document owner, string name, string localName, string? prefix, string namespaceUri, NodeFlags flags = NodeFlags.None)
Parameters
Properties
AssignedSlot
Gets the assigned slot of the current element, if any.
public IElement? AssignedSlot { get; }
Property Value
ChildElementCount
Gets the number of child elements.
public int ChildElementCount { get; }
Property Value
Children
Gets the child elements.
public IHtmlCollection<IElement> Children { get; }
Property Value
ClassList
Gets the list of class names.
public ITokenList ClassList { get; }
Property Value
ClassName
Gets or sets the value of the class attribute.
public string? ClassName { get; set; }
Property Value
FirstElementChild
Gets the first child element of this element.
public IElement? FirstElementChild { get; }
Property Value
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
Id
Gets or sets the id value of the element.
public string? Id { get; set; }
Property Value
InnerHtml
Gets or sets the inner HTML (excluding the current element) of the element.
public string InnerHtml { get; set; }
Property Value
IsFocused
Gets if the element is currently focused.
public bool IsFocused { get; protected set; }
Property Value
LastElementChild
Gets the last child element of this element.
public IElement? LastElementChild { get; }
Property Value
LocalName
Gets the local part of the qualified name of this element.
public string LocalName { get; }
Property Value
NamespaceUri
Gets the namespace URI of this element.
public string? NamespaceUri { get; }
Property Value
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
OuterHtml
Gets or sets the outer HTML (including the current element) of the element.
public string OuterHtml { get; set; }
Property Value
Prefix
Gets the namespace prefix of this element.
public string? Prefix { get; }
Property Value
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
ShadowRoot
Gets the shadow root of the current element, if any.
public IShadowRoot? ShadowRoot { get; }
Property Value
Slot
Gets the value of the slot attribute.
public string? Slot { get; set; }
Property Value
SourceReference
Gets the source reference if available.
public ISourceReference? SourceReference { get; set; }
Property Value
TagName
Gets the name of the tag that represents the current element.
public string TagName { get; }
Property Value
TextContent
Gets or sets the textual content of an element and all its descendants.
public override string TextContent { get; set; }
Property Value
Methods
AddAttribute(Attr)
Adds an attribute.
public void AddAttribute(Attr attr)
Parameters
attr
AttrThe 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
ShadowRootModeThe 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
Returns
- Node
The cloned node.
CloneElement(Element, Document, bool)
protected void CloneElement(Element element, Document owner, bool deep)
Parameters
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
GetAttribute(string)
Returns the value of the named attribute on the specified element.
public string? GetAttribute(string name)
Parameters
name
stringThe 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
stringA string specifying the namespace of the attribute.
localName
stringThe 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
stringA 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
stringA 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
stringtagName
stringEither 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
stringThe 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
stringA string specifying the namespace of the attribute.
localName
stringThe 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
AdjacentPositionThe relation to the current element.
html
stringThe HTML code to generate elements for.
LocateNamespace(string)
protected override sealed string? LocateNamespace(string prefix)
Parameters
prefix
string
Returns
LocatePrefix(string)
protected override sealed string? LocatePrefix(string namespaceUri)
Parameters
namespaceUri
string
Returns
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
stringThe 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
stringThe 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
stringThe 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
stringIs 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
stringA string specifying the namespace of the attribute.
localName
stringIs 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
stringThe name of the attribute as a string.
value
stringThe 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
stringA string specifying the namespace of the attribute.
name
stringThe name of the attribute as a string.
value
stringThe desired new value of the attribute.
UpdateAttribute(string, string)
protected void UpdateAttribute(string name, string value)