From 74ab8d2819af07fe5d61bbe9eb8dbfe5e10091e2 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Mon, 19 Oct 2026 18:32:27 +0200 Subject: [PATCH] Keep a single leading space in HTML export paragraphs (#3102) Found while testing crates/formats/src/html.rs (export, around line 1744; paragraph text writer) Co-authored-by: Claude Haiku 5.5 --- crates/formats/src/html.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/crates/formats/src/html.rs b/crates/formats/src/html.rs index 91d328623..a25c1f5c6 100644 --- a/crates/formats/src/html.rs +++ b/crates/formats/src/html.rs @@ -1474,9 +1474,9 @@ fn align_css(a: Option) -> Option<&'static str> { } } -fn text_html(t: &str) -> String { +fn text_html(t: &str, at_start: bool) -> String { let mut o = String::new(); - let mut prev_space = false; + let mut prev_space = at_start; for c in t.chars() { match c { '\n' => { @@ -1547,8 +1547,9 @@ pub fn inlines_html(inlines: &[Inline]) -> String { open.push_str(&format!("", css.join(";"))); close.push(""); } + let at_start = o.is_empty(); o.push_str(&open); - o.push_str(&text_html(t)); + o.push_str(&text_html(t, at_start)); for c in close.iter().rev() { o.push_str(c); } @@ -2036,4 +2037,13 @@ mod tests { }; assert_eq!(p.text(), "World"); } + + #[test] + fn leading_space_survives_round_trip() { + let doc = Document::from_text(" lead"); + let html = export(&doc); + assert!(!html.contains("

lead"), "{html}"); + let back = import(&html); + assert_eq!(back.plain_text(Default::default()).replace('\u{a0}', " "), " lead"); + } }