pub struct Element { /* private fields */ }Expand description
A single HTML element.
Implementations§
Source§impl Element
impl Element
Sourcepub fn select<T: AsRef<str>>(&self, css_query: T) -> Option<ElementList>
pub fn select<T: AsRef<str>>(&self, css_query: T) -> Option<ElementList>
Find elements that match the given CSS (or JQuery) selector.
Sourcepub fn select_first<T: AsRef<str>>(&self, css_query: T) -> Option<Element>
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.
Sourcepub fn attr<T: AsRef<str>>(&self, attr_name: T) -> Option<String>
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())
);Sourcepub fn text(&self) -> Option<String>
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()));Sourcepub fn untrimmed_text(&self) -> Option<String>
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()));Sourcepub fn outer_html(&self) -> Option<String>
pub fn outer_html(&self) -> Option<String>
Sourcepub fn parent(&self) -> Option<Element>
pub fn parent(&self) -> Option<Element>
Get the element’s parent element, returning None if there isn’t one.
Sourcepub fn children(&self) -> ElementList ⓘ
pub fn children(&self) -> ElementList ⓘ
Get the element’s children elements.
Sourcepub fn siblings(&self) -> ElementList ⓘ
pub fn siblings(&self) -> ElementList ⓘ
Get the sibling elements of the element.
Sourcepub fn next(&self) -> Option<Element>
pub fn next(&self) -> Option<Element>
Get the next sibling of the element, returning None if there isn’t one.
Sourcepub fn prev(&self) -> Option<Element>
pub fn prev(&self) -> Option<Element>
Get the previous sibling of the element, returning None if there isn’t one.
Sourcepub fn set_text<T: AsRef<str>>(&mut self, text: T) -> Result<(), HtmlError>
pub fn set_text<T: AsRef<str>>(&mut self, text: T) -> Result<(), HtmlError>
Set the element’s text content, clearing any existing content.
Sourcepub fn set_html<T: AsRef<str>>(&mut self, text: T) -> Result<(), HtmlError>
pub fn set_html<T: AsRef<str>>(&mut self, text: T) -> Result<(), HtmlError>
Set the element’s inner HTML, clearing the existing HTML.
Sourcepub fn prepend<T: AsRef<str>>(&mut self, text: T) -> Result<(), HtmlError>
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.
Sourcepub fn append<T: AsRef<str>>(&mut self, text: T) -> Result<(), HtmlError>
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.
Sourcepub fn data(&self) -> Option<String>
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.
Sourcepub fn tag_name(&self) -> Option<String>
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.
Sourcepub fn class_name(&self) -> Option<String>
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.
Sourcepub fn has_class<T: AsRef<str>>(&self, class_name: T) -> bool
pub fn has_class<T: AsRef<str>>(&self, class_name: T) -> bool
Test if this element has a class. Case insensitive.
Sourcepub fn add_class<T: AsRef<str>>(
&mut self,
class_name: T,
) -> Result<(), HtmlError>
pub fn add_class<T: AsRef<str>>( &mut self, class_name: T, ) -> Result<(), HtmlError>
Add a class name to this element’s class attribute.
Sourcepub fn remove_class<T: AsRef<str>>(
&mut self,
class_name: T,
) -> Result<(), HtmlError>
pub fn remove_class<T: AsRef<str>>( &mut self, class_name: T, ) -> Result<(), HtmlError>
Remove a class name from this element’s class attribute.
Sourcepub fn has_attr<T: AsRef<str>>(&self, attr_name: T) -> bool
pub fn has_attr<T: AsRef<str>>(&self, attr_name: T) -> bool
Test if this element has an attribute. Case insensitive.