Case ce-0108-code-block-whitespace-chrome · Scenario scenario-code-block-editing

Multiple spaces are collapsed in code blocks despite pre tag

OS: Windows 11 Device: Desktop or Laptop Any Browser: Chrome 120.0 Keyboard: US Status: draft
code pre whitespace chrome

Phenomenon

When editing text within a <pre><code> code block, multiple consecutive spaces may still be collapsed despite the <pre> tag which should preserve whitespace. This can break code formatting.

Reproduction example

  1. Create a code block: <pre><code>function test() {</code></pre>
  2. Try to type multiple spaces for indentation
  3. Observe the DOM

Observed behavior

  • Multiple spaces may be collapsed to single space
  • Even though <pre> should preserve whitespace
  • Code indentation is lost
  • Formatting breaks

Expected behavior

  • <pre> tag should preserve all whitespace
  • Multiple spaces should be maintained
  • Code formatting should be preserved
  • Behavior should match native code editors

Browser Comparison

  • Chrome/Edge: May collapse spaces despite <pre> (this case)
  • Firefox: Similar whitespace handling issues
  • Safari: Whitespace preservation inconsistent

Notes and possible direction for workarounds

  • Ensure white-space: pre CSS is applied
  • Intercept space insertion and use &nbsp; if needed
  • Monitor and preserve whitespace in code blocks
  • Consider using contenteditable=“false” for code blocks and custom editing
Before
    function test() {
Code block with indentation (4 spaces)
→
After Editing (Bug)
 function test() {
After editing, multiple spaces collapsed to one
vs
✅ Expected
    function test() {
Expected: Spaces preserved with &lt;pre&gt; tag

Browser compatibility matrix

This matrix shows which browser and OS combinations have documented cases for this scenario. The current case is highlighted. Click on a cell to view other cases.

Current case
Confirmed
Draft
— No case documented

All variants (detailed table)

Complete list of all cases for this scenario with full environment details.

Case OS Device Browser Keyboard Status
ce-0108-code-block-whitespace-chrome Windows 11 Desktop or Laptop Any Chrome 120.0 US draft
ce-0136-code-block-paste-formats Windows 11 Desktop or Laptop Any Firefox 120.0 US draft
ce-0147-code-block-line-breaks-lost Windows 11 Desktop or Laptop Any Chrome 120.0 US draft
ce-0157-code-block-formatting-allowed Windows 11 Desktop or Laptop Any Chrome 120.0 US draft

Playground for this case

Use the reported environment as a reference and record what happens in your environment while interacting with the editable area.

Reported environment
OS: Windows 11
Device: Desktop or Laptop Any
Browser: Chrome 120.0
Keyboard: US
Your environment
Sample HTML:
Event log
Use this log together with the case description when filing or updating an issue.
0 events
Interact with the editable area to see events here.

Comments & Discussion

Have questions, suggestions, or want to share your experience? Join the discussion below.