From bc7e7881b43d5b1bd214a63e6050525f54f88edc Mon Sep 17 00:00:00 2001 From: slhx agent Date: Mon, 1 Jun 2026 23:57:19 +0200 Subject: [PATCH] feat(build): lower static fragments as safe html Add a generated static_fragment helper for include_str!-style prototype fragments and use it in the v0 page assembly path so static lowered .heml no longer needs repeated SafeHtml::trusted plumbing. req: dx/006 req: html_safety/001 req: html_safety/002 req: component/003 --- REQUIREMENTS.md | 2 +- examples/v0/src/main.rs | 10 +++++----- slhx-build/src/lib.rs | 6 ++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/REQUIREMENTS.md b/REQUIREMENTS.md index 33b2b53..19fb8aa 100644 --- a/REQUIREMENTS.md +++ b/REQUIREMENTS.md @@ -76,7 +76,7 @@ slhx competes with React by making frontend frameworks unnecessary for most apps 005 Error messages must explain fixes in author language, not internal language. Say “add `h-key="todo.id"` to this `h-for`”, not “missing ScopeKey for ResourceRef”. ### req: dx/006 -006 Generated resource methods are the preferred authoring API: `slots::todo_list.render(view)`, `slots::card.replace(key, view)`, `slots::count.text(42)`, `atoms::user.set(user)`. The public facade and generated view modules expose `render(view)` for trusted hemplate-to-`SafeHtml` page and fragment composition, plus `lower(html)` for prototype/static `.heml` fragments that need generated resource lowering; `render_html(view)` and `lower_html(html)` remain explicit compatibility aliases. These return `impl IntoEffect`, `SafeHtml`, or lowered HTML at the boundary. Raw `Effect` constructors, opcodes, and `EffectWriter` remain low-level. [north_star] +006 Generated resource methods are the preferred authoring API: `slots::todo_list.render(view)`, `slots::card.replace(key, view)`, `slots::count.text(42)`, `atoms::user.set(user)`. The public facade and generated view modules expose `render(view)` for trusted hemplate-to-`SafeHtml` page and fragment composition, `static_fragment(include_str!(...))` for prototype/static `.heml` fragments that need generated resource lowering as `SafeHtml`, plus `lower(html)` for callers that need the lowered string; `render_html(view)` and `lower_html(html)` remain explicit compatibility aliases. These return `impl IntoEffect`, `SafeHtml`, or lowered HTML at the boundary. Raw `Effect` constructors, opcodes, and `EffectWriter` remain low-level. [north_star] ### req: dx/007 007 Tuple composition of `IntoEffect` is the canonical batch syntax: `(a, b, c)` implements `IntoEffect` up to arity 12. `Effect::batch((...))` is available but not required for the happy path. diff --git a/examples/v0/src/main.rs b/examples/v0/src/main.rs index 9200641..d45ff2b 100644 --- a/examples/v0/src/main.rs +++ b/examples/v0/src/main.rs @@ -183,12 +183,12 @@ fn all_examples() -> SafeHtml { // Static `.heml` fragments are lowered by generated code before they join rendered views. // req: html_safety/001 req: html_safety/002 req: component/003 SafeHtml::join([ - SafeHtml::trusted(counter::lower(include_str!("../templates/counter.heml"))), - SafeHtml::trusted(todos::lower(include_str!("../templates/todos.heml"))), - SafeHtml::trusted(wizard::lower(include_str!("../templates/wizard.heml"))), - SafeHtml::trusted(auth::lower(include_str!("../templates/auth.heml"))), + counter::static_fragment(include_str!("../templates/counter.heml")), + todos::static_fragment(include_str!("../templates/todos.heml")), + wizard::static_fragment(include_str!("../templates/wizard.heml")), + auth::static_fragment(include_str!("../templates/auth.heml")), render_page_swap("Welcome", "Welcome"), - SafeHtml::trusted(notifications::lower(include_str!("../templates/notifications.heml"))), + notifications::static_fragment(include_str!("../templates/notifications.heml")), ]) } diff --git a/slhx-build/src/lib.rs b/slhx-build/src/lib.rs index 26bdd7c..8c65bf0 100644 --- a/slhx-build/src/lib.rs +++ b/slhx-build/src/lib.rs @@ -525,6 +525,9 @@ impl Resources { out.push_str(&format!("__slhx_lower_html(html.as_ref(), &{table_name})\n")); out.push_str(&format!("{pad}}}\n\n")); out.push_str(&format!("{pad}pub fn lower_html(html: impl ::std::convert::AsRef) -> ::std::string::String {{ lower(html) }}\n\n")); + out.push_str(&format!("{pad}pub fn static_fragment(html: &'static str) -> ::slhx::SafeHtml {{\n")); + out.push_str(&format!("{pad} ::slhx::SafeHtml::trusted(lower(html))\n")); + out.push_str(&format!("{pad}}}\n\n")); out.push_str(&format!("{pad}pub fn render(view: &impl ::hemplate::Hemplate) -> ::slhx::SafeHtml {{\n")); out.push_str(&format!("{pad} let mut html = ::std::string::String::new();\n")); out.push_str(&format!("{pad} ::hemplate::Hemplate::render_into(view, &mut html).expect(\"slhx hemplate view renders\");\n")); @@ -1301,6 +1304,7 @@ mod tests { assert!(generated.contains("pub const ALL_IDS")); assert!(generated.contains("pub fn lower(html: impl ::std::convert::AsRef) -> ::std::string::String")); assert!(generated.contains("pub fn lower_html")); + assert!(generated.contains("pub fn static_fragment(html: &'static str) -> ::slhx::SafeHtml")); assert!(generated.contains("pub fn render(view: &impl ::hemplate::Hemplate) -> ::slhx::SafeHtml")); assert!(generated.contains("pub fn render_html(view: &impl ::hemplate::Hemplate) -> ::slhx::SafeHtml")); assert!(generated.contains("data-slhx-slot")); @@ -1369,12 +1373,14 @@ mod tests { assert!(!generated.contains("\n#[allow(non_upper_case_globals)]\npub mod slots")); assert!(generated.contains("\npub fn lower(html: impl ::std::convert::AsRef) -> ::std::string::String")); assert!(generated.contains("\npub fn lower_html")); + assert!(generated.contains("\npub fn static_fragment(html: &'static str) -> ::slhx::SafeHtml")); assert!(generated.contains("\npub fn render(view: &impl ::hemplate::Hemplate) -> ::slhx::SafeHtml")); assert!(generated.contains("\npub fn render_html(view: &impl ::hemplate::Hemplate) -> ::slhx::SafeHtml")); assert!(generated.contains("pub mod todo")); assert!(generated.contains(" pub const COMPONENT: ::slhx::ComponentRef = ::slhx::ComponentRef::new(\"todo\")")); assert!(generated.contains(" pub mod slots")); assert!(generated.contains(" pub fn lower_html")); + assert!(generated.contains(" pub fn static_fragment(html: &'static str) -> ::slhx::SafeHtml")); let _ = std::fs::remove_dir_all(&base); }