From 4a975fdb8b4c2700f2ed0cb9d77f62b3573bdb80 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 5 Aug 2025 17:13:21 +0200 Subject: [PATCH] Ensure that whitespace is not collapsed in inline raw blocks (#6703) --- crates/typst-html/src/rules.rs | 14 +++++++++----- tests/ref/html/raw-html-inline-spaces.html | 10 ++++++++++ tests/ref/html/raw-html.html | 2 +- tests/suite/text/raw.typ | 3 +++ 4 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 tests/ref/html/raw-html-inline-spaces.html diff --git a/crates/typst-html/src/rules.rs b/crates/typst-html/src/rules.rs index 0633d4a9..0f73ef8c 100644 --- a/crates/typst-html/src/rules.rs +++ b/crates/typst-html/src/rules.rs @@ -415,16 +415,20 @@ const RAW_RULE: ShowFn = |elem, _, styles| { seq.push(line.clone().pack()); } + let mut inline = css::Properties::new(); + let block = elem.block.get(styles); + if !block { + // Without the `
` tag, whitespace would be collapsed by default.
+        inline.push("white-space", "pre-wrap");
+    }
+
     let code = HtmlElem::new(tag::code)
+        .with_styles(inline)
         .with_body(Some(Content::sequence(seq)))
         .pack()
         .spanned(elem.span());
 
-    Ok(if elem.block.get(styles) {
-        HtmlElem::new(tag::pre).with_body(Some(code)).pack()
-    } else {
-        code
-    })
+    Ok(if block { HtmlElem::new(tag::pre).with_body(Some(code)).pack() } else { code })
 };
 
 /// This is used by `RawElem::synthesize` through a routine.
diff --git a/tests/ref/html/raw-html-inline-spaces.html b/tests/ref/html/raw-html-inline-spaces.html
new file mode 100644
index 00000000..193d0425
--- /dev/null
+++ b/tests/ref/html/raw-html-inline-spaces.html
@@ -0,0 +1,10 @@
+
+
+  
+    
+    
+  
+  
+    

This has double spaces inside, which should be kept.

+ + diff --git a/tests/ref/html/raw-html.html b/tests/ref/html/raw-html.html index 18cf19c9..cd476c8b 100644 --- a/tests/ref/html/raw-html.html +++ b/tests/ref/html/raw-html.html @@ -5,7 +5,7 @@ -

This is *inline*.

+

This is *inline*.

#set text(blue)
*Hello* _world_!
diff --git a/tests/suite/text/raw.typ b/tests/suite/text/raw.typ index 79a1c000..7add8cdc 100644 --- a/tests/suite/text/raw.typ +++ b/tests/suite/text/raw.typ @@ -494,6 +494,9 @@ This is ```typ *inline*```. *Hello* _world_! ``` +--- raw-html-inline-spaces html --- +This has `double spaces inside`, which should be kept. + --- raw-line --- #set page(width: 200pt)