Element

Struct Element 

Source
pub struct Element { /* private fields */ }
Expand description

A single HTML element.

Implementations§

Source§

impl Element

Source

pub fn select<T: AsRef<str>>(&self, css_query: T) -> Option<ElementList>

Find elements that match the given CSS (or JQuery) selector.

Source

pub fn select_first<T: AsRef<str>>(&self, css_query: T) -> Option<Element>

Find the first element that matches the given CSS (or JQuery) selector.

Source

pub fn attr<T: AsRef<str>>(&self, attr_name: T) -> Option<String>

Get an attribute value by its key.

To get an absolute URL from an attribute that may be a relative URL, prefix the key with abs:.

§Examples
use aidoku::imports::html::Html;
let html = Html::parse_with_url("<img src=\"/image.jpg\" />", "https://example.com").unwrap();
let el = html.select_first("img").unwrap();
assert_eq!(
    el.attr("abs:src"),
    Some("https://example.com/image.jpg".into())
);
Source

pub fn text(&self) -> Option<String>

Get the normalized, combined text of this element and its children.

Whitespace is normalized and trimmed.

Note that this method returns text that would be presented to a reader. The contents of data nodes (e.g. <script> tags) are not considered text, and instead, Element::html or Element::data can be used for them.

§Examples
use aidoku::imports::html::Html;
let html = Html::parse("<p>Hello <b>there</b> now! </p>").unwrap();
let el = html.select_first("p").unwrap();
assert_eq!(el.text(), Some("Hello there now!".into()));
Source

pub fn untrimmed_text(&self) -> Option<String>

Get the text of this element and its children.

Whitespace is not normalized and trimmed.

Notices from Element::text apply.

§Examples
use aidoku::imports::html::Html;
let html = Html::parse("<p>Hello <b>there</b> now! </p>").unwrap();
let el = html.select_first("p").unwrap();
assert_eq!(el.untrimmed_text(), Some("Hello there now! ".into()));
Source

pub fn html(&self) -> Option<String>

Get the element’s inner HTML.

§Examples
use aidoku::imports::html::Html;
let html = Html::parse("<div><p></p></div>").unwrap();
let div = html.select_first("div").unwrap();
assert_eq!(div.html(), Some("<p></p>".into()));
Source

pub fn outer_html(&self) -> Option<String>

Get the element’s outer HTML.

§Examples
use aidoku::imports::html::Html;
let html = Html::parse("<div><p></p></div>").unwrap();
let div = html.select_first("div").unwrap();
assert_eq!(div.outer_html(), Some("<div><p></p></div>".into()));
Source

pub fn remove(self)

Remove this element from the DOM tree.

Source

pub fn parent(&self) -> Option<Element>

Get the element’s parent element, returning None if there isn’t one.

Source

pub fn children(&self) -> ElementList

Get the element’s children elements.

Source

pub fn siblings(&self) -> ElementList

Get the sibling elements of the element.

Source

pub fn next(&self) -> Option<Element>

Get the next sibling of the element, returning None if there isn’t one.

Source

pub fn prev(&self) -> Option<Element>

Get the previous sibling of the element, returning None if there isn’t one.

Source

pub fn set_text<T: AsRef<str>>(&mut self, text: T) -> Result<(), HtmlError>

Set the element’s text content, clearing any existing content.

Source

pub fn set_html<T: AsRef<str>>(&mut self, text: T) -> Result<(), HtmlError>

Set the element’s inner HTML, clearing the existing HTML.

Source

pub fn prepend<T: AsRef<str>>(&mut self, text: T) -> Result<(), HtmlError>

Prepend inner HTML into this element.

The given HTML will be parsed, and each node prepended to the start of the element’s children.

Source

pub fn append<T: AsRef<str>>(&mut self, text: T) -> Result<(), HtmlError>

Append inner HTML into this element.

The given HTML will be parsed, and each node appended to the end of the element’s children.

Source

pub fn base_uri(&self) -> Option<String>

Get the base URI of this Element.

Source

pub fn own_text(&self) -> Option<String>

Gets the (normalized) text owned by this element.

Source

pub fn data(&self) -> Option<String>

Get the combined data (e.g. the inside of a <script> tag) of this element.

Note that data is NOT the text of the element. Use Element::text to get the text that would be visible to a user, and Element::data for the contents of scripts, comments, CSS styles, etc.

Source

pub fn id(&self) -> Option<String>

Get the id attribute of this element.

Source

pub fn tag_name(&self) -> Option<String>

Get the name of the tag for this element.

This will always be the lowercased version. For example, <DIV> and <div> would both return div.

Source

pub fn class_name(&self) -> Option<String>

Get the literal value of this node’s class attribute.

For example, on <div class="header gray"> this would return header gray.

Source

pub fn has_class<T: AsRef<str>>(&self, class_name: T) -> bool

Test if this element has a class. Case insensitive.

Source

pub fn add_class<T: AsRef<str>>( &mut self, class_name: T, ) -> Result<(), HtmlError>

Add a class name to this element’s class attribute.

Source

pub fn remove_class<T: AsRef<str>>( &mut self, class_name: T, ) -> Result<(), HtmlError>

Remove a class name from this element’s class attribute.

Source

pub fn has_attr<T: AsRef<str>>(&self, attr_name: T) -> bool

Test if this element has an attribute. Case insensitive.

Source

pub fn set_attr<K: AsRef<str>, V: AsRef<str>>( &mut self, key: K, value: V, ) -> Result<(), HtmlError>

Set an attribute value on this element.

If this element already has an attribute with the key, its value is updated; otherwise, a new attribute is added.

Source

pub fn remove_attr<T: AsRef<str>>(&mut self, attr: T) -> Result<(), HtmlError>

Remove an attribute from this element.

Trait Implementations§

Source§

impl Display for Element

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Returns the outer HTML of the node.

Source§

impl Drop for Element

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl ElementHelpers for Element

Source§

fn text_with_newlines(&self) -> Option<String>

Get the text of the element(s) and their children. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.