Table of Contents

Class NodeExtensions

Namespace
AngleSharp.Dom
Assembly
AngleSharp.dll

Useful methods for node objects.

public static class NodeExtensions
Inheritance
NodeExtensions
Inherited Members

Methods

EnsurePreInsertionValidity(INode, INode, INode?)

Ensures the validity for inserting the given node at parent before the provided child. Throws an error is the insertation is invalid.

public static void EnsurePreInsertionValidity(this INode parent, INode node, INode? child)

Parameters

parent INode

The origin that will be mutated.

node INode

The node to be inserted.

child INode

The reference node of the insertation.

FindChild<TNode>(INode)

Tries to find a direct child of a certain type.

public static TNode? FindChild<TNode>(this INode parent) where TNode : class, INode

Parameters

parent INode

The parent that contains the elements.

Returns

TNode

The instance or null.

Type Parameters

TNode

The node type to find.

FindDescendant<TNode>(INode, int)

Tries to find a descendant of a certain type.

public static TNode? FindDescendant<TNode>(this INode parent, int maxDepth = 1024) where TNode : class, INode

Parameters

parent INode

The parent that contains the elements.

maxDepth int

The maximum depth to allow for searching. A value of 0 is equivalent to FindChild.

Returns

TNode

The instance or null.

Type Parameters

TNode

The node type to find.

GetAncestor<T>(INode)

Gets the first ancestor node that is of the specified type.

public static T? GetAncestor<T>(this INode node) where T : INode

Parameters

node INode

The child of the potential ancestor.

Returns

T

The specified ancestor or its default value.

Type Parameters

T

GetAncestors(INode)

Gets the ancestor nodes of the provided node, in tree order.

public static IEnumerable<INode> GetAncestors(this INode node)

Parameters

node INode

The child of the ancestors.

Returns

IEnumerable<INode>

An iterator over all ancestors.

GetAssignedSlot(IShadowRoot, string?)

Gets the assigned slot given by the shadow root and the slot name.

public static IElement? GetAssignedSlot(this IShadowRoot root, string? name)

Parameters

root IShadowRoot

The shadow tree hosting the slots.

name string

The name of the slot to target.

Returns

IElement

The slot or default slot, if any.

GetAssociatedHost(INode)

Gets the associated host object, if any. This is mostly interesting for the HTML5 template tag.

public static INode? GetAssociatedHost(this INode node)

Parameters

node INode

The node that probably has an host object

Returns

INode

The host object or null.

GetDescendants(INode)

Gets the descendant nodes of the provided parent, in tree order.

public static IEnumerable<INode> GetDescendants(this INode parent)

Parameters

parent INode

The parent of the descendants.

Returns

IEnumerable<INode>

An iterator over all descendants.

GetDescendantsAndSelf(INode)

Gets the descendant nodes and itself of the provided parent, in tree order.

public static IEnumerable<INode> GetDescendantsAndSelf(this INode parent)

Parameters

parent INode

The parent of the descendants.

Returns

IEnumerable<INode>

An iterator over all descendants and itself.

GetElementCount(INode)

Gets the element count of the given node.

public static int GetElementCount(this INode parent)

Parameters

parent INode

The parent of potential element nodes.

Returns

int

The number of element nodes in the parent.

GetInclusiveAncestors(INode)

Gets the inclusive ancestor nodes of the provided node, in tree order.

public static IEnumerable<INode> GetInclusiveAncestors(this INode node)

Parameters

node INode

The child of the ancestors.

Returns

IEnumerable<INode>

An iterator over all ancestors including the given node.

GetRoot(INode)

Gets the root of the given node, which is the node itself, if it has no parent, or the root of the parent.

public static INode GetRoot(this INode node)

Parameters

node INode

The node to get the root of.

Returns

INode

The root node.

HasDataListAncestor(INode)

Checks if any parent is an HTML datalist element..

public static bool HasDataListAncestor(this INode child)

Parameters

child INode

The node to use as starting point.

Returns

bool

True if a datalist element is among the ancestors, otherwise false.

HasTextNodes(INode)

Checks if the node has any text node children.

public static bool HasTextNodes(this INode node)

Parameters

node INode

The parent of the potential text nodes.

Returns

bool

True if the node has any text nodes, otherwise false.

HyperReference(INode, string)

Gets the hyperreference of the given URL - transforming the given (relative) URL to an absolute URL if required.

public static Url? HyperReference(this INode node, string url)

Parameters

node INode

The node that spawns the hyper reference.

url string

The given URL.

Returns

Url

The absolute URL.

Index(INode)

Gets the index of the provided node in the parent's collection.

public static int Index(this INode node)

Parameters

node INode

The node which needs to know its index.

Returns

int

The index of the node or -1 if the node is not a child of a parent.

Index(IEnumerable<INode>, INode)

Gets the index of the given item in the list of nodes.

public static int Index(this IEnumerable<INode> nodes, INode item)

Parameters

