Creating link from selected text works but may create nested links in Chrome
OS: Windows 11 · Device: Desktop or Laptop Any · Browser: Chrome 120.0 · Keyboard: US
Open case →Scenario
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.
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.
<a> element around selected text, preserves formattingImplement custom link handling:
element.addEventListener('beforeinput', (e) => {
if (e.inputType === 'formatCreateLink') {
e.preventDefault();
const selection = window.getSelection();
if (selection.rangeCount === 0) return;
const range = selection.getRangeAt(0);
const url = prompt('Enter URL:');
if (url) {
// Create link while avoiding nested links
createLinkSafely(range, url);
}
}
});
function createLinkSafely(range, url) {
// Check if selection is already in a link
const existingLink = range.commonAncestorContainer.closest('a');
if (existingLink) {
// Remove existing link first
const parent = existingLink.parentNode;
while (existingLink.firstChild) {
parent.insertBefore(existingLink.firstChild, existingLink);
}
parent.removeChild(existingLink);
}
// Create new link
const link = document.createElement('a');
link.href = url;
link.textContent = range.toString();
range.deleteContents();
range.insertNode(link);
}
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-0100-link-insertion-chrome | Windows 11 | Desktop or Laptop Any | Chrome 120.0 | US | draft |
| ce-0133-link-removal-leaves-empty | Windows 11 | Desktop or Laptop Any | Firefox 120.0 | US | draft |
| ce-0152-link-href-update-difficult | 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.
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.
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.
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.
Have questions, suggestions, or want to share your experience? Join the discussion below.