From adb0fb24d596f84fa92cc132eff8908a2b9eb4f2 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Mon, 30 Nov 2026 20:08:05 -0800 Subject: [PATCH] Clamp both Lorem Ipsum selection offsets --- crates/engine-core/src/cmd/type_extra_cmds.rs | 5 +++-- .../src/cmd/type_paste_lorem_selection_tests.rs | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/engine-core/src/cmd/type_extra_cmds.rs b/crates/engine-core/src/cmd/type_extra_cmds.rs index 7dea7d2d72..ad11a954a5 100644 --- a/crates/engine-core/src/cmd/type_extra_cmds.rs +++ b/crates/engine-core/src/cmd/type_extra_cmds.rs @@ -805,8 +805,9 @@ fn paste_lorem(s: &mut Session, p: &Value) -> Result { // existing creation step; the first edit of an existing layer gets the type-edit label. let label = if range_end.is_some() && p.get("coalesce").and_then(Value::as_str).is_some() { "Edit Type Layer" } else { "Paste Lorem Ipsum" }; let caret = with_text(s, p, label, |t, _| { - let at = p.get("at").and_then(Value::as_u64).map_or(t.text.len(), |c| byte_index(&t.text, c as usize)); - let end = range_end.map_or(at, |c| byte_index(&t.text, usize::try_from(c).unwrap_or(usize::MAX))); + let to_byte = |c| byte_index(&t.text, usize::try_from(c).unwrap_or(usize::MAX)); + let at = p.get("at").and_then(Value::as_u64).map_or(t.text.len(), to_byte); + let end = range_end.map_or(at, to_byte); let start = at.min(end); replace_text(t, start, at.max(end), LOREM_IPSUM); Ok(char_index(&t.text, start + LOREM_IPSUM.len())) diff --git a/crates/engine-core/src/cmd/type_paste_lorem_selection_tests.rs b/crates/engine-core/src/cmd/type_paste_lorem_selection_tests.rs index ded602629d..339b2a3a60 100644 --- a/crates/engine-core/src/cmd/type_paste_lorem_selection_tests.rs +++ b/crates/engine-core/src/cmd/type_paste_lorem_selection_tests.rs @@ -42,3 +42,16 @@ fn paste_lorem_replaces_a_new_layers_placeholder_in_its_creation_step() { assert!(session.undo()); assert!(session.active().unwrap().doc.layer(id).is_none()); } + +#[test] +fn paste_lorem_clamps_both_oversized_character_offsets() { + let mut session = crate::session(); + session.execute("file.new", json!({"width": 300, "height": 120})).unwrap(); + let layer = session.execute("type.create", json!({"x": 20, "y": 70, "text": "é猫Ω"})).unwrap()["layer"].as_u64().unwrap(); + let beyond_wasm32 = u64::from(u32::MAX) + 1; + + let result = session.execute("type.pasteLoremIpsum", json!({"layer": layer, "at": beyond_wasm32, "end": beyond_wasm32})).unwrap(); + + assert_eq!(result["caret"], 3 + LOREM_IPSUM.chars().count()); + assert_eq!(session.execute("type.info", json!({"layer": layer})).unwrap()["text"], format!("é猫Ω{LOREM_IPSUM}")); +}