Scenario

Browser translation breaks contenteditable editing

When browser translation features (like Google Translate) are activated, they manipulate the DOM by replacing text content and injecting elements. This can break contenteditable functionality, causing cursor positioning issues, event handling problems, and IME composition failures.

other
Scenario ID
scenario-browser-translation

Details

When browser translation features (like Google Translate) are activated, they manipulate the DOM by replacing text content and injecting elements. This can break contenteditable functionality, causing cursor positioning issues, event handling problems, and IME composition failures.

Observed Behavior

  • DOM manipulation: Translation replaces node text content and injects <span> or <div> elements
  • Cursor positioning: Cursor won’t move or appears at wrong position after translation
  • Input failures: Pasted content doesn’t insert correctly, key events act incorrectly
  • IME composition: Translation resets or interferes with composition buffers
  • Event listeners: Event subscriptions may be detached during DOM manipulation
  • Selection issues: Selection and caret behavior becomes unreliable

Browser Comparison

  • Chrome: Google Translate integration most likely to cause issues
  • Edge: Similar translation features with similar problems
  • Firefox: Less affected but still has issues
  • Safari: Native translation features may cause different issues

Impact

  • Editing becomes impossible: Users cannot edit content after translation
  • Data loss risk: Translation may corrupt content structure
  • IME failures: Composition input becomes unreliable
  • Poor user experience: Users must disable translation to edit

Workarounds

1. Disable Translation for Specific Elements

Use translate="no" attribute:

<div contenteditable="true" translate="no">
  Editable content that should not be translated
</div>

2. Use “notranslate” Class

Google Translate respects this class:

<div contenteditable="true" class="notranslate">
  Editable content
</div>

3. Reapply contenteditable After Translation

Use MutationObserver to restore attributes:

const observer = new MutationObserver((mutations) => {
  for (const mutation of mutations) {
    if (mutation.type === 'attributes' && 
        mutation.attributeName === 'contenteditable') {
      // Reapply contenteditable if removed
      if (!mutation.target.hasAttribute('contenteditable')) {
        mutation.target.setAttribute('contenteditable', 'true');
      }
    }
  }
});

observer.observe(editableElement, {
  attributes: true,
  attributeFilter: ['contenteditable']
});

4. Disable Translation During Editing

Detect editing mode and disable translation:

let isEditing = false;

editableElement.addEventListener('focus', () => {
  isEditing = true;
  document.documentElement.setAttribute('translate', 'no');
});

editableElement.addEventListener('blur', () => {
  isEditing = false;
  document.documentElement.removeAttribute('translate');
});

5. Use Controlled Editor Components

Use robust editor libraries that handle DOM mutations better:

  • Quill: Handles DOM mutations more robustly
  • ProseMirror: Better at detecting and handling translation changes
  • Slate: More resilient to external DOM manipulation

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-0562-browser-translation-breaks-editing Any Any Desktop or Laptop Any Chrome Latest US draft

Cases

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

Related Scenarios

Other scenarios that share similar tags or category.

Tags: ime, composition

beforeinput and input events have different inputType values

During IME composition or in certain browser/IME combinations, the beforeinput event may have a different inputType than the corresponding input event. For example, beforeinput may fire with insertCompositionText while input fires with deleteContentBackward. This mismatch can cause handlers to misinterpret the actual DOM change and requires storing beforeinput's targetRanges for use in input event handling.

1 case
Tags: ime, composition

Selection mismatch between beforeinput and input events

The selection (window.getSelection()) in beforeinput events can differ from the selection in corresponding input events. This mismatch can occur during IME composition, text prediction, or when typing adjacent to formatted elements like links. The selection in beforeinput may include adjacent formatted text, while input selection reflects the final cursor position.

1 case
Tags: ime, composition

getTargetRanges() returns empty array in beforeinput events

The getTargetRanges() method in beforeinput events may return an empty array or undefined in various scenarios, including text prediction, certain IME compositions, or specific browser/device combinations. When getTargetRanges() is unavailable, developers must rely on window.getSelection() as a fallback, but this may be less accurate.

1 case
Tags: ime, composition

IME Composition State Management System

Comprehensive system for managing IME (Input Method Editor) composition state across different browsers and IME implementations, including state tracking, event normalization, and cross-platform consistency.

0 cases

Comments & Discussion

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