Class Elements
A list of Element s, with methods that act on every element in the list.
To get an
Elements
object, use the
Select(String)
method.
Inheritance
Namespace: Supremes.Nodes
Assembly: Supremes.dll
Syntax
public sealed class Elements : IList<Element>
Properties
Count
Gets the number of elements contained in the Elements.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
First
Get the first matched element.
Declaration
public Element First { get; }
Property Value
Type | Description |
---|---|
Element | The first matched element, or |
Forms
Get the FormElement forms from the selected elements, if any.
Declaration
public IReadOnlyList<FormElement> Forms { get; }
Property Value
Type | Description |
---|---|
IReadOnlyList<FormElement> | a list of FormElement s pulled from the matched elements. The list will be empty if the elements contain no forms. |
HasText
Determine if any of the matched elements have a text content (that is not just whitespace).
Declaration
public bool HasText { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Html
Get the combined inner HTML of all matched elements. Set the inner HTML of each matched element.
Declaration
public string Html { get; set; }
Property Value
Type | Description |
---|---|
System.String | HTML to parse and set into each matched element. |
Remarks
if you want to use fluent API, write using Supremes.Fluent;
.
See Also
Item[Int32]
Gets or sets the element at the specified index.
Declaration
public Element this[int index] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index |
Property Value
Type | Description |
---|---|
Element |
Last
Get the last matched element.
Declaration
public Element Last { get; }
Property Value
Type | Description |
---|---|
Element | The last matched element, or |
OuterHtml
Get the combined outer HTML of all matched elements.
Declaration
public string OuterHtml { get; }
Property Value
Type | Description |
---|---|
System.String | string of all element's outer HTML. |
See Also
Parents
Get all of the parents and ancestor elements of the matched elements.
Declaration
public Elements Parents { get; }
Property Value
Type | Description |
---|---|
Elements | all of the parents and ancestor elements of the matched elements |
TagName
Get the tag name of the first matched element. Set the tag name of each matched element.
Declaration
public string TagName { get; set; }
Property Value
Type | Description |
---|---|
System.String | the new tag name |
Remarks
For example, to change each
<i>
to a
<em>
, do
doc.Select("i").TagName = "em";
if you want to use fluent API, write using Supremes.Fluent;
.
See Also
Text
Get the combined text of all the matched elements.
Declaration
public string Text { get; }
Property Value
Type | Description |
---|---|
System.String | string of all text: unescaped and no HTML. |
Remarks
Note that it is possible to get repeats if the matched elements contain both parent elements and their own children, as the Element.text() method returns the combined text of a parent and all its children.
See Also
Val
Get the form element's value of the first matched element. Set the form element's value in each of the matched elements.
Declaration
public string Val { get; set; }
Property Value
Type | Description |
---|---|
System.String | The value to set into each matched element |
Remarks
if you want to use fluent API, write using Supremes.Fluent;
.
See Also
Methods
Add(Element)
Adds an item to the Elements.
Declaration
public void Add(Element element)
Parameters
Type | Name | Description |
---|---|---|
Element | element |
AddClass(String)
Add the class name to every matched element's
class
attribute.
Declaration
public Elements AddClass(string className)
Parameters
Type | Name | Description |
---|---|---|
System.String | className | class name to add |
Returns
Type | Description |
---|---|
Elements | this |
After(String)
Insert the supplied HTML after each matched element's outer HTML.
Declaration
public Elements After(string html)
Parameters
Type | Name | Description |
---|---|---|
System.String | html | HTML to insert after each element |
Returns
Type | Description |
---|---|
Elements | this, for chaining |
See Also
Append(String)
Add the supplied HTML to the end of each matched element's inner HTML.
Declaration
public Elements Append(string html)
Parameters
Type | Name | Description |
---|---|---|
System.String | html | HTML to add inside each element, after the existing HTML |
Returns
Type | Description |
---|---|
Elements | this, for chaining |
See Also
Attr(String)
Get an attribute value from the first matched element that has the attribute.
Declaration
public string Attr(string attributeKey)
Parameters
Type | Name | Description |
---|---|---|
System.String | attributeKey | The attribute key. |
Returns
Type | Description |
---|---|
System.String | The attribute value from the first matched element that has the attribute. If no elements were matched (isEmpty() == true), or if the no elements have the attribute, returns empty string. |
See Also
Attr(String, String)
Set an attribute on all matched elements.
Declaration
public Elements Attr(string attributeKey, string attributeValue)
Parameters
Type | Name | Description |
---|---|---|
System.String | attributeKey | attribute key |
System.String | attributeValue | attribute value |
Returns
Type | Description |
---|---|
Elements | this |
Before(String)
Insert the supplied HTML before each matched element's outer HTML.
Declaration
public Elements Before(string html)
Parameters
Type | Name | Description |
---|---|---|
System.String | html | HTML to insert before each element |
Returns
Type | Description |
---|---|
Elements | this, for chaining |
See Also
Clear()
Removes all items from the Elements.
Declaration
public void Clear()
Contains(Object)
Determines whether the Elements contains a specific value.
Declaration
public bool Contains(object o)
Parameters
Type | Name | Description |
---|---|---|
System.Object | o |
Returns
Type | Description |
---|---|
System.Boolean |
Empty()
Empty (remove all child nodes from) each matched element.
Declaration
public Elements Empty()
Returns
Type | Description |
---|---|
Elements | this, for chaining |
Remarks
This is similar to setting the inner HTML of each element to nothing.
E.g. HTML:
<div><p>Hello <b>there</b></p> <p>now</p></div>
doc.Select("p").Empty();
HTML =
<div><p></p> <p></p></div>
See Also
Eq(Int32)
Get the nth matched element as an Elements object.
Declaration
public Elements Eq(int index)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | the (zero-based) index of the element in the list to retain |
Returns
Type | Description |
---|---|
Elements | Elements containing only the specified element, or, if that element did not exist, an empty list. |
Remarks
See also
Equals(Object)
Compares two Elements instances for equality.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj |
Returns
Type | Description |
---|---|
System.Boolean |
GetEnumerator()
Returns the hash code for this instance.
Declaration
public IEnumerator<Element> GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<Element> |
GetHashCode()
Returns the hash code for this instance.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
System.Int32 |
HasAttr(String)
Checks if any of the matched elements have this attribute set.
Declaration
public bool HasAttr(string attributeKey)
Parameters
Type | Name | Description |
---|---|---|
System.String | attributeKey | attribute key |
Returns
Type | Description |
---|---|
System.Boolean | true if any of the elements have the attribute; false if none do. |
HasClass(String)
Determine if any of the matched elements have this class name set in their
class
attribute.
Declaration
public bool HasClass(string className)
Parameters
Type | Name | Description |
---|---|---|
System.String | className | class name to check for |
Returns
Type | Description |
---|---|
System.Boolean | true if any do, false if none do |
IndexOf(Element)
Determines the index of a specific item in the Elements.
Declaration
public int IndexOf(Element element)
Parameters
Type | Name | Description |
---|---|---|
Element | element |
Returns
Type | Description |
---|---|
System.Int32 |
Insert(Int32, Element)
Inserts an item to the Elements at the specified index.
Declaration
public void Insert(int index, Element element)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | |
Element | element |
Is(String)
Test if any of the matched elements match the supplied query.
Declaration
public bool Is(string query)
Parameters
Type | Name | Description |
---|---|---|
System.String | query | A selector |
Returns
Type | Description |
---|---|
System.Boolean | true if at least one element in the list matches the query. |
Not(String)
Remove elements from this list that match the Selector query.
Declaration
public Elements Not(string query)
Parameters
Type | Name | Description |
---|---|---|
System.String | query | the selector query whose results should be removed from these elements |
Returns
Type | Description |
---|---|
Elements | a new elements list that contains only the filtered results |
Remarks
E.g. HTML:
<div class=logo>One</div> <div>Two</div>
Elements divs = doc.select("div").not("#logo");
Result:
divs: [
Prepend(String)
Add the supplied HTML to the start of each matched element's inner HTML.
Declaration
public Elements Prepend(string html)
Parameters
Type | Name | Description |
---|---|---|
System.String | html | HTML to add inside each element, before the existing HTML |
Returns
Type | Description |
---|---|
Elements | this, for chaining |
See Also
Remove()
Remove each matched element from the DOM.
Declaration
public Elements Remove()
Returns
Type | Description |
---|---|
Elements | this, for chaining |
Remarks
This is similar to setting the outer HTML of each element to nothing.
E.g. HTML:
<div><p>Hello</p> <p>there</p> <img /></div>
doc.Select("p").Remove();
HTML =
<div> <img /></div>
Note that this method should not be used to clean user-submitted HTML; rather, use Cleaner to clean HTML.
See Also
Remove(Element)
Removes the first occurrence of a specific object from the Elements.
Declaration
public bool Remove(Element element)
Parameters
Type | Name | Description |
---|---|---|
Element | element |
Returns
Type | Description |
---|---|
System.Boolean |
RemoveAt(Int32)
Removes the Elements item at the specified index.
Declaration
public void RemoveAt(int index)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index |
RemoveAttr(String)
Remove an attribute from every matched element.
Declaration
public Elements RemoveAttr(string attributeKey)
Parameters
Type | Name | Description |
---|---|---|
System.String | attributeKey | The attribute to remove. |
Returns
Type | Description |
---|---|
Elements | this (for chaining) |
RemoveClass(String)
Remove the class name from every matched element's
class
attribute, if present.
Declaration
public Elements RemoveClass(string className)
Parameters
Type | Name | Description |
---|---|---|
System.String | className | class name to remove |
Returns
Type | Description |
---|---|
Elements | this |
Select(String)
Find matching elements within this element list.
Declaration
public Elements Select(string query)
Parameters
Type | Name | Description |
---|---|---|
System.String | query | A Selector query |
Returns
Type | Description |
---|---|
Elements | the filtered list of elements, or an empty list if none match. |
ToggleClass(String)
Toggle the class name on every matched element's
class
attribute.
Declaration
public Elements ToggleClass(string className)
Parameters
Type | Name | Description |
---|---|---|
System.String | className | class name to add if missing, or remove if present, from every element. |
Returns
Type | Description |
---|---|
Elements | this |
ToString()
Get the combined outer HTML of all matched elements.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
System.String | string of all element's outer HTML. |
Remarks
Alias of OuterHtml .
See Also
Unwrap()
Removes the matched elements from the DOM, and moves their children up into their parents.
Declaration
public Elements Unwrap()
Returns
Type | Description |
---|---|
Elements | this (for chaining) |
Remarks
This has the effect of dropping the elements but keeping their children.
This is useful for e.g removing unwanted formatting elements but keeping their contents.
E.g. with HTML:
<div><font>One</font> <font><a href="/">Two</a></font></div>
doc.Select("font").Unwrap();
HTML =
<div>One <a href="/">Two</a></div>
See Also
Wrap(String)
Wrap the supplied HTML around each matched elements.
Declaration
public Elements Wrap(string html)
Parameters
Type | Name | Description |
---|---|---|
System.String | html | HTML to wrap around each element, e.g.
|
Returns
Type | Description |
---|---|
Elements | this (for chaining) |
Remarks
For example, with HTML
<p><b>This</b> is <b>Nsoup</b></p>
,
doc.Select("b").Wrap("<i></i>");
becomes
<p><i><b>This</b></i> is <i><b>jsoup</b></i></p>