Merge pull request #1097 from Silvertree2010/fix/h1120-text-set-keeps-a-stray-carriage

This commit is contained in:
Brandon Thomas committed 2026-10-23 17:22:12 -04:00
commit a7876aab54
2 files changed
+18

No files matched your search

+17
View File
@@ -94,6 +94,23 @@ fn text_editing_flow() {
assert_eq!(s.doc().unwrap().selection.shapes.len(), 1);
}
/// `text.set` treats CRLF (or a lone CR) as one paragraph break and keeps no `\r` in the text.
#[test]
fn text_set_crlf_makes_clean_paragraphs() {
let mut s = session();
let r = s.execute("insert.textBox", &json!({"text": "x"})).unwrap();
let id = r["id"].as_u64().unwrap();
s.execute("text.set", &json!({"id": id, "text": "one\r\ntwo"})).unwrap();
let t = s.execute("text.get", &json!({"id": id})).unwrap();
assert_eq!(t["paragraphs"][0]["text"], "one");
assert_eq!(t["paragraphs"][1]["text"], "two");
assert_eq!(t["text"], "one\ntwo");
// A lone CR (old Mac line ends) is a paragraph break too, as `edit::insert` treats it.
s.execute("text.set", &json!({"id": id, "text": "a\rb"})).unwrap();
let t = s.execute("text.get", &json!({"id": id})).unwrap();
assert_eq!(t["text"], "a\nb");
}
/// Cut with only a caret copies nothing, so it must not delete the character after the caret.
#[test]
fn cut_with_only_a_caret_changes_nothing() {
+1
View File
@@ -603,6 +603,7 @@ pub struct TextBody {
impl TextBody {
pub fn from_text(text: &str) -> Self {
let text = text.replace("\r\n", "\n").replace('\r', "\n");
TextBody { paragraphs: text.split('\n').map(Paragraph::new).collect(), ..Default::default() }
}
/// Plain text with `\n` between paragraphs.