lib.rs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #![doc = include_str!("../README.md")]
  2. use std::fmt::{Display, Formatter};
  3. use dioxus_core::IntoVNode;
  4. use dioxus_core::*;
  5. fn app(_cx: Scope) -> Element {
  6. None
  7. }
  8. pub struct SsrRenderer {
  9. vdom: VirtualDom,
  10. cfg: SsrConfig,
  11. }
  12. impl SsrRenderer {
  13. pub fn new(cfg: impl FnOnce(SsrConfig) -> SsrConfig) -> Self {
  14. Self {
  15. cfg: cfg(SsrConfig::default()),
  16. vdom: VirtualDom::new(app),
  17. }
  18. }
  19. pub fn render_lazy<'a>(&'a mut self, f: LazyNodes<'a, '_>) -> String {
  20. let scope = self.vdom.base_scope();
  21. let factory = NodeFactory::new(&scope);
  22. let root = f.into_vnode(factory);
  23. format!(
  24. "{:}",
  25. TextRenderer {
  26. cfg: self.cfg.clone(),
  27. root: &root,
  28. vdom: None
  29. }
  30. )
  31. }
  32. }
  33. pub fn render_lazy<'a>(f: LazyNodes<'a, '_>) -> String {
  34. let vdom = VirtualDom::new(app);
  35. let scope: *const ScopeState = vdom.base_scope();
  36. // Safety
  37. //
  38. // The lifetimes bounds on LazyNodes are really complicated - they need to support the nesting restrictions in
  39. // regular component usage. The <'a> lifetime is used to enforce that all calls of IntoVnode use the same allocator.
  40. //
  41. // When LazyNodes are provided, they are FnOnce, but do not come with a allocator selected to borrow from. The <'a>
  42. // lifetime is therefore longer than the lifetime of the allocator which doesn't exist... yet.
  43. //
  44. // Therefore, we cast our local bump alloactor into right lifetime. This is okay because our usage of the bump arena
  45. // is *definitely* shorter than the <'a> lifetime, and we return *owned* data - not borrowed data.
  46. let scope = unsafe { &*scope };
  47. let root = f.into_vnode(NodeFactory::new(&scope));
  48. format!(
  49. "{:}",
  50. TextRenderer {
  51. cfg: SsrConfig::default(),
  52. root: &root,
  53. vdom: None
  54. }
  55. )
  56. }
  57. pub fn render_vdom(dom: &VirtualDom) -> String {
  58. format!("{:}", TextRenderer::from_vdom(dom, SsrConfig::default()))
  59. }
  60. pub fn render_vdom_cfg(dom: &VirtualDom, cfg: impl FnOnce(SsrConfig) -> SsrConfig) -> String {
  61. format!(
  62. "{:}",
  63. TextRenderer::from_vdom(dom, cfg(SsrConfig::default()))
  64. )
  65. }
  66. pub fn render_vdom_scope(vdom: &VirtualDom, scope: ScopeId) -> Option<String> {
  67. Some(format!(
  68. "{:}",
  69. TextRenderer {
  70. cfg: SsrConfig::default(),
  71. root: vdom.get_scope(scope).unwrap().root_node(),
  72. vdom: Some(vdom)
  73. }
  74. ))
  75. }
  76. /// A configurable text renderer for the Dioxus VirtualDOM.
  77. ///
  78. ///
  79. /// ## Details
  80. ///
  81. /// This uses the `Formatter` infrastructure so you can write into anything that supports `write_fmt`. We can't accept
  82. /// any generic writer, so you need to "Display" the text renderer. This is done through `format!` or `format_args!`
  83. ///
  84. /// ## Example
  85. /// ```ignore
  86. /// static App: Component = |cx| cx.render(rsx!(div { "hello world" }));
  87. /// let mut vdom = VirtualDom::new(App);
  88. /// vdom.rebuild();
  89. ///
  90. /// let renderer = TextRenderer::new(&vdom);
  91. /// let output = format!("{}", renderer);
  92. /// assert_eq!(output, "<div>hello world</div>");
  93. /// ```
  94. pub struct TextRenderer<'a, 'b> {
  95. vdom: Option<&'a VirtualDom>,
  96. root: &'b VNode<'a>,
  97. cfg: SsrConfig,
  98. }
  99. impl Display for TextRenderer<'_, '_> {
  100. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  101. self.html_render(self.root, f, 0)
  102. }
  103. }
  104. impl<'a> TextRenderer<'a, '_> {
  105. pub fn from_vdom(vdom: &'a VirtualDom, cfg: SsrConfig) -> Self {
  106. Self {
  107. cfg,
  108. root: vdom.base_scope().root_node(),
  109. vdom: Some(vdom),
  110. }
  111. }
  112. fn html_render(&self, node: &VNode, f: &mut std::fmt::Formatter, il: u16) -> std::fmt::Result {
  113. match &node {
  114. VNode::Text(text) => {
  115. if self.cfg.indent {
  116. for _ in 0..il {
  117. write!(f, " ")?;
  118. }
  119. }
  120. write!(f, "{}", text.text)?
  121. }
  122. VNode::Placeholder(_anchor) => {
  123. //
  124. if self.cfg.indent {
  125. for _ in 0..il {
  126. write!(f, " ")?;
  127. }
  128. }
  129. write!(f, "<!-- -->")?;
  130. }
  131. VNode::Element(el) => {
  132. if self.cfg.indent {
  133. for _ in 0..il {
  134. write!(f, " ")?;
  135. }
  136. }
  137. write!(f, "<{}", el.tag)?;
  138. let mut inner_html = None;
  139. let mut attr_iter = el.attributes.iter().peekable();
  140. while let Some(attr) = attr_iter.next() {
  141. match attr.namespace {
  142. None => match attr.name {
  143. "dangerous_inner_html" => inner_html = Some(attr.value),
  144. _ => write!(f, " {}=\"{}\"", attr.name, attr.value)?,
  145. },
  146. Some(ns) => {
  147. // write the opening tag
  148. write!(f, " {}=\"", ns)?;
  149. let mut cur_ns_el = attr;
  150. 'ns_parse: loop {
  151. write!(f, "{}:{};", cur_ns_el.name, cur_ns_el.value)?;
  152. match attr_iter.peek() {
  153. Some(next_attr) if next_attr.namespace == Some(ns) => {
  154. cur_ns_el = attr_iter.next().unwrap();
  155. }
  156. _ => break 'ns_parse,
  157. }
  158. }
  159. // write the closing tag
  160. write!(f, "\"")?;
  161. }
  162. }
  163. }
  164. // we write the element's id as a data attribute
  165. //
  166. // when the page is loaded, the `querySelectorAll` will be used to collect all the nodes, and then add
  167. // them interpreter's stack
  168. if let (true, Some(id)) = (self.cfg.pre_render, node.try_mounted_id()) {
  169. write!(f, " dioxus-id=\"{}\"", id)?;
  170. for _listener in el.listeners {
  171. // todo: write the listeners
  172. }
  173. }
  174. match self.cfg.newline {
  175. true => writeln!(f, ">")?,
  176. false => write!(f, ">")?,
  177. }
  178. if let Some(inner_html) = inner_html {
  179. write!(f, "{}", inner_html)?;
  180. } else {
  181. for child in el.children {
  182. self.html_render(child, f, il + 1)?;
  183. }
  184. }
  185. if self.cfg.newline {
  186. writeln!(f)?;
  187. }
  188. if self.cfg.indent {
  189. for _ in 0..il {
  190. write!(f, " ")?;
  191. }
  192. }
  193. write!(f, "</{}>", el.tag)?;
  194. if self.cfg.newline {
  195. writeln!(f)?;
  196. }
  197. }
  198. VNode::Fragment(frag) => {
  199. for child in frag.children {
  200. self.html_render(child, f, il + 1)?;
  201. }
  202. }
  203. VNode::Component(vcomp) => {
  204. let idx = vcomp.scope.get().unwrap();
  205. if let (Some(vdom), false) = (self.vdom, self.cfg.skip_components) {
  206. let new_node = vdom.get_scope(idx).unwrap().root_node();
  207. self.html_render(new_node, f, il + 1)?;
  208. } else {
  209. }
  210. }
  211. }
  212. Ok(())
  213. }
  214. }
  215. #[derive(Clone, Debug, Default)]
  216. pub struct SsrConfig {
  217. /// currently not supported - control if we indent the HTML output
  218. indent: bool,
  219. /// Control if elements are written onto a new line
  220. newline: bool,
  221. /// Choose to write ElementIDs into elements so the page can be re-hydrated later on
  222. pre_render: bool,
  223. // Currently not implemented
  224. // Don't proceed onto new components. Instead, put the name of the component.
  225. // TODO: components don't have names :(
  226. skip_components: bool,
  227. }
  228. impl SsrConfig {
  229. pub fn indent(mut self, a: bool) -> Self {
  230. self.indent = a;
  231. self
  232. }
  233. pub fn newline(mut self, a: bool) -> Self {
  234. self.newline = a;
  235. self
  236. }
  237. pub fn pre_render(mut self, a: bool) -> Self {
  238. self.pre_render = a;
  239. self
  240. }
  241. pub fn skip_components(mut self, a: bool) -> Self {
  242. self.skip_components = a;
  243. self
  244. }
  245. }