use crate::asset_snapshot::AssetSnapshot;
use crate::resolve::embed_renderer::html_escape_attr;
use crate::resolve::title_params::TitleParams;
const CLASS_EMBED: &str = "moss-embed";
#[allow(unused_variables)]
pub fn synthesize_iframe_html(
params: &TitleParams,
src: &str,
assets: &AssetSnapshot,
) -> String {
let mut full_src = String::from(src);
if let Some(q) = params.get("query") {
full_src.push('?');
full_src.push_str(q);
}
if let Some(f) = params.get("fragment") {
full_src.push('#');
full_src.push_str(f);
}
let data_width_attr = match params.get("data-width") {
Some(w) => format!(r#" data-width="{}""#, html_escape_attr(w)),
None => String::new(),
};
let title_attr = match params.get("title") {
Some(t) => format!(" title=\"{}\"", html_escape_attr(t)),
None => String::new(),
};
let width_attr = match params.get("width") {
Some(w) => format!(" width=\"{}\"", html_escape_attr(w)),
None => String::new(),
};
let height_attr = match params.get("height") {
Some(h) => format!(" height=\"{}\"", html_escape_attr(h)),
None => String::new(),
};
format!(
"<iframe class=\"{}\" data-type=\"iframe\"{} src=\"{}\"{}{}{} loading=\"lazy\"></iframe>",
CLASS_EMBED,
data_width_attr,
html_escape_attr(&full_src),
title_attr,
width_attr,
height_attr,
)
}
#[cfg(test)]
mod tests {
use super::*;
fn empty_snapshot() -> AssetSnapshot {
AssetSnapshot::new()
}
fn params_with(kvs: &[(&str, &str)]) -> TitleParams {
let mut p = TitleParams::default();
for (k, v) in kvs {
p.insert(*k, *v);
}
p
}
#[test]
fn iframe_basic_shape() {
let p = params_with(&[("kind", "iframe")]);
let out = synthesize_iframe_html(&p, "widget.html", &empty_snapshot());
assert!(out.contains("<iframe"));
assert!(out.contains(r#"src="widget.html""#));
assert!(out.contains(r#"loading="lazy""#));
assert!(out.contains(r#"class="moss-embed""#));
assert!(out.contains(r#"data-type="iframe""#));
}
#[test]
fn iframe_with_data_width() {
let p = params_with(&[("kind", "iframe"), ("data-width", "wide")]);
let out = synthesize_iframe_html(&p, "widget.html", &empty_snapshot());
assert!(out.contains(r#"data-width="wide""#), "got: {}", out);
}
#[test]
fn iframe_with_query_param() {
let p = params_with(&[("kind", "iframe"), ("query", "k=v&x=y")]);
let out = synthesize_iframe_html(&p, "widget.html", &empty_snapshot());
assert!(
out.contains(r#"src="widget.html?k=v"#)
|| out.contains(r#"src="widget.html?k=v&x=y""#),
"expected query in src, got: {}",
out
);
}
#[test]
fn iframe_with_title() {
let p = params_with(&[("kind", "iframe"), ("title", "My Widget")]);
let out = synthesize_iframe_html(&p, "widget.html", &empty_snapshot());
assert!(out.contains(r#"title="My Widget""#));
}
}