@@ -2221,6 +2221,13 @@ fn convert_doc_to_bytes(name: &str, doc: &serde_json::Value) -> Result<Vec<u8>, |
2221 | 2221 | /// dropping it. The pre-#1306 version handled only top-level |
2222 | 2222 | /// paragraphs/headings and silently deleted everything else on save. |
2223 | 2223 | /// |
| 2224 | +/// Line endings: plain-mode load (`plain_text_to_prosemirror`) strips |
| 2225 | +/// the CR of CRLF endings and this join emits LF only, so a CRLF file |
| 2226 | +/// is normalized to LF on save - a deliberate, documented choice |
| 2227 | +/// (#1307), pinned by the round-trip tests below; the file's |
| 2228 | +/// trailing-newline state is preserved through the load/save pair. |
| 2229 | +/// Byte-exact CRLF round-trips remain available in Raw mode. |
| 2230 | +/// |
2224 | 2231 | /// Inline atoms carry no `content`, so recursion alone loses them. |
2225 | 2232 | /// Pinned representations (#1491): an `image` contributes its `alt` |
2226 | 2233 | /// text verbatim when non-empty, otherwise its `src` verbatim when |
@@ -3332,6 +3339,40 @@ mod tests { |
3332 | 3339 | serde_json::json!({"type": "paragraph", "content": [{"type": "text", "text": text}]}) |
3333 | 3340 | } |
3334 | 3341 |
|
| 3342 | + #[test] |
| 3343 | + fn test_plain_mode_roundtrip_preserves_trailing_newline() { |
| 3344 | + // #1307 pinned policy: the real load/save pair is byte-stable |
| 3345 | + // for the common shapes, including the trailing newline. |
| 3346 | + for text in ["a\nb\n", "a\nb", "a\n", "\n", ""] { |
| 3347 | + let doc = super::super::markdown::plain_text_to_prosemirror(text); |
| 3348 | + assert_eq!(extract_plain_text(&doc), text, "{text:?}"); |
| 3349 | + } |
| 3350 | + } |
| 3351 | + |
| 3352 | + #[test] |
| 3353 | + fn test_plain_mode_roundtrip_normalizes_crlf_to_lf() { |
| 3354 | + // #1307 pinned policy: CRLF normalizes to LF on save, and the |
| 3355 | + // trailing-newline state survives the normalization. |
| 3356 | + let doc = super::super::markdown::plain_text_to_prosemirror("alpha\r\nbeta\r\n"); |
| 3357 | + assert_eq!(extract_plain_text(&doc), "alpha\nbeta\n"); |
| 3358 | + let doc = super::super::markdown::plain_text_to_prosemirror("alpha\r\nbeta"); |
| 3359 | + assert_eq!(extract_plain_text(&doc), "alpha\nbeta"); |
| 3360 | + } |
| 3361 | + |
| 3362 | + #[test] |
| 3363 | + fn test_plain_mode_roundtrip_preserves_lone_terminal_cr() { |
| 3364 | + // Codex finding on PR #4613: a carriage return that is NOT part |
| 3365 | + // of a CRLF ending is content. Only segments terminated by '\n' |
| 3366 | + // may have their CR stripped; the final segment keeps it. |
| 3367 | + let doc = super::super::markdown::plain_text_to_prosemirror("alpha\r"); |
| 3368 | + assert_eq!(extract_plain_text(&doc), "alpha\r"); |
| 3369 | + let doc = super::super::markdown::plain_text_to_prosemirror("alpha\r\nbeta\r"); |
| 3370 | + assert_eq!(extract_plain_text(&doc), "alpha\nbeta\r"); |
| 3371 | + // A CR-only line ending mid-file is likewise preserved. |
| 3372 | + let doc = super::super::markdown::plain_text_to_prosemirror("alpha\rbeta\n"); |
| 3373 | + assert_eq!(extract_plain_text(&doc), "alpha\rbeta\n"); |
| 3374 | + } |
| 3375 | + |
3335 | 3376 | #[test] |
3336 | 3377 | fn test_extract_plain_text_paragraph_heading_byte_identical() { |
3337 | 3378 | // Pre-#1306 behaviour for paragraph/heading-only docs is unchanged. |
|