Table of Contents

Class ElementExtensions

Namespace
AngleSharp.Dom
Assembly
AngleSharp.dll

Useful methods for element objects.

public static class ElementExtensions
Inheritance
ElementExtensions
Inherited Members

Methods

AddClass<T>(T, string)

Adds the specified class name(s) for all elements in the given collection.

public static T AddClass<T>(this T elements, string className) where T : class, IEnumerable<IElement>

Parameters

elements T

The collection.

className string

The name(s) of the class(es).

Returns

T

The collection itself.

Type Parameters

T

The type of element collection.

After<T>(T, string)

Inserts the nodes generated from the given HTML code after each element of the provided elements.

public static T After<T>(this T elements, string html) where T : class, IEnumerable<IElement>

Parameters

elements T

The elements to iterate through.

html string

The HTML code that generates the nodes.

Returns

T

The unchanged collection.

Type Parameters

T

The type of collection.

Append<T>(T, string)

Appends the nodes generated from the given HTML code to each element of the provided elements.

public static T Append<T>(this T elements, string html) where T : class, IEnumerable<IElement>

Parameters

elements T

The elements to iterate through.

html string

The HTML code that generates the nodes.

Returns

T

The unchanged collection.

Type Parameters

T

The type of collection.

Attr<T>(T, IEnumerable<KeyValuePair<string, string>>)

Sets the specified attribute name to the specified value for all elements in the given collection.

public static T Attr<T>(this T elements, IEnumerable<KeyValuePair<string, string>> attributes) where T : class, IEnumerable<IElement>

Parameters

elements T

The collection.

attributes IEnumerable<KeyValuePair<string, string>>

An enumeration of attributes in form of key-value pairs.

Returns

T

The collection itself.

Type Parameters

T

The type of element collection.

Attr<T>(T, object)

Sets the specified attribute name to the specified value for all elements in the given collection.

public static T Attr<T>(this T elements, object attributes) where T : class, IEnumerable<IElement>

Parameters

elements T

The collection of elements.

attributes object

An enumeration of attributes in form of an anonymous object, that carries key-value pairs.

Returns

T

The collection itself.

Type Parameters

T

The type of element collection.

Attr<T>(T, string)

Gets the values of the specified attribute for all elements in the given collection.

public static IEnumerable<string> Attr<T>(this T elements, string attributeName) where T : IEnumerable<IElement>

Parameters

elements T

The collection of elements.

attributeName string

The name of the attribute.

Returns

IEnumerable<string>

The attributes' values.

Type Parameters

T

The type of element collection.

Attr<T>(T, string, string)

Sets the specified attribute name to the specified value for all elements in the given collection.

public static T Attr<T>(this T elements, string attributeName, string attributeValue) where T : class, IEnumerable<IElement>

Parameters

elements T

The collection.

attributeName string

The name of the attribute.

attributeValue string

The value of the attribute.

Returns

T

The collection itself.

Type Parameters

T

The type of element collection.

Before<T>(T, string)

Inserts the nodes generated from the given HTML code before each element of the provided elements.

public static T Before<T>(this T elements, string html) where T : class, IEnumerable<IElement>

Parameters

elements T

The elements to iterate through.

html string

The HTML code that generates the nodes.

Returns

T

The unchanged collection.

Type Parameters

T

The type of collection.

ClearAttr(IElement)

Clears the attributes of the given element.

public static IElement ClearAttr(this IElement element)

Parameters

element IElement

The element to clear.

Returns

IElement

The element itself.

ClearAttr<T>(T)

Clears the attributes of all elements.

public static T ClearAttr<T>(this T elements) where T : class, IEnumerable<IElement>

Parameters

elements T

The collection to clear.

Returns

T

The collection itself.

Type Parameters

T

The type of element collection.

CreateRequestFor(IElement, Url)

Creates a new resource request from the provided element for the given url.

public static ResourceRequest CreateRequestFor(this IElement element, Url url)

