Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(ui): preserve surrogate pairs when clamping host input

yhh46fadd88603f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c6c5ad330..410555c7a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Text-input length limits preserve complete supplementary characters.** Oversized host input still uses UTF-16 length limits, but truncation backs off when the boundary would split a valid surrogate pair. The corrected value and selection return to the editing surface together, preventing malformed emoji in committed input while preserving BMP limits and existing cursor semantics.
+
- **Text fields read only the editing surface they actually own.** Newly focused fields retain their committed display until attached, and read-only fields never borrow a previous field's transient value. Switching a live field to read-only detaches the OS surface and hides its caret; restoring editability reattaches the committed value without replaying rejected input.
- **Grid path searches reuse their working memory.** Repeated queries reuse per-grid score, parent, visitation and heap storage instead of allocating map-sized buffers each time. Search stamps isolate queries, nested searches receive separate storage, exceptions release leases, and returned routes remain caller-owned. Weighted paths, snapping and best-effort endpoints retain their behavior.
diff --git a/sdk/src/ui/text/text-input-plugin.ts b/sdk/src/ui/text/text-input-plugin.ts
index 9cc071aa0..d532f948e 100644
--- a/sdk/src/ui/text/text-input-plugin.ts
+++ b/sdk/src/ui/text/text-input-plugin.ts
@@ -193,6 +193,13 @@ export class TextInputPlugin implements Plugin {
let val = state.value;
if (ti.maxLength > 0 && val.length > ti.maxLength) {
val = val.substring(0, ti.maxLength);
+ // maxLength counts UTF-16 units, but a cut between a surrogate
+ // pair must reject the whole character rather than store half.
+ const tail = val.charCodeAt(val.length - 1);
+ const next = state.value.charCodeAt(val.length);
+ if (tail >= 0xd800 && tail <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) {
+ val = val.slice(0, -1);
+ }
editor!.write({ ...state, value: val, selectionStart: val.length, selectionEnd: val.length });
}
diff --git a/sdk/tests/text-input-plugin-lifecycle.test.ts b/sdk/tests/text-input-plugin-lifecycle.test.ts
index 0da5352ca..8ad986c9c 100644
--- a/sdk/tests/text-input-plugin-lifecycle.test.ts
+++ b/sdk/tests/text-input-plugin-lifecycle.test.ts
@@ -282,3 +282,22 @@ it('renders a newly focused field from its value until the surface is attached',
await app.tick(1 / 60);
expect(app.world.getEntitiesWithComponents([Text]).map(e => app.world.get(e, Text).content)).toEqual(['typed']);
});
+
+it.each([
+ { value: '🙂', maxLength: 1, expected: '' },
+ { value: 'a🙂b', maxLength: 2, expected: 'a' },
+ { value: 'a🙂b', maxLength: 3, expected: 'a🙂' },
+ { value: '中文', maxLength: 1, expected: '中' },
+ { value: '\ud83dX', maxLength: 1, expected: '\ud83d' },
+])('clamps oversized host input at complete UTF-16 characters: $maxLength of $value', async ({ value, maxLength, expected }) => {
+ const app = build(new TextInputPlugin());
+ const field = app.world.spawn();
+ app.world.insert(field, TextInput, { value: '', maxLength });
+ app.getResource(FocusManager).focus(field);
+ await app.tick(1 / 60);
+ vi.spyOn(surfaces[0], 'read').mockReturnValue({ value, selectionStart: value.length, selectionEnd: value.length, backward: false });
+ for (const listener of surfaces[0].listeners) listener({ kind: 'change' });
+ expect(app.world.get(field, TextInput).value).toBe(expected);
+ expect(app.world.get(field, TextInput).cursorPos).toBe(expected.length);
+ expect(surfaces[0].write).toHaveBeenCalledWith({ value: expected, selectionStart: expected.length, selectionEnd: expected.length, backward: false });
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index bf937be54..277cdb84e 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'fix(ui): preserve surrogate pairs when clamping host input':
+ { note: 'Text-input length limits preserve complete supplementary characters.' },
'fix(ui): bind field rendering to the active editing owner':
{ note: 'Text fields read only the editing surface they actually own.' },
'feat(editor): inspect live agent route failures on demand':