# Editing and selection

Select a word, replace it, and delete one visible character without splitting a combining sequence. `TextEditingState` lets you exercise those operations without a native window.

```csharp
using Xui.Core.UI.Input;

public static class EditingRecipe
{
    public static string ReplaceWord()
    {
        var editor = new TextEditingState
        {
            Text = "Hello, Gigi"
        };
        editor.Selection = new TextRange(7, 11);
        editor.Insert("Xui");
        return editor.Text; // Hello, Xui
    }

    public static string DeleteOneGrapheme()
    {
        var editor = new TextEditingState
        {
            Text = "Cafe\u0301"
        };
        editor.DeleteBackward();
        return editor.Text; // Caf: e + the combining accent are removed together.
    }
}
```

`ReplaceWord()` returns `Hello, Xui`. `DeleteOneGrapheme()` returns `Caf`: the final `e` and its combining accent form one grapheme and disappear together. Call these methods from an ordinary test to check the behavior without keyboard automation.

## Use ranges with an exclusive end

`TextRange(Start, End)` uses UTF-16 offsets. `Start == End` represents a caret; a nonempty interval includes Start and excludes End. `Editor.Selection` normalizes endpoints to the document and grapheme boundaries. A nonempty range that cuts through a grapheme expands to include it.

The editor offers `Previous`, `Next`, and `Boundary` for grapheme movement. Do not increment a caret by one code unit and assume you moved one visible character. Emoji, surrogate pairs, and combining sequences make that assumption fail.

`Insert` replaces the selection, or the current marked composition range. `Replace` targets an explicit range. `DeleteBackward` and `DeleteForward` delete the selection or the adjacent grapheme. Whole `Text` assignment ends composition and moves the caret to the end; repeatedly assigning an unchanged model string during typing can therefore disrupt editing.

## Separate document and selection changes

`Changing` runs while old state is still readable. `Changed` runs after the transaction. The flags identify text and selection changes; one transaction can include both. Keep handlers short and avoid writing back an equivalent value unconditionally, which can create unnecessary edit cycles.

Core `TextBox.Selection` also exposes a `uint` interval for compatibility. Prefer the shared `Editor.Selection`/`TextRange` API when writing logic that should work with both `TextBox` and ToolKit `TextInput`.

## Be explicit about missing editor features

This state is a single-line text model, not an undo history or rich-text document. It rejects control characters in committed replacement input. Add a deliberate undo model if your application needs one; do not assume repeated `Changed` notifications provide native undo grouping.

The shared `TextInputLayer` handles caret movement, deletion, and Shift selection, but it does not provide a portable clipboard command service. Native iOS/Android editing adapters and browser text input have different behavior. Verify cut, copy, paste, selection handles, and composed Unicode on each target rather than claiming them from the interface alone.
