Scenario

HTML entity encoding and decoding is inconsistent

Special characters in contenteditable elements may be encoded as HTML entities (<, >, &, etc.) or decoded to their actual characters inconsistently across browsers. This can cause issues when copying, pasting, or serializing content.

formatting
Scenario ID
scenario-html-entity-encoding

Details

Special characters in contenteditable elements may be encoded as HTML entities (<, >, &, etc.) or decoded to their actual characters inconsistently across browsers. This can cause issues when copying, pasting, or serializing content.

Observed Behavior

Scenario 1: Typing special characters

  • Chrome/Edge: Characters may be stored as entities or actual characters
  • Firefox: Similar inconsistent behavior
  • Safari: May encode/decode differently

Scenario 2: Copying content with entities

  • Chrome/Edge: May copy as entities or decoded characters
  • Firefox: Similar behavior
  • Safari: Encoding in clipboard varies

Scenario 3: Pasting content with entities

  • Chrome/Edge: May decode entities or preserve them
  • Firefox: Similar behavior
  • Safari: Entity handling inconsistent

Scenario 4: Serializing content (innerHTML)

  • Chrome/Edge: May encode or decode entities
  • Firefox: Similar behavior
  • Safari: Encoding behavior varies

Impact

  • Inconsistent character representation
  • Issues when copying/pasting special characters
  • Problems with content serialization
  • Difficulty maintaining exact character representation

Browser Comparison

  • Chrome/Edge: Generally more consistent encoding
  • Firefox: May encode/decode differently
  • Safari: Most inconsistent behavior

Workaround

Normalize entity encoding:

function normalizeEntities(element) {
  const walker = document.createTreeWalker(
    element,
    NodeFilter.SHOW_TEXT,
    null
  );
  
  let node;
  while (node = walker.nextNode()) {
    // Decode common entities to actual characters
    node.textContent = node.textContent
      .replace(/&lt;/g, '<')
      .replace(/&gt;/g, '>')
      .replace(/&amp;/g, '&')
      .replace(/&quot;/g, '"')
      .replace(/&#39;/g, "'");
  }
}

// Normalize on input
element.addEventListener('input', () => {
  normalizeEntities(element);
});

// Handle paste
element.addEventListener('paste', (e) => {
  e.preventDefault();
  const text = e.clipboardData.getData('text/plain');
  const html = e.clipboardData.getData('text/html');
  
  const selection = window.getSelection();
  if (selection.rangeCount > 0) {
    const range = selection.getRangeAt(0);
    range.deleteContents();
    
    if (html) {
      // Decode entities in HTML
      const temp = document.createElement('div');
      temp.innerHTML = html;
      normalizeEntities(temp);
      range.insertNode(document.createRange().createContextualFragment(temp.innerHTML));
    } else {
      range.insertNode(document.createTextNode(text));
    }
  }
});

References

Scenario flow

Visual view of how this scenario connects to its concrete cases and environments. Nodes can be dragged and clicked.

React Flow mini map

Variants

Each row is a concrete case for this scenario, with a dedicated document and playground.

Case OS Device Browser Keyboard Status
ce-0123-html-entities-paste Windows 11 Desktop or Laptop Any Chrome 120.0 US draft
ce-0132-html-entities-copy Windows 11 Desktop or Laptop Any Chrome 120.0 US draft
ce-0162-html-entities-serialization Windows 11 Desktop or Laptop Any Chrome 120.0 US draft

Browser compatibility

This matrix shows which browser and OS combinations have documented cases for this scenario. Click on a cell to view the specific case.

Confirmed
Draft
No case documented

Cases

Open a case to see the detailed description and its dedicated playground.

Related Scenarios

Other scenarios that share similar tags or category.

Category: formatting

Background color changes behave inconsistently

Changing background color (highlighting) in contenteditable elements behaves inconsistently across browsers. Background colors may be applied as inline styles, may not persist when typing, or may interfere with text selection. The behavior differs from text color changes.

3 cases
Category: formatting

Blockquote editing behavior varies across browsers

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.

4 cases
Category: formatting

Browser auto-formatting interferes with contenteditable editing

Browsers may automatically format text in contenteditable elements, such as converting URLs to links, capitalizing text, formatting numbers, or applying other transformations. This auto-formatting can interfere with editing, cause cursor positioning issues, and create unwanted markup or style changes.

0 cases
Category: formatting

Browser automatically converts URLs and emails to links in contenteditable

Browsers, especially Internet Explorer and legacy Edge, automatically detect URLs, email addresses, and phone numbers in contenteditable elements and convert them to clickable links. This auto-linking behavior can interfere with editing, cause cursor positioning issues, and create unwanted markup.

0 cases
Category: formatting

Code block editing behavior varies across browsers

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.

4 cases

Comments & Discussion

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