fix: custom elements SSR

This commit is contained in:
Greg Johnston 2024-07-13 19:14:18 -04:00
parent 64bc2580ff
commit 7752ab78e3
2 changed files with 12 additions and 49 deletions

View file

@ -3,14 +3,13 @@ use crate::{
html::element::{CreateElement, ElementType, HtmlElement},
renderer::{dom::Dom, Renderer},
};
use std::{borrow::Cow, fmt::Debug, marker::PhantomData, sync::Arc};
use std::{fmt::Debug, marker::PhantomData};
// FIXME custom element HTML rendering is broken because tag names aren't static
/// Creates a custom element.
#[track_caller]
pub fn custom<E, Rndr>(tag: E) -> HtmlElement<Custom<E>, (), (), Rndr>
where
E: CustomElementKey,
E: AsRef<str>,
Rndr: Renderer,
{
HtmlElement {
@ -25,30 +24,28 @@ where
/// A custom HTML element.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Custom<E>(E)
where
E: CustomElementKey;
pub struct Custom<E>(E);
impl<E> ElementType for Custom<E>
where
E: CustomElementKey,
E: AsRef<str> + Send,
{
type Output = web_sys::HtmlElement;
const TAG: &'static str = E::KEY;
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> where E: CustomElementKey {}
impl<E> ElementWithChildren for Custom<E> {}
impl<E> CreateElement<Dom> for Custom<E>
where
E: CustomElementKey,
E: AsRef<str>,
{
fn create_element(&self) -> <Dom as Renderer>::Element {
use wasm_bindgen::intern;
@ -58,37 +55,3 @@ where
.unwrap()
}
}
/// The element name for a custom element.
// TODO these are all broken for custom elements
pub trait CustomElementKey: AsRef<str> + Send {
/// The element name.
const KEY: &'static str;
}
impl<'a> CustomElementKey for &'a str {
const KEY: &'static str = "";
}
impl<'a> CustomElementKey for Cow<'a, str> {
const KEY: &'static str = "";
}
impl CustomElementKey for &String {
const KEY: &'static str = "";
}
impl CustomElementKey for String {
const KEY: &'static str = "";
}
impl CustomElementKey for Arc<str> {
const KEY: &'static str = "";
}
#[cfg(feature = "nightly")]
impl<const K: &'static str> CustomElementKey
for crate::view::static_types::Static<K>
{
const KEY: &'static str = K;
}

View file

@ -273,7 +273,7 @@ where
) {
// opening tag
buf.push('<');
buf.push_str(E::TAG);
buf.push_str(self.tag.tag());
let inner_html = attributes_to_html(self.attributes, buf);
@ -294,7 +294,7 @@ where
// closing tag
buf.push_str("</");
buf.push_str(E::TAG);
buf.push_str(self.tag.tag());
buf.push('>');
}
*position = Position::NextChild;
@ -311,7 +311,7 @@ where
let mut buf = String::with_capacity(Self::MIN_LENGTH);
// opening tag
buf.push('<');
buf.push_str(E::TAG);
buf.push_str(self.tag.tag());
let inner_html = attributes_to_html(self.attributes, &mut buf);
@ -334,7 +334,7 @@ where
// closing tag
let mut buf = String::with_capacity(3 + E::TAG.len());
buf.push_str("</");
buf.push_str(E::TAG);
buf.push_str(self.tag.tag());
buf.push('>');
buffer.push_sync(&buf);
}
@ -349,7 +349,7 @@ where
// non-Static custom elements need special support in templates
// because they haven't been inserted type-wise
if E::TAG.is_empty() && !FROM_SERVER {
todo!()
panic!("Custom elements are not supported in ViewTemplate.");
}
let curr_position = position.get();