mod error;
pub use error::ReplaceHtmlError;
#[cfg(target_arch = "wasm32")]
pub fn replace_body(html: impl Into<String>) -> Result<(), ReplaceHtmlError> {
let body = web_sys::window()
.and_then(|w| w.document())
.and_then(|d| d.body())
.ok_or(ReplaceHtmlError::HeadlessEnvironment)?;
body.set_inner_html(&html.into());
Ok(())
}
#[cfg(target_arch = "wasm32")]
pub fn replace_by_id(
id: impl Into<String>,
html: impl Into<String>,
) -> Result<(), ReplaceHtmlError> {
let id = id.into();
let dom = web_sys::window()
.and_then(|w| w.document())
.ok_or(ReplaceHtmlError::HeadlessEnvironment)?;
let element = dom
.get_element_by_id(&id)
.ok_or(ReplaceHtmlError::ElementNotFound(id))?;
element.set_inner_html(&html.into());
Ok(())
}
#[cfg(not(target_arch = "wasm32"))]
#[allow(unused_variables)]
pub fn replace_by_id(
id: impl Into<String>,
html: impl Into<String>,
) -> Result<(), ReplaceHtmlError> {
Err(ReplaceHtmlError::TargetNotSupported)
}
#[cfg(not(target_arch = "wasm32"))]
#[allow(unused_variables)]
pub fn replace_body(html: impl Into<String>) -> Result<(), ReplaceHtmlError> {
Err(ReplaceHtmlError::TargetNotSupported)
}