blitz_script/lib.rs
1//! JavaScript execution on top of Blitz
2//!
3//! This crate implements a [`ScriptDocument`]: a wrapper around a [`BaseDocument`](blitz_dom::BaseDocument)
4//! which can execute the JavaScript contained in (or referenced by) the document's `<script>` tags
5//! using the [Boa](https://boajs.dev) JavaScript engine, and which exposes JavaScript DOM APIs
6//! (`document`, elements, events, timers, etc) backed by `blitz-dom` to the scripts it runs.
7//!
8//! It is capable of running real-world JavaScript frameworks such as [Preact](https://preactjs.com/).
9//!
10//! ### Modules
11//!
12//! `<script type="module">` is parsed in module goal and its imports are
13//! resolved and fetched through the same [`ScriptFetcher`] as classic
14//! `<script src>`, so a module graph blocks the document thread the same way a
15//! classic script already does. `<script type="importmap">`, `import.meta.url`,
16//! dynamic `import()` and JSON modules (`with { type: "json" }`) are supported;
17//! import-map `integrity` and module workers are not.
18//!
19//! ### Example
20//!
21//! ```rust
22//! use blitz_script::ScriptDocument;
23//! use blitz_dom::DocumentConfig;
24//!
25//! let mut doc = ScriptDocument::from_html(
26//! r#"
27//! <div id="root"></div>
28//! <script>
29//! const el = document.createElement("h1");
30//! el.textContent = "Hello from JS";
31//! document.getElementById("root").appendChild(el);
32//! </script>
33//! "#,
34//! DocumentConfig::default(),
35//! );
36//! doc.execute_scripts();
37//! ```
38
39#![allow(clippy::collapsible_if)]
40
41#[cfg(feature = "debug-control")]
42mod debug_control;
43mod document;
44mod docwrite;
45mod dom;
46pub mod dom_exception;
47mod domc;
48mod event_handler;
49mod fetch;
50mod fonts;
51mod job_budget;
52pub mod media;
53mod module;
54mod raster;
55mod runtime;
56pub mod script_stats;
57mod state;
58mod timers;
59
60// Embedders extending the native script surface must use the same Boa types
61// and registry dependencies as this engine.
62pub use boa_engine;
63pub use boa_runtime;
64
65#[cfg(feature = "debug-control")]
66pub use debug_control::DebugController;
67pub use document::ScriptDocument;
68pub use fetch::{DefaultScriptFetcher, FetchError, ScriptFetcher};
69pub use raster::raster_image_for_attribute;