dioxus/packages/ssr/src/lib.rs

559 lines
19 KiB
Rust
Raw Normal View History

#![doc = include_str!("../README.md")]
2021-01-21 07:25:44 +00:00
use std::fmt::{Display, Formatter, Write};
2021-01-21 07:25:44 +00:00
use dioxus_core::exports::bumpalo;
2021-11-10 22:09:52 +00:00
use dioxus_core::IntoVNode;
2021-07-13 03:44:20 +00:00
use dioxus_core::*;
2022-01-05 22:30:12 +00:00
fn app(_cx: Scope) -> Element {
None
}
2021-10-18 16:53:56 +00:00
pub struct SsrRenderer {
2022-01-05 22:30:12 +00:00
vdom: VirtualDom,
2021-10-18 21:46:31 +00:00
cfg: SsrConfig,
2021-10-18 16:53:56 +00:00
}
2021-10-18 21:46:31 +00:00
impl SsrRenderer {
pub fn new(cfg: impl FnOnce(SsrConfig) -> SsrConfig) -> Self {
2021-10-18 16:53:56 +00:00
Self {
2021-10-18 21:46:31 +00:00
cfg: cfg(SsrConfig::default()),
2022-01-05 22:30:12 +00:00
vdom: VirtualDom::new(app),
2021-10-18 16:53:56 +00:00
}
}
2021-12-29 04:48:25 +00:00
pub fn render_lazy<'a>(&'a mut self, f: LazyNodes<'a, '_>) -> String {
2022-01-05 22:30:12 +00:00
let scope = self.vdom.base_scope();
2022-01-08 14:32:53 +00:00
let factory = NodeFactory::new(scope);
2021-10-18 16:53:56 +00:00
let root = f.into_vnode(factory);
format!(
"{:}",
TextRenderer {
2021-10-18 21:46:31 +00:00
cfg: self.cfg.clone(),
2021-10-18 16:53:56 +00:00
root: &root,
vdom: Some(&self.vdom),
bump: bumpalo::Bump::new(),
2021-10-18 16:53:56 +00:00
}
)
}
}
2022-01-08 14:32:53 +00:00
#[allow(clippy::needless_lifetimes)]
2021-12-29 04:48:25 +00:00
pub fn render_lazy<'a>(f: LazyNodes<'a, '_>) -> String {
2022-01-05 22:30:12 +00:00
let vdom = VirtualDom::new(app);
let scope: *const ScopeState = vdom.base_scope();
2021-10-18 21:46:31 +00:00
// Safety
//
// The lifetimes bounds on LazyNodes are really complicated - they need to support the nesting restrictions in
// regular component usage. The <'a> lifetime is used to enforce that all calls of IntoVnode use the same allocator.
//
// When LazyNodes are provided, they are FnOnce, but do not come with a allocator selected to borrow from. The <'a>
// lifetime is therefore longer than the lifetime of the allocator which doesn't exist... yet.
2021-10-18 21:46:31 +00:00
//
2022-01-08 14:32:53 +00:00
// Therefore, we cast our local bump allocator to the right lifetime. This is okay because our usage of the bump
// arena is *definitely* shorter than the <'a> lifetime, and we return *owned* data - not borrowed data.
2022-01-05 22:30:12 +00:00
let scope = unsafe { &*scope };
2021-10-18 21:46:31 +00:00
2022-01-08 14:32:53 +00:00
let root = f.into_vnode(NodeFactory::new(scope));
2021-10-18 21:46:31 +00:00
let vdom = Some(&vdom);
let ssr_renderer = TextRenderer {
cfg: SsrConfig::default(),
root: &root,
vdom,
bump: bumpalo::Bump::new(),
};
let r = ssr_renderer.to_string();
drop(ssr_renderer);
r
2021-10-18 21:46:31 +00:00
}
2021-07-18 07:54:42 +00:00
pub fn render_vdom(dom: &VirtualDom) -> String {
format!("{:}", TextRenderer::from_vdom(dom, SsrConfig::default()))
}
2022-01-07 05:33:09 +00:00
pub fn pre_render_vdom(dom: &VirtualDom) -> String {
format!(
"{:}",
TextRenderer::from_vdom(dom, SsrConfig::default().pre_render(true))
)
}
pub fn render_vdom_cfg(dom: &VirtualDom, cfg: impl FnOnce(SsrConfig) -> SsrConfig) -> String {
format!(
"{:}",
TextRenderer::from_vdom(dom, cfg(SsrConfig::default()))
)
2021-07-18 07:54:42 +00:00
}
pub fn render_vdom_scope(vdom: &VirtualDom, scope: ScopeId) -> Option<String> {
Some(format!(
"{:}",
TextRenderer {
cfg: SsrConfig::default(),
2021-12-15 03:48:20 +00:00
root: vdom.get_scope(scope).unwrap().root_node(),
vdom: Some(vdom),
bump: bumpalo::Bump::new()
2021-07-18 07:54:42 +00:00
}
))
}
/// A configurable text renderer for the Dioxus VirtualDOM.
///
2021-01-21 07:25:44 +00:00
///
/// ## Details
2021-01-21 07:25:44 +00:00
///
/// This uses the `Formatter` infrastructure so you can write into anything that supports `write_fmt`. We can't accept
/// any generic writer, so you need to "Display" the text renderer. This is done through `format!` or `format_args!`
2021-01-21 07:25:44 +00:00
///
2021-07-11 19:17:55 +00:00
/// ## Example
/// ```ignore
2021-12-29 04:48:25 +00:00
/// static App: Component = |cx| cx.render(rsx!(div { "hello world" }));
2021-07-11 19:17:55 +00:00
/// let mut vdom = VirtualDom::new(App);
/// vdom.rebuild();
///
2021-07-11 19:17:55 +00:00
/// let renderer = TextRenderer::new(&vdom);
/// let output = format!("{}", renderer);
/// assert_eq!(output, "<div>hello world</div>");
/// ```
pub struct TextRenderer<'a, 'b, 'c> {
vdom: Option<&'c VirtualDom>,
2021-10-18 16:53:56 +00:00
root: &'b VNode<'a>,
2021-07-11 19:17:55 +00:00
cfg: SsrConfig,
bump: bumpalo::Bump,
2021-01-21 07:25:44 +00:00
}
impl<'a: 'c, 'c> Display for TextRenderer<'a, '_, 'c> {
2021-07-18 07:54:42 +00:00
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
2022-01-07 05:33:09 +00:00
let mut last_node_was_text = false;
self.html_render(self.root, f, 0, &mut last_node_was_text)
2021-07-18 07:54:42 +00:00
}
}
impl<'a> TextRenderer<'a, '_, 'a> {
pub fn from_vdom(vdom: &'a VirtualDom, cfg: SsrConfig) -> Self {
2021-07-11 19:17:55 +00:00
Self {
cfg,
root: vdom.base_scope().root_node(),
2021-07-18 07:54:42 +00:00
vdom: Some(vdom),
bump: bumpalo::Bump::new(),
2021-07-11 19:17:55 +00:00
}
2021-01-21 07:25:44 +00:00
}
}
2021-01-21 07:25:44 +00:00
impl<'a: 'c, 'c> TextRenderer<'a, '_, 'c> {
2022-01-07 05:33:09 +00:00
fn html_render(
&self,
node: &VNode,
f: &mut impl Write,
2022-01-07 05:33:09 +00:00
il: u16,
last_node_was_text: &mut bool,
) -> std::fmt::Result {
match &node {
2021-08-20 14:34:41 +00:00
VNode::Text(text) => {
if *last_node_was_text {
2022-01-07 05:33:09 +00:00
write!(f, "<!--spacer-->")?;
}
2021-07-11 23:31:07 +00:00
if self.cfg.indent {
for _ in 0..il {
write!(f, " ")?;
}
}
2022-01-07 05:33:09 +00:00
*last_node_was_text = true;
2021-07-11 23:31:07 +00:00
write!(f, "{}", text.text)?
}
2021-11-23 20:53:57 +00:00
VNode::Placeholder(_anchor) => {
2022-01-07 05:36:13 +00:00
*last_node_was_text = false;
2021-07-30 21:04:04 +00:00
if self.cfg.indent {
for _ in 0..il {
write!(f, " ")?;
}
}
write!(f, "<!--placeholder-->")?;
2021-07-30 21:04:04 +00:00
}
2021-08-20 14:34:41 +00:00
VNode::Element(el) => {
2022-01-07 05:36:13 +00:00
*last_node_was_text = false;
2021-07-11 23:31:07 +00:00
if self.cfg.indent {
for _ in 0..il {
write!(f, " ")?;
}
}
2021-12-21 05:46:10 +00:00
write!(f, "<{}", el.tag)?;
2021-10-18 22:07:19 +00:00
let inner_html = render_attributes(el.attributes.iter(), f)?;
match self.cfg.newline {
true => writeln!(f, ">")?,
false => write!(f, ">")?,
}
if let Some(inner_html) = inner_html {
write!(f, "{}", inner_html)?;
} else {
let mut last_node_was_text = false;
for child in el.children {
self.html_render(child, f, il + 1, &mut last_node_was_text)?;
}
}
if self.cfg.newline {
writeln!(f)?;
}
if self.cfg.indent {
for _ in 0..il {
write!(f, " ")?;
}
}
write!(f, "</{}>", el.tag)?;
if self.cfg.newline {
writeln!(f)?;
}
}
VNode::Fragment(frag) => {
for child in frag.children {
self.html_render(child, f, il + 1, last_node_was_text)?;
}
}
VNode::Component(vcomp) => {
let idx = vcomp.scope.get().unwrap();
if let (Some(vdom), false) = (self.vdom, self.cfg.skip_components) {
let new_node = vdom.get_scope(idx).unwrap().root_node();
self.html_render(new_node, f, il + 1, last_node_was_text)?;
} else {
}
}
VNode::TemplateRef(tmpl) => {
if let Some(vdom) = self.vdom {
let template_id = &tmpl.template_id;
let dynamic_context = &tmpl.dynamic_context;
vdom.with_template(template_id, move |tmpl| {
match tmpl {
Template::Static(s) => {
for r in s.root_nodes {
self.render_template_node(
&s.nodes,
&s.nodes[r.0],
dynamic_context,
f,
last_node_was_text,
il,
)?;
}
}
Template::Owned(o) => {
for r in &o.root_nodes {
self.render_template_node(
&o.nodes,
&o.nodes[r.0],
dynamic_context,
f,
last_node_was_text,
il,
)?;
}
}
};
Ok(())
})?
} else {
panic!("Cannot render template without vdom");
}
}
}
Ok(())
}
2022-10-02 21:12:24 +00:00
fn render_template_node<TemplateNodes, Attributes, V, Children, Listeners, TextSegments, Text>(
&self,
template_nodes: &TemplateNodes,
node: &TemplateNode<Attributes, V, Children, Listeners, TextSegments, Text>,
dynamic_context: &TemplateContext,
f: &mut impl Write,
last_node_was_text: &mut bool,
il: u16,
) -> std::fmt::Result
where
TemplateNodes:
AsRef<[TemplateNode<Attributes, V, Children, Listeners, TextSegments, Text>]>,
Attributes: AsRef<[TemplateAttribute<V>]>,
Children: AsRef<[TemplateNodeId]>,
Listeners: AsRef<[usize]>,
Text: AsRef<str>,
TextSegments: AsRef<[TextTemplateSegment<Text>]>,
V: TemplateValue,
{
match &node.node_type {
TemplateNodeType::Element(el) => {
*last_node_was_text = false;
if self.cfg.indent {
for _ in 0..il {
write!(f, " ")?;
}
}
write!(f, "<{}", el.tag)?;
2021-10-18 22:07:19 +00:00
let mut inner_html = None;
2022-10-02 21:12:24 +00:00
let mut attr_iter = el.attributes.as_ref().iter().peekable();
2021-07-15 15:06:52 +00:00
while let Some(attr) = attr_iter.next() {
match attr.attribute.namespace {
None => {
if attr.attribute.name == "dangerous_inner_html" {
inner_html = {
let text = match &attr.value {
TemplateAttributeValue::Static(val) => {
val.allocate(&self.bump).as_text().unwrap()
}
TemplateAttributeValue::Dynamic(idx) => dynamic_context
.resolve_attribute(*idx)
.as_text()
.unwrap(),
};
Some(text)
}
} else if is_boolean_attribute(attr.attribute.name) {
match &attr.value {
TemplateAttributeValue::Static(val) => {
let val = val.allocate(&self.bump);
if val.is_truthy() {
write!(f, " {}=\"{}\"", attr.attribute.name, val)?
}
}
TemplateAttributeValue::Dynamic(idx) => {
let val = dynamic_context.resolve_attribute(*idx);
if val.is_truthy() {
write!(f, " {}=\"{}\"", attr.attribute.name, val)?
}
}
}
} else {
match &attr.value {
TemplateAttributeValue::Static(val) => {
let val = val.allocate(&self.bump);
write!(f, " {}=\"{}\"", attr.attribute.name, val)?
}
TemplateAttributeValue::Dynamic(idx) => {
let val = dynamic_context.resolve_attribute(*idx);
write!(f, " {}=\"{}\"", attr.attribute.name, val)?
}
2022-01-10 06:32:32 +00:00
}
}
}
2021-07-15 15:06:52 +00:00
Some(ns) => {
// write the opening tag
write!(f, " {}=\"", ns)?;
let mut cur_ns_el = attr;
loop {
match &attr.value {
TemplateAttributeValue::Static(val) => {
let val = val.allocate(&self.bump);
write!(f, "{}:{};", cur_ns_el.attribute.name, val)?;
}
TemplateAttributeValue::Dynamic(idx) => {
let val = dynamic_context.resolve_attribute(*idx);
write!(f, "{}:{};", cur_ns_el.attribute.name, val)?;
}
}
2021-07-15 15:06:52 +00:00
match attr_iter.peek() {
Some(next_attr)
if next_attr.attribute.namespace == Some(ns) =>
{
2021-07-15 15:06:52 +00:00
cur_ns_el = attr_iter.next().unwrap();
}
_ => break,
2021-07-15 15:06:52 +00:00
}
}
// write the closing tag
write!(f, "\"")?;
}
}
}
2021-07-15 15:06:52 +00:00
2021-07-11 19:17:55 +00:00
match self.cfg.newline {
2021-10-18 21:46:31 +00:00
true => writeln!(f, ">")?,
2021-07-11 19:17:55 +00:00
false => write!(f, ">")?,
}
2021-10-18 22:07:19 +00:00
if let Some(inner_html) = inner_html {
write!(f, "{}", inner_html)?;
} else {
2022-01-07 05:33:09 +00:00
let mut last_node_was_text = false;
for child in el.children.as_ref() {
self.render_template_node(
template_nodes,
&template_nodes.as_ref()[child.0],
dynamic_context,
f,
&mut last_node_was_text,
il + 1,
)?;
2021-10-18 22:07:19 +00:00
}
}
2021-07-11 23:31:07 +00:00
if self.cfg.newline {
2021-10-18 21:46:31 +00:00
writeln!(f)?;
2021-07-11 23:31:07 +00:00
}
if self.cfg.indent {
for _ in 0..il {
write!(f, " ")?;
}
}
2021-12-21 05:46:10 +00:00
write!(f, "</{}>", el.tag)?;
2021-07-11 23:31:07 +00:00
if self.cfg.newline {
2021-10-18 21:46:31 +00:00
writeln!(f)?;
2021-07-11 19:17:55 +00:00
}
}
TemplateNodeType::Text(txt) => {
if *last_node_was_text {
write!(f, "<!--spacer-->")?;
}
if self.cfg.indent {
for _ in 0..il {
write!(f, " ")?;
}
2021-07-11 19:17:55 +00:00
}
*last_node_was_text = true;
let text = dynamic_context.resolve_text(txt);
write!(f, "{}", text)?
}
TemplateNodeType::DynamicNode(idx) => {
let node = dynamic_context.resolve_node(*idx);
self.html_render(node, f, il, last_node_was_text)?;
}
}
Ok(())
}
}
2021-10-18 21:46:31 +00:00
fn render_attributes<'a, 'b: 'a, I>(
attrs: I,
f: &mut impl Write,
) -> Result<Option<&'b str>, std::fmt::Error>
where
I: Iterator<Item = &'a Attribute<'b>>,
{
let mut inner_html = None;
let mut attr_iter = attrs.peekable();
while let Some(attr) = attr_iter.next() {
match attr.attribute.namespace {
None => {
if attr.attribute.name == "dangerous_inner_html" {
inner_html = Some(attr.value.as_text().unwrap())
2021-10-18 21:46:31 +00:00
} else {
2022-10-02 21:12:24 +00:00
if is_boolean_attribute(attr.attribute.name) && !attr.value.is_truthy() {
continue;
}
write!(f, " {}=\"{}\"", attr.attribute.name, attr.value)?
2021-07-18 07:54:42 +00:00
}
}
Some(ns) => {
// write the opening tag
write!(f, " {}=\"", ns)?;
let mut cur_ns_el = attr;
loop {
write!(f, "{}:{};", cur_ns_el.attribute.name, cur_ns_el.value)?;
match attr_iter.peek() {
Some(next_attr) if next_attr.attribute.namespace == Some(ns) => {
cur_ns_el = attr_iter.next().unwrap();
}
_ => break,
}
}
// write the closing tag
write!(f, "\"")?;
}
}
}
Ok(inner_html)
}
fn is_boolean_attribute(attribute: &'static str) -> bool {
2022-10-02 21:12:24 +00:00
matches!(
attribute,
"allowfullscreen"
| "allowpaymentrequest"
| "async"
| "autofocus"
| "autoplay"
| "checked"
| "controls"
| "default"
| "defer"
| "disabled"
| "formnovalidate"
| "hidden"
| "ismap"
| "itemscope"
| "loop"
| "multiple"
| "muted"
| "nomodule"
| "novalidate"
| "open"
| "playsinline"
| "readonly"
| "required"
| "reversed"
| "selected"
| "truespeed"
)
}
2021-01-21 07:25:44 +00:00
2021-10-18 21:46:31 +00:00
#[derive(Clone, Debug, Default)]
pub struct SsrConfig {
2021-09-25 02:15:50 +00:00
/// currently not supported - control if we indent the HTML output
indent: bool,
2021-09-25 02:15:50 +00:00
/// Control if elements are written onto a new line
newline: bool,
2021-09-25 02:15:50 +00:00
/// Choose to write ElementIDs into elements so the page can be re-hydrated later on
pre_render: bool,
// Currently not implemented
// Don't proceed onto new components. Instead, put the name of the component.
// TODO: components don't have names :(
skip_components: bool,
}
impl SsrConfig {
pub fn indent(mut self, a: bool) -> Self {
self.indent = a;
self
}
pub fn newline(mut self, a: bool) -> Self {
self.newline = a;
self
}
pub fn pre_render(mut self, a: bool) -> Self {
self.pre_render = a;
self
}
pub fn skip_components(mut self, a: bool) -> Self {
self.skip_components = a;
self
}
}