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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::borrow::Cow;
use web_sys::Node;
use yew::virtual_dom::VNode;
use yew::{prelude::*, Component, ComponentLink, Html, ShouldRender};
use crate::{Htmlify, Attribute};
#[derive(Debug, Clone, Eq, PartialEq, Properties)]
pub struct Props
{
pub tag: &'static str,
pub attributes: Vec<Attribute>,
pub html: Cow<'static, str>
}
pub struct RawHtml
{
props: Props,
}
impl RawHtml
{
pub fn from<T: Htmlify>(t: &T) -> Html
{
html!
{
<RawHtml tag=T::TAG attributes={t.attributes()} html={t.inner_html()} />
}
}
}
impl Component for RawHtml
{
type Message = ();
type Properties = Props;
fn create(props: Self::Properties, _: ComponentLink<Self>) -> Self
{
Self { props }
}
fn update(&mut self, _: Self::Message) -> ShouldRender
{
unreachable!()
}
fn change(&mut self, props: Self::Properties) -> ShouldRender
{
if self.props != props
{
self.props = props;
true
}
else
{
false
}
}
fn view(&self) -> Html
{
VNode::VRef(Node::from
({
let element = web_sys::window().unwrap().document().unwrap().create_element(self.props.tag).unwrap();
for attr in self.props.attributes.iter()
{
element.set_attribute(&attr.name, &attr.value).unwrap();
}
element.set_inner_html(&self.props.html);
element
}))
}
}