diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index dd64b08c..7b21ad26 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -94,6 +94,19 @@ fn text_editing_flow() { assert_eq!(s.doc().unwrap().selection.shapes.len(), 1); } +/// `text.set` treats CRLF 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"); +} + /// 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() { diff --git a/crates/model/src/text.rs b/crates/model/src/text.rs index b317d3d9..4b018594 100644 --- a/crates/model/src/text.rs +++ b/crates/model/src/text.rs @@ -603,6 +603,7 @@ pub struct TextBody { impl TextBody { pub fn from_text(text: &str) -> Self { + let text = text.replace("\r\n", "\n"); TextBody { paragraphs: text.split('\n').map(Paragraph::new).collect(), ..Default::default() } } /// Plain text with `\n` between paragraphs.