wext 0.7.2

web-sys extension traits, convenience functions and types
Documentation
pub trait NodeExt: AsRef<web_sys::Node> + Clone {
    /// shorthand for `self.append_child(&child).unwrap()`
    fn child(&self, child: impl AsRef<web_sys::Node>) -> Self {
        self.as_ref().append_child(child.as_ref()).unwrap();
        self.clone()
    }
    /// sets
    fn txt<'a>(&self, text: impl Into<Option<&'a str>>) -> Self {
        self.as_ref().set_text_content(text.into());
        self.clone()
    }
    /// removes all child nodes
    fn clear(&self) {
        let node = self.as_ref();
        while let Some(child) = node.last_child() {
            node.remove_child(&child).unwrap();
        }
    }
    /// shorthand for `self.parent_node().unwrap().replace_child(&new, self).unwrap()`
    fn replace(&self, new: impl AsRef<web_sys::Node>) {
        self.as_ref().parent_node().unwrap().replace_child(new.as_ref(), self.as_ref()).unwrap();
    }
    /// like [Self::replace], but also overwrites `self` with the new element and returns the original `self`.
    fn overwrite(&mut self, new: Self) -> Self
    where
        Self: Sized,
    {
        self.replace(new.as_ref());
        std::mem::replace(self, new)
    }
}

impl<T: AsRef<web_sys::Node> + Clone> NodeExt for T {}

#[cfg(test)]
pub mod tests {
    use crate::prelude::*;
    use wasm_bindgen_test::*;
    use web_sys::{HtmlButtonElement, HtmlDivElement};

    #[wasm_bindgen_test]
    fn test_element() {
        let b = HtmlButtonElement::create_button();
        HtmlDivElement::create_div().child(b);
    }
}