Skip to content
This repository has been archived by the owner on Mar 2, 2021. It is now read-only.

Commit

Permalink
whtml_stringify (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
sepiropht committed Jul 8, 2019
1 parent fd9383a commit f79215b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
31 changes: 30 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::Render;
use crate::RenderContext;
use crate::{cached_set::CacheId, RootRender, VdomWeak};
use bumpalo::Bump;
use std::fmt;
use std::iter;
use std::mem;
use std::u32;

/// A virtual DOM node.
#[derive(Debug, Clone)]
pub struct Node<'a> {
Expand Down Expand Up @@ -269,3 +270,31 @@ impl Listener<'_> {
}
}
}

pub fn html_string<R>(component: &R) -> String
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
})
}
15 changes: 9 additions & 6 deletions src/render_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,19 @@ impl<'a> RenderContext<'a> {
}
/// return an empty rendering context
pub_unstable_internal! {
pub(crate) fn empty() -> Self {
let cached_set = &RefCell::new(CachedSet::default());
let bump = &Bump::new();
let templates = &mut FxHashMap::default();
RenderContext {
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: (),
}
})
}
}

Expand Down

0 comments on commit f79215b

Please sign in to comment.