Applying multiple formatting creates deeply nested structures
OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Chrome 120.0 · Keyboard: US
Open case →Scenario
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.
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.
<b><i>text</i></b> or <i><b>text</b></i>Normalize formatting structure:
function normalizeFormatting(element) {
// Normalize nested formatting to consistent structure
const formattingTags = ['b', 'strong', 'i', 'em', 'u', 's', 'strike'];
formattingTags.forEach(tag => {
const elements = element.querySelectorAll(tag);
elements.forEach(el => {
// If element only contains whitespace, remove it
if (!el.textContent.trim()) {
const parent = el.parentNode;
while (el.firstChild) {
parent.insertBefore(el.firstChild, el);
}
el.remove();
return;
}
// If parent has same tag, merge
if (el.parentElement && el.parentElement.tagName.toLowerCase() === tag) {
const parent = el.parentElement;
while (el.firstChild) {
parent.insertBefore(el.firstChild, el);
}
el.remove();
}
});
});
// Ensure consistent tag usage (b vs strong, i vs em)
element.querySelectorAll('strong').forEach(strong => {
const b = document.createElement('b');
while (strong.firstChild) {
b.appendChild(strong.firstChild);
}
strong.parentNode.replaceChild(b, strong);
});
element.querySelectorAll('em').forEach(em => {
const i = document.createElement('i');
while (em.firstChild) {
i.appendChild(em.firstChild);
}
em.parentNode.replaceChild(i, em);
});
}
element.addEventListener('input', () => {
requestAnimationFrame(() => {
normalizeFormatting(element);
});
});
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-0112-nested-formatting-complex | Windows 11 | Desktop or Laptop Any | Chrome 120.0 | US | draft |
| ce-0128-nested-formatting-removal | Windows 11 | Desktop or Laptop Any | Chrome 120.0 | US | draft |
| ce-0160-nested-formatting-order-inconsistent | Windows 11 | Desktop or Laptop Any | Safari 17.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 | |
| Safari |
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: Chrome 120.0 · Keyboard: US
Open case →OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Safari 17.0 · Keyboard: US
Open case →Other scenarios that share similar tags or category.
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 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 blockquote elements in contenteditable behaves inconsistently across browsers. Pressing Enter, applying formatting, or pasting content may break the blockquote structure, create nested blockquotes, or behave unexpectedly.
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.
Have questions, suggestions, or want to share your experience? Join the discussion below.