Trait lol_html::html_content::UserData
source · pub trait UserData {
// Required methods
fn user_data(&self) -> &dyn Any;
fn user_data_mut(&mut self) -> &mut dyn Any;
fn set_user_data(&mut self, data: impl Any);
}
Expand description
Data that can be attached to a rewritable unit by a user and shared between content handler invocations.
Same rewritable units can be passed to different content handlers if all of them capture the
unit. UserData
trait provides capability to attach arbitrary data to a rewritable unit, so
handlers can make decision on how to process the unit based on the information provided by
previous handlers.
Example
use lol_html::{rewrite_str, element, RewriteStrSettings};
use lol_html::html_content::UserData;
rewrite_str(
r#"<div id="foo"></div>"#,
RewriteStrSettings {
element_content_handlers: vec![
element!("*", |el| {
el.set_user_data("Captured by `*`");
Ok(())
}),
element!("#foo", |el| {
let user_data = el.user_data_mut().downcast_mut::<&'static str>().unwrap();
assert_eq!(*user_data, "Captured by `*`");
*user_data = "Captured by `#foo`";
Ok(())
}),
element!("div", |el| {
let user_data = el.user_data().downcast_ref::<&'static str>().unwrap();
assert_eq!(*user_data, "Captured by `#foo`");
Ok(())
})
],
..RewriteStrSettings::default()
}
).unwrap();
Required Methods§
sourcefn user_data_mut(&mut self) -> &mut dyn Any
fn user_data_mut(&mut self) -> &mut dyn Any
Returns a mutable reference to the attached user data.
sourcefn set_user_data(&mut self, data: impl Any)
fn set_user_data(&mut self, data: impl Any)
Attaches user data to a rewritable unit.