nodes IEnumerable<INode>

The source list of nodes.

item INode

The item to search for.

Returns

int

The index of the item or -1 if not found.

IndexOf(INode, INode)

Finds the index of the given node of the provided parent node.

public static int IndexOf(this INode parent, INode node)

Parameters

parent INode

The parent of the given node.

node INode

The node which needs to know its index.

Returns

int

The node's index or -1 if the node is not a child of the parent.

IsAncestorOf(INode, INode)

Checks if the parent is an ancestor of the given node.

public static bool IsAncestorOf(this INode parent, INode node)

Parameters

parent INode

The possible parent to use.

node INode

The node to check for being descendant.

Returns

bool

True if the given parent is actually an ancestor of the node.

IsDescendantOf(INode, INode)

Checks if the node is an descendant of the given parent.

public static bool IsDescendantOf(this INode node, INode parent)

Parameters

node INode

The descendant node to use.

parent INode

The possible parent to use.

Returns

bool

True if the given parent is actually an ancestor of the node.

IsEndPoint(INode)

Checks if the provided node is an endpoint, i.e., does not host any other node.

public static bool IsEndPoint(this INode node)

Parameters

node INode

The node that is checked.

Returns

bool

True if the node is an endpoint, otherwise false.

IsFollowedByDoctype(INode?)

Checks if the given child is followed by a document type.

public static bool IsFollowedByDoctype(this INode? child)

Parameters

child INode

The child that precedes the doctype.

Returns

bool

True if a doctype node is following the child, otherwise false.

IsFollowing(INode, INode)

Checks if the context node is after the provided node.

public static bool IsFollowing(this INode after, INode before)

Parameters

after INode

The context node.

before INode

The provided ref node.

Returns

bool

True if the context node is following the ref node in tree order.

IsHostIncludingInclusiveAncestor(INode, INode)

Checks for an inclusive ancestor relationship or if the host (if any) has such a relationship.

public static bool IsHostIncludingInclusiveAncestor(this INode parent, INode node)

Parameters

parent INode

The possible parent to use.

node INode

The node to check for being descendant.

Returns

bool

True if the given parent is actually an inclusive ancestor (including the host) of the provided node.

IsInclusiveAncestorOf(INode, INode)

Checks if the parent is an inclusive ancestor of the given node.

public static bool IsInclusiveAncestorOf(this INode parent, INode node)

Parameters

parent INode

The possible parent to use.

node INode

The node to check for being descendant.

Returns

bool

True if the given parent is actually an inclusive ancestor of the provided node.

IsInclusiveDescendantOf(INode, INode)

Checks if the node is an inclusive descendant of the given parent.

public static bool IsInclusiveDescendantOf(this INode node, INode parent)

Parameters

node INode

The descendant node to use.

parent INode

The possible parent to use.

Returns

bool

True if the given parent is actually an inclusive ancestor of the provided node.

IsInsertable(INode)

Checks if the provided node can be inserted into some other node. This excludes, e.g., documents from being inserted.

public static bool IsInsertable(this INode node)

Parameters

node INode

The node that is checked.

Returns

bool

True if the node is insertable, otherwise false.

IsPrecededByElement(INode)

Checks if the given child is preceded by an element node.

public static bool IsPrecededByElement(this INode child)

Parameters

child INode

The child that follows any element.

Returns

bool

True if an element node is preceded the child, otherwise false.

IsPreceding(INode, INode)

Checks if the context node is before the provided node.

public static bool IsPreceding(this INode before, INode after)

Parameters

before INode

The context node.

after INode

The provided ref node.

Returns

bool

True if the context node is preceding the ref node in tree order.

IsSiblingOf(INode, INode)

Checks if the current node is a sibling of the specified element.

public static bool IsSiblingOf(this INode node, INode element)

Parameters

node INode

The maybe sibling.

element INode

The node to check for having the same parent.

Returns

bool

True if the parent is actually non-null and actually the same.

PreInsert(INode, INode, INode?)

Pre-inserts the given node at the parent before the provided child.

public static INode PreInsert(this INode parent, INode node, INode? child)

Parameters

parent INode

The origin that will be mutated.

node INode

The node to be inserted.

child INode

The reference node of the insertation.

Returns

INode

The inserted node, which is node.

PreRemove(INode, INode)

Pre-removes the given child of the parent.

public static INode PreRemove(this INode parent, INode child)

Parameters

parent INode

The origin that will be mutated.

child INode

The node that will be removed.

Returns

INode

The removed node, which is child.

Text(INode)

Gets the content text of the given DOM node.

public static string Text(this INode node)

Parameters

node INode

The node to stringify.

Returns

string

The text of the node and its children.

Text<T>(T, string)

Sets the text content of the given elements.

public static T Text<T>(this T nodes, string text) where T : IEnumerable<INode>

Parameters

nodes T

The collection.

text string

The text that should be set.

Returns

T

The collection itself.

Type Parameters

T

The type of collection.