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

Serialize to HTML #82

Open
olanod opened this issue May 29, 2019 · 4 comments
Open

Serialize to HTML #82

olanod opened this issue May 29, 2019 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@olanod
Copy link

olanod commented May 29, 2019

Would it be possible to serialize a tree as an html string or some mechanism to create a dodrio dom in server side and serve it via http?

@fitzgen fitzgen added enhancement New feature or request help wanted Extra attention is needed labels Jun 3, 2019
@fitzgen
Copy link
Owner

fitzgen commented Jun 3, 2019

Yep! I've been intending to support this sometime in the future.

The first step would be to add a to_html_string method on Node:

impl<'a> Node<'a> {
    fn to_html_string(&self) -> String { ... }
}

And the second step would be a way to create an empty RenderContext for use in contexts outside the DOM.

Happy to give some further pointers if you (or anyone else) is interested in contributing this.

@sepiropht
Copy link

I'm want to try it ! It is possible ?

@fitzgen
Copy link
Owner

fitzgen commented Jun 4, 2019

@sepiropht sure thing!

To expand a bit on what I wrote above:

I think actually rather than making this a method on Node, it should be a function that takes any generic R: Render. It would internally create an empty rendering context, render the component into it, and then stringify the resulting node recursively by passing around a mutable reference to the string that is being built up, and matching on each different kind of node.

Here is a sketch:

pub fn html_string<R>(component: &R) -> String
where
    R: Render,
{
    // Create an empty rendering context. You'll need to create this `empty`
    // method.
    let cx = &mut RenderContext::empty();

    // Render the component in the new context.
    let node = component.render(cx);

    // Stringify the node into an HTML string.
    let mut s = String::new();
    html_string_recursive(cx, &mut s, self);
    return s;

    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) => {
                // TODO: push the HTML-formatted element to `s`...

                // Recursively stringify the children.
                for c in &e.children {
                    html_string(cx, s, c);
                }

                // TODO: push the close tag to `s`...
            }
            NodeKind::Cached(ref c) => {
                // TODO: use cx.cached_set.get to get the underlying cached node
                // and then call `html_string_recursive` on it...
            }
        }
    }
}

@sepiropht
Copy link

Ok thanks! I will look at this as soon as possible!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants