Recursive AI

Edit text in a rendered HTML form in C#

OneRender is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Edit an HTML input without a browser; the host supplies input and painting.

Terminal
dotnet add package RecursiveAI.OneRender.Image

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneRender.Image;
using OneRender;
using OneRender.Interaction;
 
var ui = InteractivePage.Create(
    "<input id=name value=Ada>",
    new HtmlPageOptions(FontProviders.CreateDefault()));
var field = ui.Page.Document.GetElementById("name");
ui.Focus(field);
ui.SetSelection(field, 3, 3);
ui.InsertText(" Lovelace");
Console.WriteLine(ui.GetValue(field));
Text
Ada Lovelace

Unzip the example, then dotnet run.

Full program

C#
using OneRender.Image;
using OneRender;
using OneRender.Interaction;
 
var ui = InteractivePage.Create(
    "<input id=name value=Ada>",
    new HtmlPageOptions(FontProviders.CreateDefault()));
var field = ui.Page.Document.GetElementById("name");
ui.Focus(field);
ui.SetSelection(field, 3, 3);
ui.InsertText(" Lovelace");
Console.WriteLine(ui.GetValue(field));
ui.HandleKey(new KeyInput { Key = "ArrowLeft", Down = true });
ui.HandleKey(new KeyInput { Key = "ArrowLeft", Down = true,
    Modifiers = KeyModifiers.Shift });
Console.WriteLine($"Selected: {ui.GetSelectedText()}");
var painter = new OneRender.Painting.Painter();
var list = new OneRender.Painting.DisplayList();
painter.Paint(ui.Page.Fragments, list);
Console.WriteLine($"Selection frame items: {list.Count}");
ui.HandleKey(new KeyInput { Key = "Backspace", Down = true });
Console.WriteLine($"After deletion: {ui.GetValue(field)}");
Console.WriteLine($"Caret available: {ui.TryGetCaretRect(out _)}");
Text
Ada Lovelace
Selected: c
Selection frame items: 7
After deletion: Ada Lovelae
Caret available: True

The full program moves left, selects one character with Shift+Left, paints the selection frame and deletes the selection. Read-only controls allow selection and copy but not edits; disabled controls do not accept editing.

Variations

Commit IME text. After the snippet at the top, select the value and supply preedit, then committed text:

C#
ui.SelectAll();
ui.CompositionStart();
ui.CompositionUpdate("Grace");
Console.WriteLine($"During preedit: {ui.GetValue(field)}");
ui.CompositionEnd("Grace");
Console.WriteLine($"Committed: {ui.GetValue(field)}");
Text
During preedit: Ada Lovelace
Committed: Grace

Hit testing and events · Dark-mode form controls


The short answer

Create an InteractivePage, find the input node, focus it, set the selection and call InsertText. Read the current editing value through GetValue, not by assuming the HTML value attribute is a live editing buffer. Feed movement and deletion through HandleKey when you want the editor’s keyboard behavior.

OneRender owns the editing session and the rendered control. The host supplies keyboard, pointer, clipboard and composition input. No browser or page script is required for this example. See the text-editing contract for the supported controls and event sequence.

How do I edit text with HTML?

For a headless rendered UI, first decide whether you want to replace a value or perform an edit at a caret. Setting a value and inserting text are different operations. InsertText uses the active control’s current selection: it inserts at a collapsed caret or replaces the selected range.

The snippet focuses the field by script, moves the caret to the end of “Ada” and inserts a surname. Script focus alone puts the caret at the beginning, so omitting the explicit selection would change where the new text goes. The full program then moves left, extends the selection left with Shift and deletes the selected character.

Text-like inputs and textareas use the editing engine. Newlines are normalized, and single-line fields discard them. A number field filters its text to the documented number characters. These are control-specific behaviors, not a general rich-text document editor. The page does not provide file, color or date picker dialogs.

How to use selection in HTML?

Use SetSelection for an explicit range, or feed keys and pointer input for interactive selection. The full program prints the selected substring before deletion, then paints the fragment tree into a display list while the selection is present. The list is what a rendering backend draws.

Caret, selection and composition underlines are ordinary FillRect paint items with stable IDs, inside the field’s clip and scrolling transform. There is no separate operating-system text box hiding behind the rendered page. TryGetCaretRect gives the host page-coordinate geometry when it needs to position another UI element.

Arrow movement is by grapheme cluster, and Shift extends selection. In mixed-direction text, caret movement and selection follow logical order, not visual order. Account for that limit before choosing this editor for a bidirectional-text interface. The displayed list count in the example describes that small selection frame; it is not a fixed count for every field.

How to make input text not editable in HTML?

Use readonly for a field that still allows selection and copy, or disabled for a control that should not accept interaction. These states are not interchangeable. The editor respects them when applying changes, rather than expecting the host to filter every key itself.

For an input method editor, the host sends CompositionStart, one or more CompositionUpdate calls, then CompositionEnd with committed text. Preedit text is drawn but does not replace the committed value yet. The variation prints the unchanged value during preedit and the replacement after commit. An empty commit cancels composition.

Keys during composition are ignored by the editor. Supply composition events from the host’s input system rather than treating every intermediate candidate as a permanent insertion. The host also supplies time for caret blinking and an ITextClipboard for clipboard operations; the library does not reach into the system clipboard on its own.

All onerender recipes