#dom #node #simulated #replace #web-sys #sandbox #browser-independent

nightly rdom

A Rust-based simulated DOM (browser-independent replacement for web_sys)

10 releases

0.2.0 Apr 20, 2021
0.1.8 Mar 30, 2021

#4 in #simulated

MIT license

37KB
863 lines

rdom logo

DOM in Rust without a browser

This repository is archived. The effort was shelved because I do not have a need for it anymore. If you need a DOM in Rust, it is worth considering whether you need something simple, or if you need the full dynamic capabilities of something like JSDom. If the latter, I would suggest maybe using JSDOM + Deno. If the former, picking up development of this library may be worth it. However, we were running into some complicated situations so, I'm just saying there may be dragons.

Old introduction

Hello and welcome. This library provides server-side or browserless simulation of a DOM.

docs.rs CI status Discord Chat

Example Usage

use std::sync::Arc;

use rdom::config::ScreenMetrics;
use rdom::node::concrete::*;
use rdom::node::contents::{CommentNodeStorage, NodeType, TextNodeStorage};
use rdom::node::element::{ElementNodeStorage, HtmlButtonElementStorage, HtmlHtmlElementStorage};
use rdom::node::{AnyNodeArc, NodeBehavior};
use rdom::sandbox::Sandbox;

fn main() {
    let metrics: ScreenMetrics = Default::default();
    let sbox = Sandbox::new(metrics);
    let doc = sbox.clone().window().document();

    let document_element = sbox
        .builder::<ElementNodeArc>()
        .build_html();

    // We don't use the text node, but those are available
    let _text = doc.create_text_node("Hello, world!".to_string());

    doc.append_child(document_element.into());

    println!("Doc has {} child node(s)", doc.child_nodes().length());
    // Prints out Doc has 1 child node(s)
}

Is this library ready yet?

No, it's still a pre-alpha work in progress. However, it is open to collaboration.

What's the difference between this library and say, web-sys or Dodrio?

Rdom tries to behave like web-sys as much as possible, in that it provides a DOM that looks and acts a lot like a DOM as a Rust programmer would see a DOM via interaction with web-sys.

The key difference is that there is no actual browser. I mean, you could have one, but it's not required.

Additionally, this library supports the concept of multiple sandboxes, so not all nodes belong to one big node pool like in web-sys. Instead, each sandbox contains a single document tree and some information about the window (like width and height).

Compared to Dodrio, this library is focused much more on the actual DOM element types. Dodrio is basically agnostic to what different types of tags are, and is more focused on the question of how to manipulate a DOM to match some ideal, like React's virtual DOM does. If that's still unclear, Dodrio targets web-sys as a backend (it's a dependency), and in theory, this library could also be a backend for Dodrio someday.

Not all of the DOM has been implemented. How did you decide what parts to implement?

The reason for developing this library was to enable SSR support for Yew. That was the entire purpose, so while it is built to be as versatile as reasonable, that is the main use case that we are serving right now, although applications to become a maintainer are certainly welcome.


lib.rs:

A Rust-based simulated DOM (browser-independent replacement for web_sys)

Dependencies

~275–730KB
~17K SLoC