text-document-io 1.9.3

Import/export for text-document: plain text, Markdown, HTML, LaTeX, DOCX
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Import/export tests for tables and lists nested inside blockquotes.
//!
//! Regression coverage for the blockquote-depth gap: tables used to lose
//! their blockquote context during import (the frame stack was driven only
//! by block elements), so `> | a | b |` landed in the root frame and a
//! table *after* a quote was swallowed into it.

extern crate text_document_io as document_io;
use anyhow::Result;
use common::long_operation::{LongOperationManager, OperationStatus};
use common::types::EntityId;

use test_harness::{frame_controller, get_frame_id, get_table_ids, setup};

use document_io::document_io_controller;
use document_io::*;

fn wait_for_long_operation(long_op_manager: &LongOperationManager, op_id: &str) {
    while let Some(OperationStatus::Running) = long_op_manager.get_operation_status(op_id) {
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
}

fn import_markdown(
    db: &test_harness::DbContext,
    ev: &std::sync::Arc<test_harness::EventHub>,
    md: &str,
) -> Result<()> {
    let mut long_op_manager = LongOperationManager::new();
    let op_id = document_io_controller::import_markdown(
        db,
        ev,
        &mut long_op_manager,
        &ImportMarkdownDto {
            markdown_text: md.to_string(),
        },
    )?;
    wait_for_long_operation(&long_op_manager, &op_id);
    assert_eq!(
        long_op_manager.get_operation_status(&op_id),
        Some(OperationStatus::Completed),
        "markdown import did not complete"
    );
    Ok(())
}

fn import_html(
    db: &test_harness::DbContext,
    ev: &std::sync::Arc<test_harness::EventHub>,
    html: &str,
) -> Result<()> {
    let mut long_op_manager = LongOperationManager::new();
    let op_id = document_io_controller::import_html(
        db,
        ev,
        &mut long_op_manager,
        &ImportHtmlDto {
            html_text: html.to_string(),
        },
    )?;
    wait_for_long_operation(&long_op_manager, &op_id);
    assert_eq!(
        long_op_manager.get_operation_status(&op_id),
        Some(OperationStatus::Completed),
        "html import did not complete"
    );
    Ok(())
}

/// Sub-frame ids referenced by `child_order` (negative entries), in order.
fn sub_frame_ids(frame: &test_harness::FrameDto) -> Vec<EntityId> {
    frame
        .child_order
        .iter()
        .filter(|&&e| e < 0)
        .map(|&e| (-e) as EntityId)
        .collect()
}

/// Block ids referenced by `child_order` (positive entries), in order.
fn block_entries(frame: &test_harness::FrameDto) -> Vec<EntityId> {
    frame
        .child_order
        .iter()
        .filter(|&&e| e > 0)
        .map(|&e| e as EntityId)
        .collect()
}

fn get_frame(db: &test_harness::DbContext, id: EntityId) -> test_harness::FrameDto {
    frame_controller::get(db, &id)
        .expect("frame_controller::get failed")
        .expect("frame not found")
}

// ─── Markdown import ────────────────────────────────────────────────

#[test]
fn test_md_import_table_first_in_blockquote() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_markdown(&db, &ev, "> | a | b |\n> |---|---|\n> | c | d |")?;

    let root = get_frame(&db, get_frame_id(&db)?);
    let root_subs = sub_frame_ids(&root);
    assert_eq!(
        root_subs.len(),
        1,
        "root should contain exactly the blockquote sub-frame, child_order: {:?}",
        root.child_order
    );
    assert!(block_entries(&root).is_empty(), "no loose blocks at root");

    let bq = get_frame(&db, root_subs[0]);
    assert_eq!(
        bq.fmt_is_blockquote,
        Some(true),
        "sub-frame is a blockquote"
    );
    assert!(bq.table.is_none(), "blockquote frame is not a table anchor");

    let bq_subs = sub_frame_ids(&bq);
    assert_eq!(bq_subs.len(), 1, "blockquote contains the table anchor");
    let anchor = get_frame(&db, bq_subs[0]);
    assert!(anchor.table.is_some(), "anchor frame links to the table");
    assert_eq!(anchor.parent_frame, Some(bq.id));

    assert_eq!(get_table_ids(&db)?.len(), 1);
    Ok(())
}

#[test]
fn test_md_import_text_then_table_in_blockquote() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_markdown(
        &db,
        &ev,
        "> Some text\n>\n> | a | b |\n> |---|---|\n> | c | d |",
    )?;

    let root = get_frame(&db, get_frame_id(&db)?);
    let root_subs = sub_frame_ids(&root);
    assert_eq!(root_subs.len(), 1);

    let bq = get_frame(&db, root_subs[0]);
    assert_eq!(bq.fmt_is_blockquote, Some(true));
    assert_eq!(block_entries(&bq).len(), 1, "one paragraph block in quote");
    let bq_subs = sub_frame_ids(&bq);
    assert_eq!(bq_subs.len(), 1, "one table anchor in quote");
    // Document order: block first, then the anchor.
    assert!(bq.child_order[0] > 0 && bq.child_order[1] < 0);
    Ok(())
}

#[test]
fn test_md_import_table_after_blockquote_closes() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_markdown(&db, &ev, "> Para\n\n| a | b |\n|---|---|\n| c | d |")?;

    let root = get_frame(&db, get_frame_id(&db)?);
    let root_subs = sub_frame_ids(&root);
    assert_eq!(
        root_subs.len(),
        2,
        "root holds the blockquote AND the table anchor, child_order: {:?}",
        root.child_order
    );

    let bq = get_frame(&db, root_subs[0]);
    assert_eq!(bq.fmt_is_blockquote, Some(true));
    assert!(
        sub_frame_ids(&bq).is_empty(),
        "the table must NOT be swallowed into the blockquote"
    );
    assert_eq!(block_entries(&bq).len(), 1);

    let anchor = get_frame(&db, root_subs[1]);
    assert!(
        anchor.table.is_some(),
        "second sub-frame is the table anchor"
    );
    assert_eq!(anchor.parent_frame, Some(root.id));
    Ok(())
}

#[test]
fn test_md_import_table_in_nested_blockquote() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_markdown(&db, &ev, ">> | a | b |\n>> |---|---|\n>> | c | d |")?;

    let root = get_frame(&db, get_frame_id(&db)?);
    let depth1_ids = sub_frame_ids(&root);
    assert_eq!(depth1_ids.len(), 1);
    let depth1 = get_frame(&db, depth1_ids[0]);
    assert_eq!(depth1.fmt_is_blockquote, Some(true));

    let depth2_ids = sub_frame_ids(&depth1);
    assert_eq!(depth2_ids.len(), 1);
    let depth2 = get_frame(&db, depth2_ids[0]);
    assert_eq!(depth2.fmt_is_blockquote, Some(true));

    let anchor_ids = sub_frame_ids(&depth2);
    assert_eq!(anchor_ids.len(), 1);
    assert!(get_frame(&db, anchor_ids[0]).table.is_some());
    Ok(())
}

#[test]
fn test_md_import_list_in_blockquote() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_markdown(&db, &ev, "> - item1\n> - item2")?;

    let root = get_frame(&db, get_frame_id(&db)?);
    let root_subs = sub_frame_ids(&root);
    assert_eq!(root_subs.len(), 1);

    let bq = get_frame(&db, root_subs[0]);
    assert_eq!(bq.fmt_is_blockquote, Some(true));
    let items = block_entries(&bq);
    assert_eq!(items.len(), 2, "both list items live in the quote");

    // Both blocks must belong to the same list entity.
    let mut list_ids = Vec::new();
    for bid in &items {
        let rel = test_harness::block_controller::get_relationship(
            &db,
            bid,
            &test_harness::BlockRelationshipField::List,
        )?;
        assert_eq!(rel.len(), 1, "list item block has a List relationship");
        list_ids.push(rel[0]);
    }
    assert_eq!(list_ids[0], list_ids[1], "items grouped into one list");
    Ok(())
}

#[test]
fn test_md_import_mixed_list_and_table_in_blockquote() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_markdown(
        &db,
        &ev,
        "> - item1\n>\n> | a | b |\n> |---|---|\n> | c | d |",
    )?;

    let root = get_frame(&db, get_frame_id(&db)?);
    let root_subs = sub_frame_ids(&root);
    assert_eq!(root_subs.len(), 1);

    let bq = get_frame(&db, root_subs[0]);
    assert_eq!(bq.fmt_is_blockquote, Some(true));
    assert_eq!(block_entries(&bq).len(), 1, "list item block in quote");
    let anchors = sub_frame_ids(&bq);
    assert_eq!(anchors.len(), 1, "table anchor in quote");
    assert!(get_frame(&db, anchors[0]).table.is_some());
    // Document order preserved: block before table.
    assert!(bq.child_order[0] > 0 && bq.child_order[1] < 0);
    Ok(())
}

// ─── HTML import ────────────────────────────────────────────────────

