moss_core/render/
iframe.rs1use crate::asset_snapshot::AssetSnapshot;
18use crate::resolve::embed_renderer::html_escape_attr;
19use crate::resolve::title_params::TitleParams;
20
21const CLASS_EMBED: &str = "moss-embed";
24
25#[allow(unused_variables)]
48pub fn synthesize_iframe_html(
49 params: &TitleParams,
50 src: &str,
51 assets: &AssetSnapshot,
52) -> String {
53 let mut full_src = String::from(src);
58 if let Some(q) = params.get("query") {
59 full_src.push('?');
60 full_src.push_str(q);
61 }
62 if let Some(f) = params.get("fragment") {
63 full_src.push('#');
64 full_src.push_str(f);
65 }
66
67 let data_width_attr = match params.get("data-width") {
68 Some(w) => format!(r#" data-width="{}""#, html_escape_attr(w)),
69 None => String::new(),
70 };
71
72 let title_attr = match params.get("title") {
73 Some(t) => format!(" title=\"{}\"", html_escape_attr(t)),
74 None => String::new(),
75 };
76
77 let width_attr = match params.get("width") {
78 Some(w) => format!(" width=\"{}\"", html_escape_attr(w)),
79 None => String::new(),
80 };
81
82 let height_attr = match params.get("height") {
83 Some(h) => format!(" height=\"{}\"", html_escape_attr(h)),
84 None => String::new(),
85 };
86
87 format!(
88 "<iframe class=\"{}\" data-type=\"iframe\"{} src=\"{}\"{}{}{} loading=\"lazy\"></iframe>",
89 CLASS_EMBED,
90 data_width_attr,
91 html_escape_attr(&full_src),
92 title_attr,
93 width_attr,
94 height_attr,
95 )
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 fn empty_snapshot() -> AssetSnapshot {
103 AssetSnapshot::new()
104 }
105
106 fn params_with(kvs: &[(&str, &str)]) -> TitleParams {
107 let mut p = TitleParams::default();
108 for (k, v) in kvs {
109 p.insert(*k, *v);
110 }
111 p
112 }
113
114 #[test]
115 fn iframe_basic_shape() {
116 let p = params_with(&[("kind", "iframe")]);
117 let out = synthesize_iframe_html(&p, "widget.html", &empty_snapshot());
118 assert!(out.contains("<iframe"));
119 assert!(out.contains(r#"src="widget.html""#));
120 assert!(out.contains(r#"loading="lazy""#));
121 assert!(out.contains(r#"class="moss-embed""#));
122 assert!(out.contains(r#"data-type="iframe""#));
123 }
124
125 #[test]
126 fn iframe_with_data_width() {
127 let p = params_with(&[("kind", "iframe"), ("data-width", "wide")]);
128 let out = synthesize_iframe_html(&p, "widget.html", &empty_snapshot());
129 assert!(out.contains(r#"data-width="wide""#), "got: {}", out);
130 }
131
132 #[test]
133 fn iframe_with_query_param() {
134 let p = params_with(&[("kind", "iframe"), ("query", "k=v&x=y")]);
135 let out = synthesize_iframe_html(&p, "widget.html", &empty_snapshot());
136 assert!(
137 out.contains(r#"src="widget.html?k=v"#)
138 || out.contains(r#"src="widget.html?k=v&x=y""#),
139 "expected query in src, got: {}",
140 out
141 );
142 }
143
144 #[test]
145 fn iframe_with_title() {
146 let p = params_with(&[("kind", "iframe"), ("title", "My Widget")]);
147 let out = synthesize_iframe_html(&p, "widget.html", &empty_snapshot());
148 assert!(out.contains(r#"title="My Widget""#));
149 }
150
151}