Skip to main content

Element

Struct Element 

Source
pub struct Element<'html> {
    pub name: &'html str,
    pub class: Option<&'html str>,
    pub id: Option<&'html str>,
    pub inner_html: Option<&'html str>,
    pub text_content: Option<Range<usize>>,
    pub attributes: Option<Range<u32>>,
    pub first_child_query: Option<QueryId>,
    pub next_sibling: Option<ElementId>,
}
Expand description

A matched HTML element stored in the Store.

Each Element represents one HTML tag that was captured during parsing. It holds zero-copy &str references into the original HTML source for its name, class, id, and inner HTML.

§Accessing Data

DataHow to access
Tag nameelement.name
Classelement.class
IDelement.id
Inner HTMLelement.inner_html
Text contentelement.text_content(&store)
All attributeselement.attributes(&store)
Single attributeelement.attribute(&store, "href")
Child query resultselement.get(&store, "selector")

Fields§

§name: &'html str

The tag name (e.g. "a", "div", "section").

§class: Option<&'html str>

The value of the class attribute, if present.

§id: Option<&'html str>

The value of the id attribute, if present.

§inner_html: Option<&'html str>

The raw HTML between the element’s opening and closing tags. Only populated when Save::inner_html was true.

§text_content: Option<Range<usize>>

Internal range into the shared text-content buffer. Use Element::text_content to get the actual &str.

§attributes: Option<Range<u32>>

Internal range into the attribute arena. Use Element::attributes or Element::attribute instead.

§first_child_query: Option<QueryId>§next_sibling: Option<ElementId>

Implementations§

Source§

impl<'html> Element<'html>

Source

pub fn iter( &self, arena: &'html Arena<Element<'html>, ElementId>, ) -> impl Iterator<Item = &'html Element<'html>>

Source

pub fn get( &self, dom: &'html Store<'_, '_>, key: &str, ) -> Option<impl Iterator<Item = &'html Element<'html>>>

Look up child elements matched by a nested query (one added via QueryBuilder::then).

The key parameter is the CSS selector string of the child query.

Returns None if this element has no nested query results for the given selector.

Source

pub fn attributes( &self, dom: &'html Store<'_, '_>, ) -> Option<&'html [Attribute<'html>]>

Return all attributes of this element as a slice.

Returns None if the element had no extra attributes beyond class and id (which are stored directly on the Element).

Source

pub fn attribute( &self, dom: &'html Store<'_, '_>, key: &str, ) -> Option<&'html str>

Look up a single attribute value by name.

Returns the attribute’s value, or None if the attribute is not present or has no value.

§Example
use scah::{Query, Save, parse};

let html = r#"<a href="https://example.com">Link</a>"#;
let queries = &[Query::all("a", Save::all()).build()];
let store = parse(html, queries);

let a = store.get("a").unwrap().next().unwrap();
assert_eq!(a.attribute(&store, "href"), Some("https://example.com"));
Source

pub fn text_content(&self, dom: &'html Store<'_, '_>) -> Option<&'html str>

Get the element’s concatenated text content.

Returns the whitespace-trimmed, concatenated text nodes within this element. Only populated when Save::text_content was true.

Trait Implementations§

Source§

impl<'html> Debug for Element<'html>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'html> Default for Element<'html>

Source§

fn default() -> Element<'html>

Returns the “default value” for a type. Read more
Source§

impl<'html> PartialEq for Element<'html>

Source§

fn eq(&self, other: &Element<'html>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'html> StructuralPartialEq for Element<'html>

Auto Trait Implementations§

§

impl<'html> Freeze for Element<'html>

§

impl<'html> RefUnwindSafe for Element<'html>

§

impl<'html> Send for Element<'html>

§

impl<'html> Sync for Element<'html>

§

impl<'html> Unpin for Element<'html>

§

impl<'html> UnsafeUnpin for Element<'html>

§

impl<'html> UnwindSafe for Element<'html>

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, 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.