Skip to content
This repository was archived by the owner on Mar 2, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::Render;
use crate::RenderContext;
use crate::{cached_set::CacheId, RootRender, VdomWeak};
use bumpalo::Bump;
use std::fmt;
Expand Down Expand Up @@ -269,3 +271,32 @@ impl Listener<'_> {
}
}
}

pub fn html_string<R>(component: &R) -> String
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: this function needs documentation and an example of using it.

where
R: Render,
{
fn html_string_recursive(cx: &mut RenderContext, s: &mut String, node: &Node) {
match node.kind {
NodeKind::Text(ref t) => s.push_str(t.text),
NodeKind::Element(ref e) => {
s.push_str(e.tag_name);
for c in e.children {
html_string_recursive(cx, s, c);
}

s.push_str(&format!("</{}>", e.tag_name));
}
NodeKind::Cached(ref c) => {
html_string_recursive(cx, s, cx.cached_set.borrow().get(c.id).0);
}
}
}

RenderContext::empty(|cx| {
let node = component.render(cx);
let mut s = String::new();
html_string_recursive(cx, &mut s, &node);
s
})
}
18 changes: 18 additions & 0 deletions src/render_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
};
use bumpalo::Bump;
use fxhash::FxHashMap;
use std::cell::RefCell;
use std::fmt;

/// Common context available to all `Render` implementations.
Expand Down Expand Up @@ -58,6 +59,23 @@ impl<'a> RenderContext<'a> {
}
}
}
/// return an empty rendering context
pub_unstable_internal! {
pub(crate) fn empty<F, T>(f: F) -> T
where F: FnOnce(&mut RenderContext) -> T,
{
let cached_set = &RefCell::new(CachedSet::default());
let bump = &Bump::new();
let templates = &mut FxHashMap::default();

f(&mut RenderContext {
bump,
cached_set,
templates,
_non_exhaustive: (),
})
}
}

pub(crate) fn cache<F>(&mut self, pinned: bool, template: Option<CacheId>, f: F) -> CacheId
where
Expand Down
15 changes: 13 additions & 2 deletions tests/web/render.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{assert_rendered, before_after, create_element, RenderFn};
use super::{assert_rendered, before_after, create_element, RenderFn, html_string};
use dodrio::{builder::*, Vdom};
use futures::prelude::*;
use std::rc::Rc;
Expand All @@ -10,7 +10,18 @@ fn render_initial_text() {

let container = create_element("div");
let _vdom = Vdom::new(&container, hello.clone());
assert_rendered(&container, &hello);
assert_rendered(container, &hello);
}

fn render_to_string() {
let hello = Rc::new(RenderFn(|cx| {
div(&cx)
.attr("id", "hello-world")
.children([text("Hello "), span(&cx).child(text("World!")).finish()])
.finish()
}));
let string_html = html_string(&hello);
assert!(&string_html, "<div id=hello_world> Hello <span>World!</span></div>");
}

#[wasm_bindgen_test]
Expand Down