Parameters

element IElement

The element creating a request.

url Url

The address that specifies the target.

Returns

ResourceRequest

The new resource request with the information.

Empty(IElement)

Empties the provided element.

public static IElement Empty(this IElement element)

Parameters

element IElement

The element to empty.

Returns

IElement

The element itself.

Empty<T>(T)

Empties all provided elements.

public static T Empty<T>(this T elements) where T : class, IEnumerable<IElement>

Parameters

elements T

The collection.

Returns

T

The collection itself.

Type Parameters

T

The type of element collection.

GetCssNamespace(IElement, string)

Gets the CSS namespace that is defined via the provided prefix.

public static string? GetCssNamespace(this IElement el, string prefix)

Parameters

el IElement

The element that is connected to a doc.

prefix string

The prefix to lookup.

Returns

string

The namespace url for the prefix.

GetNamespaceUri(IElement)

Gets the non-fixed namespace of the element.

public static string? GetNamespaceUri(this IElement element)

Parameters

element IElement

The element.

Returns

string

The url of the namespace.

GetSelector(IElement)

Creates a unique selector path used to locate the element in the DOM.

public static string GetSelector(this IElement element)

Parameters

element IElement

The starting node to create the selector path from.

Returns

string

The unique selector path for this element.

HasClass(IEnumerable<IElement>, string)

Checks if any element in the given collection has the given class(es).

public static bool HasClass(this IEnumerable<IElement> elements, string className)

Parameters

elements IEnumerable<IElement>

The collection of elements.

className string

The name(s) of the class(es).

Returns

bool

True if any element has the class(es), otherwise false.

Html(IElement)

Gets the inner HTML of the given element.

public static string Html(this IElement element)

Parameters

element IElement

The element.

Returns

string

The source code of the inner HTML.

Html<T>(T, string)

Sets the inner HTML of the given elements.

public static T Html<T>(this T elements, string html) where T : class, IEnumerable<IElement>

Parameters

elements T

The collection.

html string

The source code of the inner HTML to set.

Returns

T

The collection itself.

Type Parameters

T

The type of collection.

IsActive(IElement)

Checks if the element is currently active.

