Skip to content

Reject selection offsets inside UTF-16 surrogate pairs before editor transforms #2

Open

Description

@LunaStev

Problem

normalizeSelection validates integer ranges but accepts an offset between the two UTF-16 code units of a supplementary Unicode character. transformSelection then slices at that offset and inserts Markdown delimiters between the surrogate halves, turning well-formed text into a string containing unpaired surrogates.

The same accepted offset is not preserved by the browser/native conversion helpers. This makes an accepted programmatic selection behave differently between the JavaScript and Unicode-character-based adapters.

Code references:

Reproduction

For A😀B, browser offsets 1 and 3 surround the emoji; offset 2 is inside its surrogate pair.

const content = 'A😀B'
const selection = normalizeSelection(content, 2, 2)
const transformed = transformSelection(content, 2, 2, 'bold')
const nativeOffset = browserOffsetToUnicode(content, 2)
const restoredOffset = unicodeOffsetToBrowser(content, nativeOffset)

A focused execution of the core functions produces:

selection:                    { start: 2, end: 2 }
JSON.stringify(result.content): "A\ud83d****\ude00B"
result.content.isWellFormed(): false
offset conversion round trip: 2 -> 2 -> 3

This is an accepted programmatic API input; the report does not depend on ordinary keyboard navigation placing a browser caret inside an emoji.

Expected behavior and scope

Reject a selection endpoint inside a valid high-surrogate/low-surrogate pair before transforming text or converting it to native coordinates. Use the existing invalid-selection error convention and document this boundary requirement while keeping public browser offsets measured in UTF-16 code units.

Do not silently shift the selection to a different location. This issue concerns Unicode scalar boundaries, not grapheme-cluster navigation, combining-mark selection, or an editor-wide text-normalization policy.

Completion criteria

  • Both start and end endpoints reject the interior of a surrogate pair, including collapsed and non-collapsed selections.
  • Transform and browser-to-native conversion entry points enforce the same boundary rule.
  • Boundaries immediately before and after an emoji remain valid; formatting a complete emoji preserves its exact text.
  • Every accepted browser offset in fixtures round-trips through the native conversion helpers without changing position.
  • Cover ASCII, BMP non-ASCII text, supplementary characters at the beginning/middle/end, adjacent supplementary characters, empty text, and existing invalid range cases.
  • A rejected operation leaves document content, selection, and undo/redo history unchanged.
  • Add core contract tests and run the editor package tests without requiring a native Wave service.

Difficulty: medium.

Activity

  1. added
    bug
    help wanted
    on Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions