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
| Data | How to access |
|---|---|
| Tag name | element.name |
| Class | element.class |
| ID | element.id |
| Inner HTML | element.inner_html |
| Text content | element.text_content(&store) |
| All attributes | element.attributes(&store) |
| Single attribute | element.attribute(&store, "href") |
| Child query results | element.get(&store, "selector") |
Fields§
§name: &'html strThe 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>
impl<'html> Element<'html>
pub fn iter( &self, arena: &'html Arena<Element<'html>, ElementId>, ) -> impl Iterator<Item = &'html Element<'html>>
Sourcepub fn get(
&self,
dom: &'html Store<'_, '_>,
key: &str,
) -> Option<impl Iterator<Item = &'html Element<'html>>>
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.
Sourcepub fn attributes(
&self,
dom: &'html Store<'_, '_>,
) -> Option<&'html [Attribute<'html>]>
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).
Sourcepub fn attribute(
&self,
dom: &'html Store<'_, '_>,
key: &str,
) -> Option<&'html str>
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"));Sourcepub fn text_content(&self, dom: &'html Store<'_, '_>) -> Option<&'html str>
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.