Skip to main content

glory_web/
lib.rs

1//! Browser-only entry points are target gated; enabling web never changes core.
2#[cfg(target_arch = "wasm32")]
3mod browser;
4#[cfg(any(target_arch = "wasm32", test))]
5mod clipboard_state;
6#[cfg(any(target_arch = "wasm32", test))]
7mod file_reads;
8#[cfg(all(test, not(target_arch = "wasm32")))]
9#[path = "browser/plan_fields.rs"]
10mod plan_fields_tests;
11#[cfg(all(test, not(target_arch = "wasm32")))]
12#[path = "browser/plan_parts.rs"]
13mod plan_parts_tests;
14#[cfg(all(feature = "routing", target_arch = "wasm32"))]
15pub mod routing;
16#[cfg(any(target_arch = "wasm32", test))]
17mod semantic_changes;
18/// Experimental direct HTML template/slot runtime (limited non-editor profile).
19#[cfg(target_arch = "wasm32")]
20pub mod template;
21#[cfg(all(test, not(target_arch = "wasm32")))]
22#[path = "browser/templates.rs"]
23mod template_tests;
24pub mod view;
25#[cfg(target_arch = "wasm32")]
26pub use browser::*;
27#[cfg(feature = "stream")]
28pub const STREAM_SCRIPT: &str = include_str!("stream.js");
29
30#[cfg(all(test, feature = "stream"))]
31#[test]
32fn stream_script_accepts_the_current_html_format_at_start_and_end() {
33    for envelope in ["initial", "frame"] {
34        assert!(STREAM_SCRIPT.contains(&format!(
35            "{envelope}.html_version !== {} ||",
36            glory_html::FORMAT_VERSION
37        )));
38    }
39}
40
41/// Read a snapshot without modifying/consuming the data block. A failed
42/// hydration can create a fresh Resources context from the same payload.
43#[cfg(all(target_arch = "wasm32", feature = "resources"))]
44pub fn read_resources(
45    element: &web_sys::Element,
46    limits: glory_resource::Limits,
47) -> Result<glory_resource::Snapshot, glory_resource::Error> {
48    use glory_resource::{Error, Snapshot, VERSION};
49    if element.namespace_uri().as_deref() != Some("http://www.w3.org/1999/xhtml")
50        || element.local_name() != "script"
51        || element.get_attribute("type").as_deref() != Some("application/json")
52    {
53        return Err(Error::Payload(
54            "expected an application/json script element",
55        ));
56    }
57    if element.get_attribute("data-gly-resources").as_deref() != Some(VERSION.to_string().as_str())
58    {
59        return Err(Error::Payload("resource data block version"));
60    }
61    let json = element
62        .text_content()
63        .ok_or(Error::Payload("resource data block has no text"))?;
64    Snapshot::from_json(&json, limits)
65}
66
67/// Restore explicit resources and adopt the same initial factory. Failure
68/// preserves the payload for a fresh attempt; success removes its DOM data
69/// block so large serialized values are not retained alongside live state.
70#[cfg(all(target_arch = "wasm32", feature = "resources", feature = "hydrate"))]
71pub fn hydrate_resources_with<W: glory_ui::Widget>(
72    root: web_sys::Element,
73    data: &web_sys::Element,
74    limits: glory_resource::Limits,
75    factory: impl FnOnce(&glory_ui::Scope, &glory_resource::Resources) -> W,
76) -> Result<(WebApp, glory_resource::Resources), HydrationError> {
77    if root.contains(Some(data.as_ref())) {
78        return Err(HydrationError::Resources(glory_resource::Error::Payload(
79            "resource block must be outside the mount",
80        )));
81    }
82    let snapshot = read_resources(data, limits).map_err(HydrationError::Resources)?;
83    let resources =
84        glory_resource::Resources::restore(snapshot).map_err(HydrationError::Resources)?;
85    let app = hydrate_with(root, |scope| factory(scope, &resources))?;
86    data.remove();
87    Ok((app, resources))
88}