1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use super::ElementWithChildren;
use crate::{
    html::element::{CreateElement, ElementType, HtmlElement},
    renderer::{dom::Dom, Renderer},
};
use std::{fmt::Debug, marker::PhantomData};

/// Creates a custom element.
#[track_caller]
pub fn custom<E, Rndr>(tag: E) -> HtmlElement<Custom<E>, (), (), Rndr>
where
    E: AsRef<str>,
    Rndr: Renderer,
{
    HtmlElement {
        tag: Custom(tag),
        rndr: PhantomData,
        attributes: (),
        children: (),
        #[cfg(debug_assertions)]
        defined_at: std::panic::Location::caller(),
    }
}

/// A custom HTML element.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Custom<E>(E);

impl<E> ElementType for Custom<E>
where
    E: AsRef<str> + Send,
{
    type Output = web_sys::HtmlElement;

    const SELF_CLOSING: bool = false;
    const ESCAPE_CHILDREN: bool = true;
    const TAG: &'static str = "";

    fn tag(&self) -> &str {
        self.0.as_ref()
    }
}

impl<E> ElementWithChildren for Custom<E> {}

impl<E> CreateElement<Dom> for Custom<E>
where
    E: AsRef<str>,
{
    fn create_element(&self) -> <Dom as Renderer>::Element {
        use wasm_bindgen::intern;

        crate::dom::document()
            .create_element(intern(self.0.as_ref()))
            .unwrap()
    }
}