diff --git a/crates/engine/src/cmd/richclip.rs b/crates/engine/src/cmd/richclip.rs
index db7feb56..5ef4dbbe 100644
--- a/crates/engine/src/cmd/richclip.rs
+++ b/crates/engine/src/cmd/richclip.rs
@@ -16,20 +16,51 @@ const MAX_RUNS: usize = 10_000;
pub fn body_to_html(body: &TextBody) -> String {
let mut out = String::new();
for (i, p) in body.paragraphs.iter().enumerate() {
- if i > 0 {
- out.push_str("
");
+ let prefix = if i > 0 { "
" } else { "
" }; + if !push_if_fits(&mut out, prefix, 4) { + return plain_html(body); } - out.push_str("
");
for r in &p.runs {
let (open, close) = tags(&r.props);
- out.push_str(&open);
+ if !push_if_fits(&mut out, &open, close.len() + 4) {
+ return plain_html(body);
+ }
match &r.kind {
- deckcraft_model::text::RunKind::Break => out.push_str("
"),
- _ => push_escaped(&mut out, &r.text),
+ deckcraft_model::text::RunKind::Break => {
+ if !push_if_fits(&mut out, "
", close.len() + 4) {
+ return plain_html(body);
+ }
+ }
+ _ => {
+ if !push_escaped_bounded(&mut out, &r.text, close.len() + 4) {
+ return plain_html(body);
+ }
+ }
}
out.push_str(&close);
- if out.len() > MAX_HTML {
- break;
+ }
+ out.push_str("
" } else { "
" };
+ if !push_if_fits(&mut out, prefix, 4) {
+ return String::new();
+ }
+ for r in &p.runs {
+ let fits = if matches!(&r.kind, deckcraft_model::text::RunKind::Break) {
+ push_if_fits(&mut out, "
", 4)
+ } else {
+ push_escaped_bounded(&mut out, &r.text, 4)
+ };
+ if !fits {
+ return String::new();
}
}
out.push_str("