List structure is lost when pasting formatted content
OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Chrome 120.0 · Keyboard: US
Open case →Scenario
When editing text within list items, formatting such as bold, italic, or links may be lost or behave unexpectedly. The list structure itself may also be lost when certain operations are performed, such as pasting content or applying formatting.
When editing text within list items, formatting such as bold, italic, or links may be lost or behave unexpectedly. The list structure itself may also be lost when certain operations are performed, such as pasting content or applying formatting.
Intercept formatting operations and ensure list structure is preserved:
element.addEventListener('beforeinput', (e) => {
if (e.inputType === 'formatBold' || e.inputType === 'formatItalic') {
const selection = window.getSelection();
if (selection.rangeCount === 0) return;
const range = selection.getRangeAt(0);
const listItems = Array.from(range.commonAncestorContainer.closest('ul, ol')?.querySelectorAll('li') || [])
.filter(li => range.intersectsNode(li));
if (listItems.length > 0) {
e.preventDefault();
// Apply formatting while preserving list structure
applyFormattingToSelection(e.inputType, range, listItems);
}
}
});
element.addEventListener('paste', (e) => {
const selection = window.getSelection();
if (selection.rangeCount === 0) return;
const range = selection.getRangeAt(0);
const listItem = range.startContainer.closest('li');
if (listItem) {
e.preventDefault();
// Handle paste in list item, preserving list structure
handlePasteInListItem(e.clipboardData, range, listItem);
}
});
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-0116-list-formatting-lost-paste | Windows 11 | Desktop or Laptop Any | Chrome 120.0 | US | draft |
| ce-0124-list-formatting-bold-lost | Windows 11 | Desktop or Laptop Any | Firefox 120.0 | US | draft |
| ce-0163-list-formatting-across-items | 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 →Other scenarios that share similar tags or category.
Applying multiple formatting operations (bold, italic, underline, etc.) creates nested HTML elements that can become complex and hard to manage. Browsers handle nested formatting differently, and the resulting DOM structure can be inconsistent.
When typing text next to formatted elements (links, bold, italic, etc.) in contenteditable, the input events may include the formatted element's text in event.data, selection ranges may include the formatted element, and text may be inserted into the formatted element instead of after it. This occurs across different browsers and input methods.
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.
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.
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.
Have questions, suggestions, or want to share your experience? Join the discussion below.