123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- #![doc = include_str!("../README.md")]
- use std::fmt::{Display, Formatter, Write};
- use dioxus_core::exports::bumpalo;
- use dioxus_core::IntoVNode;
- use dioxus_core::*;
- fn app(_cx: Scope) -> Element {
- None
- }
- pub struct SsrRenderer {
- vdom: VirtualDom,
- cfg: SsrConfig,
- }
- impl SsrRenderer {
- pub fn new(cfg: impl FnOnce(SsrConfig) -> SsrConfig) -> Self {
- Self {
- cfg: cfg(SsrConfig::default()),
- vdom: VirtualDom::new(app),
- }
- }
- pub fn render_lazy<'a>(&'a mut self, f: LazyNodes<'a, '_>) -> String {
- let scope = self.vdom.base_scope();
- let factory = NodeFactory::new(scope);
- let root = f.into_vnode(factory);
- format!(
- "{:}",
- TextRenderer {
- cfg: self.cfg.clone(),
- root: &root,
- vdom: Some(&self.vdom),
- bump: bumpalo::Bump::new(),
- }
- )
- }
- }
- #[allow(clippy::needless_lifetimes)]
- pub fn render_lazy<'a>(f: LazyNodes<'a, '_>) -> String {
- let vdom = VirtualDom::new(app);
- let scope: *const ScopeState = vdom.base_scope();
- // 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.
- //
- // 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.
- let scope = unsafe { &*scope };
- let root = f.into_vnode(NodeFactory::new(scope));
- 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);
- drop(vdom);
- r
- }
- pub fn render_vdom(dom: &VirtualDom) -> String {
- format!("{:}", TextRenderer::from_vdom(dom, SsrConfig::default()))
- }
- 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()))
- )
- }
- pub fn render_vdom_scope(vdom: &VirtualDom, scope: ScopeId) -> Option<String> {
- Some(format!(
- "{:}",
- TextRenderer {
- cfg: SsrConfig::default(),
- root: vdom.get_scope(scope).unwrap().root_node(),
- vdom: Some(vdom),
- bump: bumpalo::Bump::new()
- }
- ))
- }
- /// A configurable text renderer for the Dioxus VirtualDOM.
- ///
- ///
- /// ## Details
- ///
- /// 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!`
- ///
- /// ## Example
- /// ```ignore
- /// static App: Component = |cx| cx.render(rsx!(div { "hello world" }));
- /// let mut vdom = VirtualDom::new(App);
- /// vdom.rebuild();
- ///
- /// 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>,
- root: &'b VNode<'a>,
- cfg: SsrConfig,
- bump: bumpalo::Bump,
- }
- impl<'a: 'c, 'c> Display for TextRenderer<'a, '_, 'c> {
- fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
- let mut last_node_was_text = false;
- self.html_render(self.root, f, 0, &mut last_node_was_text)
- }
- }
- impl<'a> TextRenderer<'a, '_, 'a> {
- pub fn from_vdom(vdom: &'a VirtualDom, cfg: SsrConfig) -> Self {
- Self {
- cfg,
- root: vdom.base_scope().root_node(),
- vdom: Some(vdom),
- bump: bumpalo::Bump::new(),
- }
- }
- }
- impl<'a: 'c, 'c> TextRenderer<'a, '_, 'c> {
- fn html_render(
- &self,
- node: &VNode,
- f: &mut impl Write,
- il: u16,
- last_node_was_text: &mut bool,
- ) -> std::fmt::Result {
- match &node {
- VNode::Text(text) => {
- if *last_node_was_text {
- write!(f, "<!--spacer-->")?;
- }
- if self.cfg.indent {
- for _ in 0..il {
- write!(f, " ")?;
- }
- }
- *last_node_was_text = true;
- write!(f, "{}", text.text)?
- }
- VNode::Placeholder(_anchor) => {
- *last_node_was_text = false;
- if self.cfg.indent {
- for _ in 0..il {
- write!(f, " ")?;
- }
- }
- write!(f, "<!--placeholder-->")?;
- }
- VNode::Element(el) => {
- *last_node_was_text = false;
- if self.cfg.indent {
- for _ in 0..il {
- write!(f, " ")?;
- }
- }
- write!(f, "<{}", el.tag)?;
- 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,
- &s.dynamic_mapping,
- 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,
- &o.dynamic_mapping,
- f,
- last_node_was_text,
- il,
- )?;
- }
- }
- };
- Ok(())
- })?
- } else {
- panic!("Cannot render template without vdom");
- }
- }
- }
- Ok(())
- }
- fn render_template_node<
- TemplateNodes,
- Attributes,
- V,
- Children,
- Listeners,
- TextSegments,
- Text,
- Nodes,
- TextOuter,
- TextInner,
- AttributesOuter,
- AttributesInner,
- Volatile,
- Listeners2,
- >(
- &self,
- template_nodes: &TemplateNodes,
- node: &TemplateNode<Attributes, V, Children, Listeners, TextSegments, Text>,
- dynamic_context: &TemplateContext,
- dynamic_node_mapping: &DynamicNodeMapping<
- Nodes,
- TextOuter,
- TextInner,
- AttributesOuter,
- AttributesInner,
- Volatile,
- Listeners2,
- >,
- 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>]>,
- AttributesInner: AsRef<[(TemplateNodeId, usize)]>,
- AttributesOuter: AsRef<[AttributesInner]>,
- Children: AsRef<[TemplateNodeId]>,
- Listeners: AsRef<[usize]>,
- Listeners2: AsRef<[TemplateNodeId]>,
- Nodes: AsRef<[Option<TemplateNodeId>]>,
- Text: AsRef<str>,
- TextInner: AsRef<[TemplateNodeId]>,
- TextOuter: AsRef<[TextInner]>,
- TextSegments: AsRef<[TextTemplateSegment<Text>]>,
- V: TemplateValue,
- Volatile: AsRef<[(TemplateNodeId, usize)]>,
- {
- 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)?;
- let mut inner_html = None;
- let mut attr_iter = el.attributes.as_ref().into_iter().peekable();
- 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)?
- }
- }
- }
- }
- 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)?;
- }
- }
- 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, "\"")?;
- }
- }
- }
- 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.as_ref() {
- self.render_template_node(
- template_nodes,
- &template_nodes.as_ref()[child.0],
- dynamic_context,
- dynamic_node_mapping,
- f,
- &mut last_node_was_text,
- il + 1,
- )?;
- }
- }
- 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)?;
- }
- }
- TemplateNodeType::Text(txt) => {
- if *last_node_was_text {
- write!(f, "<!--spacer-->")?;
- }
- if self.cfg.indent {
- for _ in 0..il {
- write!(f, " ")?;
- }
- }
- *last_node_was_text = true;
- let text = dynamic_context.resolve_text(&txt.segments);
- write!(f, "{}", text)?
- }
- TemplateNodeType::DynamicNode(idx) => {
- let node = dynamic_context.resolve_node(*idx);
- self.html_render(node, f, il, last_node_was_text)?;
- }
- }
- Ok(())
- }
- }
- 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())
- } else {
- if is_boolean_attribute(attr.attribute.name) {
- if !attr.value.is_truthy() {
- continue;
- }
- }
- write!(f, " {}=\"{}\"", attr.attribute.name, attr.value)?
- }
- }
- 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 {
- if let "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" = attribute
- {
- true
- } else {
- false
- }
- }
- #[derive(Clone, Debug, Default)]
- pub struct SsrConfig {
- /// currently not supported - control if we indent the HTML output
- indent: bool,
- /// Control if elements are written onto a new line
- newline: bool,
- /// 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
- }
- }
|