Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Reject invalid story.replaceRange offsets with an error (#1267)

MarekSpodBiedry251ceda1e018
diff --git a/crates/engine/src/cmd/text.rs b/crates/engine/src/cmd/text.rs
index 24ff0384a..32cbfd0f6 100644
--- a/crates/engine/src/cmd/text.rs
+++ b/crates/engine/src/cmd/text.rs
@@ -130,7 +130,11 @@ pub fn specs() -> Vec<CommandSpec> {
let text = super::text_param(p, "text");
s.edit(|d, sel| {
let st = d.story_mut(sid).ok_or(designcraft_doc::DocError::NoStory(sid))?;
- let (a, b) = (a.min(st.len()), b.min(st.len()).max(a.min(st.len())));
+ let n = st.len();
+ if a > b || b > n || !st.text.is_char_boundary(a) || !st.text.is_char_boundary(b) {
+ let msg = format!("range {a}..{b} must be UTF-8 byte offsets on character boundaries, start <= end; the story is {n} bytes");
+ return Err(bad("story.replaceRange", msg));
+ }
st.replace(a..b, &text);
let len = st.len();
if let Some(t) = sel.text.as_mut().filter(|t| t.story == sid) {
@@ -2292,9 +2296,14 @@ mod open_type_tests {
s.execute("file.new", &json!({})).unwrap();
let r = s.execute("frame.create", &json!({"rect": [0, 0, 200, 100], "content": "text", "text": "é漢🙂x"})).unwrap();
let sid = r["story"].clone();
- // Offsets inside 漢 (2..5) and 🙂 (5..9) snap back to the character starts: 漢 is replaced.
- s.execute("story.replaceRange", &json!({"story": sid, "start": 3, "end": 6, "text": "字"})).unwrap();
let story = designcraft_doc::StoryId(sid.as_u64().unwrap());
+ // Offsets inside 漢 (2..5) and 🙂 (5..9), past the end, or reversed are refused and change nothing.
+ for (start, end) in [(3, 6), (2, 4), (0, 99), (5, 2)] {
+ let r = s.execute("story.replaceRange", &json!({"story": sid, "start": start, "end": end, "text": "字"}));
+ assert!(r.is_err_and(|e| e.to_string().contains("10 bytes")), "{start}..{end}");
+ assert_eq!(s.doc().unwrap().doc.story(story).unwrap().text, "é漢🙂x");
+ }
+ s.execute("story.replaceRange", &json!({"story": sid, "start": 2, "end": 5, "text": "字"})).unwrap();
assert_eq!(s.doc().unwrap().doc.story(story).unwrap().text, "é字🙂x");
// A caret before 🙂 (byte 5) would land inside it once é (2 bytes) is gone.
s.execute("text.select", &json!({"story": sid, "anchor": 5})).unwrap();
diff --git a/docs/mcp.md b/docs/mcp.md
index bba394e33..83e8bdf32 100644
--- a/docs/mcp.md
+++ b/docs/mcp.md
@@ -137,7 +137,9 @@ Positions in a story's text are **UTF-8 byte offsets**, not character counts: `a
`start` / `end` of `story.replaceRange` and of `find.find` matches, the `pos` that `text.placeCaret`, `text.insert`
and `text.move` return, a story's `length` in `get_story` and `inspect_document`, and its `overset` position in
`get_story`. A character outside ASCII takes 2 to 4 bytes (é 2, — and は 3), so the story `はただ商店` has length 15
-and its second character starts at 3. An offset inside a character snaps back to the start of that character.
+and its second character starts at 3. An offset inside a character snaps back to the start of that character, except
+in `story.replaceRange`: there an offset inside a character, an `end` past the end of the story or a `start` after `end`
+is an error that gives the story length in bytes, and the story is left unchanged.
## Text and image fitting