Multiple spaces are collapsed in code blocks despite pre tag
OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Chrome 120.0 · Keyboard: US
Open case →Scenario
Editing text within code blocks (<pre><code>) in contenteditable elements behaves inconsistently across browsers. Line breaks, indentation, whitespace preservation, and formatting may be handled differently, making it difficult to maintain code formatting.
Editing text within code blocks (<pre><code>) in contenteditable elements behaves inconsistently across browsers. Line breaks, indentation, whitespace preservation, and formatting may be handled differently, making it difficult to maintain code formatting.
<br> tags<pre> tagImplement custom code block handling:
element.addEventListener('input', (e) => {
const codeBlocks = element.querySelectorAll('pre code, code');
codeBlocks.forEach(code => {
// Preserve whitespace
if (!code.style.whiteSpace) {
code.style.whiteSpace = 'pre';
}
// Prevent formatting inside code
code.addEventListener('beforeinput', (e) => {
if (e.inputType.startsWith('format')) {
e.preventDefault();
}
});
});
});
// Handle paste in code blocks
element.addEventListener('paste', (e) => {
const selection = window.getSelection();
if (selection.rangeCount === 0) return;
const range = selection.getRangeAt(0);
const code = range.startContainer.closest('code, pre');
if (code) {
e.preventDefault();
const text = e.clipboardData.getData('text/plain');
// Preserve whitespace and line breaks
const lines = text.split('\n');
const fragment = document.createDocumentFragment();
lines.forEach((line, index) => {
fragment.appendChild(document.createTextNode(line));
if (index < lines.length - 1) {
fragment.appendChild(document.createElement('br'));
}
});
range.deleteContents();
range.insertNode(fragment);
}
});
Visual view of how this scenario connects to its concrete cases and environments. Nodes can be dragged and clicked.
Each row is a concrete case for this scenario, with a dedicated document and playground.
| 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 |
This matrix shows which browser and OS combinations have documented cases for this scenario. Click on a cell to view the specific case.
| Browser | Windows |
|---|---|
| Chrome | |
| Firefox |
Open a case to see the detailed description and its dedicated playground.
OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Chrome 120.0 · Keyboard: US
Open case →OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Firefox 120.0 · Keyboard: US
Open case →OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Chrome 120.0 · Keyboard: US
Open case →OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Chrome 120.0 · Keyboard: US
Open case →Other scenarios that share similar tags or category.
The document.execCommand() API, which is commonly used to apply formatting (bold, italic, etc.) in contenteditable regions, has been deprecated. However, there is no complete replacement, and many implementations still rely on it. This creates uncertainty about future browser support.
Changing font family in contenteditable elements behaves inconsistently across browsers. The font-family CSS property may be applied inline, as a style attribute, or may not be applied at all. The behavior also varies when editing text after applying a font.
When applying bold formatting to selected text and then continuing to type, the bold formatting is not maintained for the newly typed characters in Safari.
When using document.execCommand('insertHTML', ...) to insert HTML content into a contenteditable region, the DOM structure may be broken or reformatted unexpectedly. Nested elements may be flattened or reorganized.
When inserting or editing links in contenteditable elements, the behavior varies significantly across browsers. Creating links, editing link text, and removing links can result in unexpected DOM structures or lost formatting.
Have questions, suggestions, or want to share your experience? Join the discussion below.