web_sys_query/query/
manipulation.rs

1//! Manipulation of the DOM
2
3use crate::{
4    error::Error,
5    query::{Collection, Element},
6};
7
8/// DOM manipulation
9impl Element {
10    /// Get the inner text.
11    pub fn text(&self) -> Result<String, Error> {
12        Ok(self.dyn_ref::<web_sys::HtmlElement>()?.inner_text())
13    }
14
15    /// Set the inner text.
16    pub fn set_text(&self, text: &str) -> Result<(), Error> {
17        self.dyn_ref::<web_sys::HtmlElement>()?.set_inner_text(text);
18        Ok(())
19    }
20}
21
22/// DOM manipulation
23impl Collection {
24    pub fn text(&self) -> Vec<String> {
25        self.0.iter().filter_map(|elem| elem.text().ok()).collect()
26    }
27
28    pub fn set_text(&self, text: &str) {
29        self.0.iter().for_each(|elem| {
30            elem.set_text(text).ok();
31        })
32    }
33}