#[test]
fn test_html_import_table_in_blockquote() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_html(
        &db,
        &ev,
        "<blockquote><table><tr><th>H</th></tr><tr><td>C</td></tr></table></blockquote>",
    )?;

    let root = get_frame(&db, get_frame_id(&db)?);
    let root_subs = sub_frame_ids(&root);
    assert_eq!(root_subs.len(), 1, "child_order: {:?}", root.child_order);

    let bq = get_frame(&db, root_subs[0]);
    assert_eq!(bq.fmt_is_blockquote, Some(true));
    let anchors = sub_frame_ids(&bq);
    assert_eq!(anchors.len(), 1);
    let anchor = get_frame(&db, anchors[0]);
    assert!(anchor.table.is_some());
    assert_eq!(anchor.parent_frame, Some(bq.id));
    Ok(())
}

#[test]
fn test_html_import_table_after_blockquote() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_html(
        &db,
        &ev,
        "<blockquote><p>Para</p></blockquote><table><tr><td>X</td></tr></table>",
    )?;

    let root = get_frame(&db, get_frame_id(&db)?);
    let root_subs = sub_frame_ids(&root);
    assert_eq!(root_subs.len(), 2, "child_order: {:?}", root.child_order);

    let bq = get_frame(&db, root_subs[0]);
    assert_eq!(bq.fmt_is_blockquote, Some(true));
    assert!(
        sub_frame_ids(&bq).is_empty(),
        "table stays outside the quote"
    );

    let anchor = get_frame(&db, root_subs[1]);
    assert!(anchor.table.is_some());
    assert_eq!(anchor.parent_frame, Some(root.id));
    Ok(())
}

#[test]
fn test_html_import_backfills_cell_frame_parent() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_html(&db, &ev, "<table><tr><td>A</td><td>B</td></tr></table>")?;

    let table_ids = get_table_ids(&db)?;
    assert_eq!(table_ids.len(), 1);
    let cells = test_harness::get_sorted_cells(&db, &table_ids[0])?;
    assert_eq!(cells.len(), 2);
    for cell in &cells {
        let cf_id = cell.cell_frame.expect("cell has a frame");
        let cf = get_frame(&db, cf_id);
        let parent = cf.parent_frame.expect("cell frame parent_frame is set");
        let anchor = get_frame(&db, parent);
        assert_eq!(
            anchor.table,
            Some(table_ids[0]),
            "cell frame parent is the table's anchor frame"
        );
    }
    Ok(())
}

#[test]
fn test_html_import_list_in_blockquote() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_html(
        &db,
        &ev,
        "<blockquote><ul><li>one</li><li>two</li></ul></blockquote>",
    )?;

    let root = get_frame(&db, get_frame_id(&db)?);
    let root_subs = sub_frame_ids(&root);
    assert_eq!(root_subs.len(), 1);
    let bq = get_frame(&db, root_subs[0]);
    assert_eq!(bq.fmt_is_blockquote, Some(true));
    assert_eq!(block_entries(&bq).len(), 2);
    Ok(())
}

// ─── Export round-trips ─────────────────────────────────────────────

#[test]
fn test_md_roundtrip_table_in_blockquote() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_markdown(&db, &ev, "> | a | b |\n> |---|---|\n> | c | d |")?;

    let result = document_io_controller::export_markdown(&db, &ev)?;
    let quoted_table_lines = result
        .markdown_text
        .lines()
        .filter(|l| l.trim_start().starts_with("> |"))
        .count();
    assert!(
        quoted_table_lines >= 3,
        "exported table rows must keep the quote prefix, got:\n{}",
        result.markdown_text
    );
    Ok(())
}

#[test]
fn test_md_roundtrip_list_in_blockquote() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_markdown(&db, &ev, "> - item1\n> - item2")?;

    let result = document_io_controller::export_markdown(&db, &ev)?;
    let quoted_items = result
        .markdown_text
        .lines()
        .filter(|l| l.trim_start().starts_with("> -"))
        .count();
    assert_eq!(
        quoted_items, 2,
        "exported list items must keep the quote prefix, got:\n{}",
        result.markdown_text
    );
    Ok(())
}

#[test]
fn test_html_roundtrip_table_in_blockquote() -> Result<()> {
    let (db, ev, _) = setup()?;
    import_markdown(&db, &ev, "> | a | b |\n> |---|---|\n> | c | d |")?;

    let result = document_io_controller::export_html(&db, &ev)?;
    let html = result.html_text;
    let bq_start = html.find("<blockquote>").expect("blockquote in export");
    let bq_end = html.find("</blockquote>").expect("blockquote closes");
    let table_pos = html.find("<table").expect("table in export");
    assert!(
        bq_start < table_pos && table_pos < bq_end,
        "table must be inside the blockquote, got:\n{html}"
    );
    Ok(())
}