use std::borrow::Cow;
use crate::dom::Text;
use crate::prelude::*;
#[derive(Debug)]
pub struct Element {
kind: Cow<'static, str>,
el: web_sys::Element,
}
impl Element {
pub fn new<S>(kind: S) -> Self
where
S: Into<Cow<'static, str>>,
{
let kind = kind.into();
let el = crate::utils::document()
.create_element(&kind)
.unwrap_throw();
Self { kind, el }
}
pub fn with_text<S>(kind: S, text: &str) -> Self
where
S: Into<Cow<'static, str>>,
{
let kind = kind.into();
let el = crate::utils::document()
.create_element(&kind)
.unwrap_throw();
let this = Self { kind, el };
this.append(Text::new(text));
this
}
pub unsafe fn from_raw<S>(kind: S, el: web_sys::Element) -> Self
where
S: Into<Cow<'static, str>>,
{
let kind = kind.into();
Self { el, kind }
}
pub fn append<C>(&self, child: C)
where
C: AsRef<web_sys::Node>,
{
self.el.append_child(child.as_ref()).unwrap_throw();
}
pub fn attr(&self, name: &str) -> Option<String> {
self.el.get_attribute(name)
}
pub fn set_attr(&self, name: &str, value: &str) {
self.el.set_attribute(name, value).unwrap_throw();
}
pub fn query_selector(&self, selectors: &str) -> Option<Element> {
self.el
.query_selector(selectors)
.unwrap_throw()
.map(|el| unsafe { Element::from_raw(el.tag_name(), el) })
}
pub fn text(&self) -> Option<String> {
self.el.text_content()
}
pub fn set_text(&self, value: &str) {
self.el.set_text_content(Some(value));
}
pub fn clear_text(&self) {
self.el.set_text_content(None);
}
}
impl AsRef<web_sys::Node> for Element {
fn as_ref(&self) -> &web_sys::Node {
self.el.as_ref()
}
}
impl AsRef<web_sys::EventTarget> for Element {
fn as_ref(&self) -> &web_sys::EventTarget {
self.el.as_ref()
}
}