feat(core): join safe html fragments

Let already-safe fragments compose without downgrading to String, and use it in the v0 page assembly example to keep rendered and lowered fragments typed at the boundary.

req: html_safety/001

req: html_safety/002

req: component/003
This commit is contained in:
slhx agent
2026-06-01 23:49:20 +02:00
parent fd5d48d59a
commit 117eb57179
4 changed files with 35 additions and 12 deletions
+14
View File
@@ -141,6 +141,20 @@ impl SafeHtml {
pub fn as_str(&self) -> &str {
&self.0
}
pub fn join(fragments: impl IntoIterator<Item = SafeHtml>) -> Self {
fragments.into_iter().collect()
}
}
impl FromIterator<SafeHtml> for SafeHtml {
fn from_iter<T: IntoIterator<Item = SafeHtml>>(iter: T) -> Self {
let mut html = String::new();
for fragment in iter {
html.push_str(fragment.as_str());
}
Self(html)
}
}
impl AsRef<str> for SafeHtml {
+12
View File
@@ -93,6 +93,18 @@ fn slot_html_requires_explicit_safe_html() {
assert_eq!(payload, Payload::Html(String::from("<strong>ok</strong>")));
}
#[test]
fn safe_html_joins_only_explicit_safe_fragments() {
// req: html_safety/001 req: html_safety/002
let html = SafeHtml::join([
SafeHtml::trusted("<main>"),
SafeHtml::trusted("<strong>ok</strong>"),
SafeHtml::trusted("</main>"),
]);
assert_eq!(html.as_str(), "<main><strong>ok</strong></main>");
}
#[test]
fn param_names_format_generated_param_names() {
// req: codegen/003