public static bool IsActive(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is currently active, otherwise false.

IsChecked(IElement)

Checks if the element is currently checked.

public static bool IsChecked(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is currently checked, otherwise false.

IsDefault(IElement)

Checks if the element is an input element that is currently in its default state.

public static bool IsDefault(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is currently in its default state, otherwise false.

IsDisabled(IElement)

Checks if the element is currently disabled.

public static bool IsDisabled(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is currently disabled, otherwise false.

IsEditable(IElement)

Checks if the element is editable.

public static bool IsEditable(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element can be edited, otherwise false.

IsEnabled(IElement)

Checks if the element is currently enabled.

public static bool IsEnabled(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is currently enabled, otherwise false.

IsFirstChild(IElement)

Checks if the element is its parent's first child.

public static bool IsFirstChild(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element's first child is the given one, otherwise false.

IsFirstOfType(IElement)

Checks if the element is the first of its type among the parent's children.

public static bool IsFirstOfType(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is the first of its type among its siblings.

IsHovered(IElement)

Checks if the element is currently hovered.

public static bool IsHovered(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is currently hovered, otherwise false.

IsInRange(IElement)

Checks if the element's value is within the range.

public static bool IsInRange(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is valid, otherwise false.

IsIndeterminate(IElement)

Checks if the element is currently in its indeterminate state.

public static bool IsIndeterminate(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is currently indeterminate, otherwise false.

IsInvalid(IElement)

Checks if the element does not validate.

public static bool IsInvalid(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is invalid, otherwise false.

IsLastChild(IElement)

Checks if the element is its parent's last child.

public static bool IsLastChild(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element's last child is the given one, otherwise false.

IsLastOfType(IElement)

Checks if the element is the last of its type among the parent's children.

public static bool IsLastOfType(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is the last of its type among its siblings.

Checks if the element is a link.

public static bool IsLink(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is a link, otherwise false.

IsMutable(IHtmlInputElement)

Checks if the input can be changed.

public static bool IsMutable(this IHtmlInputElement input)

Parameters

input IHtmlInputElement

The input to check.

Returns

bool

True if the element is mutable, otherwise false.

IsMutable(IHtmlTextAreaElement)

Checks if the textarea can be changed.

public static bool IsMutable(this IHtmlTextAreaElement textArea)

Parameters

textArea IHtmlTextAreaElement

The area to check.

Returns

bool

True if the element is mutable, otherwise false.

IsOnlyChild(IElement)

Checks if the element is its parent's only child.

public static bool IsOnlyChild(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element has no siblings, otherwise false.

IsOnlyOfType(IElement)

Checks if the element is the only of its type among the parent's children.

public static bool IsOnlyOfType(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is the only of its type among its siblings.

IsOptional(IElement)

Checks if the element is only optional and not required.

public static bool IsOptional(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is optional, otherwise false.

IsOutOfRange(IElement)

Checks if the element's value is out-of-range.

public static bool IsOutOfRange(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is invalid, otherwise false.

IsPlaceholderShown(IElement)

Checks if the element is currently showing a placeholder.

public static bool IsPlaceholderShown(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is currently showing a placeholder, otherwise false.

IsPseudo(IElement, string)

Checks if the element is a pseudo element (before or after).

public static bool IsPseudo(this IElement element, string name)

Parameters

element IElement

The element to check.

name string

The pseudo element's name.

Returns

bool

True if the element is a pseudo element, otherwise false.

IsReadOnly(IElement)

Checks if the element is readonly.

public static bool IsReadOnly(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is readonly, otherwise false.

IsRequired(IElement)

Checks if the element is required and must be filled out.

public static bool IsRequired(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is required, otherwise false.

IsShadow(IElement)

Checks if the element hosts a shadow tree.

public static bool IsShadow(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element hosts a shadow tree.

IsTarget(IElement)

Checks if the element is currently targeted.

public static bool IsTarget(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element's ID is equal to the hash.

IsUnchecked(IElement)

Checks if the element is currently unchecked.

public static bool IsUnchecked(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is currently unchecked, otherwise false.

IsValid(IElement)

Checks if the element does validate.

public static bool IsValid(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is valid, otherwise false.

IsVisible(IElement)

Checks if the element is visible on screen.

public static bool IsVisible(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element is visible, otherwise false.

IsVisited(IElement)

Checks if the element has already been visited.

public static bool IsVisited(this IElement element)

Parameters

element IElement

The element to check.

Returns

bool

True if the element has been visited, otherwise false.

LocateNamespaceFor(IElement, string)

Locates the namespace of the given prefix.

public static string? LocateNamespaceFor(this IElement element, string prefix)

Parameters

element IElement

The element that might contain the namespace information.

prefix string

The prefix of the namespace to find.

Returns

string

The url of the namespace or null, if the prefix could not be found.

LocatePrefixFor(IElement, string)

Locates the prefix of the given namespace.

public static string? LocatePrefixFor(this IElement element, string namespaceUri)

Parameters

element IElement

The element that might contain the namespace information.

namespaceUri string

The url of the namespace.

Returns

string

The prefix or null, if the namespace could not be found.

MatchesCssNamespace(IElement, string)

Checks if the element with the provided prefix matches the CSS namespace.

public static bool MatchesCssNamespace(this IElement el, string prefix)

Parameters

el IElement

The element to examine.

prefix string

The namespace in question.

Returns

bool

True if the namespace is matched, else false.

NavigateAsync<TElement>(TElement)

Navigates to the hyper reference given by the provided element without any possibility for cancellation.

public static Task<IDocument> NavigateAsync<TElement>(this TElement element) where TElement : class, IUrlUtilities, IElement

Parameters

element TElement

The element of navigation.

Returns

Task<IDocument>

The task eventually resulting in the response.

Type Parameters

TElement

The type of element.

NavigateAsync<TElement>(TElement, CancellationToken)

Navigates to the hyper reference given by the provided element.

public static Task<IDocument> NavigateAsync<TElement>(this TElement element, CancellationToken cancel) where TElement : class, IUrlUtilities, IElement

Parameters

element TElement

The element of navigation.

cancel CancellationToken

The token for cancellation.

Returns

Task<IDocument>

The task eventually resulting in the response.

Type Parameters

TElement

The type of element.

Prepend<T>(T, string)

Prepends the nodes generated from the given HTML code to each element of the provided elements.

public static T Prepend<T>(this T elements, string html) where T : class, IEnumerable<IElement>

Parameters

elements T

The elements to iterate through.

html string

The HTML code that generates the nodes.

Returns

T

The unchanged collection.

Type Parameters

T

The type of collection.

RemoveClass<T>(T, string)

Removes the specified class name(s) for all elements in the given collection.

public static T RemoveClass<T>(this T elements, string className) where T : class, IEnumerable<IElement>

Parameters

elements T

The collection.

className string

The name(s) of the class(es).

Returns

T

The collection itself.

Type Parameters

T

The type of element collection.

ToCollection<TElement>(IEnumerable<TElement>)

Creates a new HTML Collection from the provided elements.

public static IHtmlCollection<TElement> ToCollection<TElement>(this IEnumerable<TElement> elements) where TElement : class, IElement

Parameters

elements IEnumerable<TElement>

The elements to include.

Returns

IHtmlCollection<TElement>

The created collection.

Type Parameters

TElement

The base type of the elements.

ToggleClass<T>(T, string)

Toggles the specified class name(s) for all elements in the given collection.

public static T ToggleClass<T>(this T elements, string className) where T : class, IEnumerable<IElement>

Parameters

elements T

The collection.

className string

The name(s) of the class(es).

Returns

T

The collection itself.

Type Parameters

T

The type of element collection.

TryLocateCustomNamespace(IElement, string, out string?)

Tries to locate a custom namespace URI.

public static bool TryLocateCustomNamespace(this IElement element, string prefix, out string? namespaceUri)

Parameters

element IElement

The element to locate the namespace URI for.

prefix string

The prefix of the custom namespace.

namespaceUri string

The located namespace URI.

Returns

bool

True if the namespace URI could be located, otherwise false.

TryLocateStandardNamespace(IElement, out string?)

Tries to locate the standard namespace URI.

public static bool TryLocateStandardNamespace(this IElement element, out string? namespaceUri)

Parameters

element IElement

The element to locate the namespace URI for.

namespaceUri string

The located namespace URI.

Returns

bool

True if the namespace URI could be located, otherwise false.

WrapAll<T>(T, string)

Wraps all elements in the inner most element of the tree generated form the provided HTML code. The tree is appended before the first element of the given list.

public static T WrapAll<T>(this T elements, string html) where T : class, IEnumerable<IElement>

Parameters

elements T

The elements to wrap.

html string

The HTML code that generates the tree.

Returns

T

The unchanged collection.

Type Parameters

T

The type of collection.

WrapInner<T>(T, string)

Wraps the content of the given elements in the inner most element of the tree generated form the provided HTML code.

public static T WrapInner<T>(this T elements, string html) where T : class, IEnumerable<IElement>

Parameters

elements T

The elements to iterate through.

html string

The HTML code that generates the tree.

Returns

T

The unchanged collection.

Type Parameters

T

The type of collection.

Wrap<T>(T, string)

Wraps the given elements in the inner most element of the tree generated form the provided HTML code.

public static T Wrap<T>(this T elements, string html) where T : class, IEnumerable<IElement>

Parameters

elements T

The elements to iterate through.

html string

The HTML code that generates the tree.

Returns

T

The unchanged collection.

Type Parameters

T

The type of collection.