Clamp both Lorem Ipsum selection offsets

This commit is contained in:
yoriswind committed 2026-11-30 20:08:05 -08:00
1 parent 5261f3696d
commit adb0fb24d5
2 files changed
+16 -2

No files matched your search

@@ -805,8 +805,9 @@ fn paste_lorem(s: &mut Session, p: &Value) -> Result<Value> {
// 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()))
@@ -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}"));
}