Skip to main content

text_document/
fragment.rs

1//! DocumentFragment — format-agnostic rich text interchange type.
2
3use crate::{CharVerticalAlignment, InlineContent, LINE_SEPARATOR, ListStyle};
4use frontend::common::parser_tools::content_parser::{
5    ParsedBlock, ParsedElement, ParsedSpan, split_block_at_line_breaks,
6};
7use frontend::common::parser_tools::fragment_schema::{
8    FragmentBlock, FragmentData, FragmentElement, FragmentListStarts, FragmentQuoting,
9    FragmentTable, FragmentTableCell, as_a_whole_text, fragment_from_json,
10    fragment_with_list_starts_to_json,
11};
12
13/// A piece of rich text that can be inserted into a [`TextDocument`](crate::TextDocument).
14///
15/// `DocumentFragment` is the clipboard/interchange type. It carries
16/// blocks, per-character format runs, image anchors, and structural
17/// metadata in a format-agnostic internal representation.
18#[derive(Debug, Clone)]
19pub struct DocumentFragment {
20    data: String,
21    plain_text: String,
22}
23
24impl DocumentFragment {
25    /// Create an empty fragment.
26    pub fn new() -> Self {
27        Self {
28            data: String::new(),
29            plain_text: String::new(),
30        }
31    }
32
33    /// Create a fragment from plain text.
34    ///
35    /// Builds valid fragment data so the fragment can be inserted via
36    /// [`TextCursor::insert_fragment`](crate::TextCursor::insert_fragment).
37    pub fn from_plain_text(text: &str) -> Self {
38        let blocks: Vec<FragmentBlock> = text
39            .split('\n')
40            .map(|line| FragmentBlock {
41                plain_text: line.to_string(),
42                elements: vec![FragmentElement {
43                    content: InlineContent::Text(line.to_string()),
44                    fmt_font_family: None,
45                    fmt_font_point_size: None,
46                    fmt_font_weight: None,
47                    fmt_font_bold: None,
48                    fmt_font_italic: None,
49                    fmt_font_underline: None,
50                    fmt_font_overline: None,
51                    fmt_font_strikeout: None,
52                    fmt_letter_spacing: None,
53                    fmt_word_spacing: None,
54                    fmt_anchor_href: None,
55                    fmt_anchor_names: vec![],
56                    fmt_is_anchor: None,
57                    fmt_tooltip: None,
58                    fmt_underline_style: None,
59                    fmt_vertical_alignment: None,
60                }],
61                heading_level: None,
62                list: None,
63                alignment: None,
64                indent: None,
65                text_indent: None,
66                marker: None,
67                top_margin: None,
68                bottom_margin: None,
69                left_margin: None,
70                right_margin: None,
71                tab_positions: vec![],
72                line_height: None,
73                non_breakable_lines: None,
74                page_break_before: None,
75                direction: None,
76                background_color: None,
77                is_code_block: None,
78                code_language: None,
79                hyphenate: None,
80                language: None,
81            })
82            .collect();
83
84        let data = serde_json::to_string(&FragmentData {
85            blocks,
86            tables: vec![],
87        })
88        .expect("fragment serialization should not fail");
89
90        Self {
91            data,
92            plain_text: text.to_string(),
93        }
94    }
95
96    /// Create a fragment from HTML, as a clipboard carries a passage copied from a web page
97    /// or another application.
98    ///
99    /// A `<pre>` goes in as paragraphs, one for each of its lines, each keeping its spaces,
100    /// where [`TextDocument::set_html`](crate::TextDocument::set_html) loads it as a code
101    /// block: a preformatted passage pasted into prose is most often a poem or a letter set
102    /// line by line, and a code block keeps no emphasis and takes no footnote reference.
103    pub fn from_html(html: &str) -> Self {
104        let parsed = frontend::common::parser_tools::content_parser::parse_html_elements(html);
105        parsed_elements_to_fragment(parsed, ParsedFrom::Html)
106    }
107
108    /// Create a fragment from Markdown. The fragment is a whole text: see
109    /// [`from_djot`](Self::from_djot).
110    pub fn from_markdown(markdown: &str) -> Self {
111        let parsed = frontend::common::parser_tools::content_parser::parse_markdown(markdown);
112        parsed_elements_to_fragment(parsed, ParsedFrom::SavedSyntax)
113    }
114
115    /// Create a fragment from djot markup. Paste always uses the lossless
116    /// default [`crate::DjotImportOptions`]; per-feature selection is exposed on
117    /// the document-level import path (`TextDocument::set_djot_with_options`).
118    ///
119    /// The fragment is a whole text: inserted over a selection holding the whole of a
120    /// text, it reads as the same Djot loaded, even as a single plain paragraph (see
121    /// [`TextCursor::insert_fragment`](crate::TextCursor::insert_fragment)).
122    pub fn from_djot(djot: &str) -> Self {
123        let parsed = frontend::common::parser_tools::content_parser::parse_djot(
124            djot,
125            &frontend::common::parser_tools::DjotImportOptions::default(),
126        );
127        parsed_elements_to_fragment(parsed, ParsedFrom::SavedSyntax)
128    }
129
130    /// Create a fragment from an entire document. The fragment is a whole text: see
131    /// [`from_djot`](Self::from_djot).
132    pub fn from_document(doc: &crate::TextDocument) -> crate::Result<Self> {
133        let inner = doc.inner.lock();
134        // Use i64::MAX as anchor to ensure the full document is captured.
135        // Document positions include inter-block gaps, so character_count
136        // alone would truncate the last block.
137        let dto = frontend::document_inspection::ExtractFragmentDto {
138            position: 0,
139            anchor: i64::MAX,
140        };
141        let result =
142            frontend::commands::document_inspection_commands::extract_fragment(&inner.ctx, &dto)?;
143        Ok(Self::from_raw(
144            as_a_whole_text(&result.fragment_data),
145            result.plain_text,
146        ))
147    }
148
149    /// Create a fragment from the serialized internal format.
150    pub(crate) fn from_raw(data: String, plain_text: String) -> Self {
151        Self { data, plain_text }
152    }
153
154    /// Export the fragment as plain text.
155    pub fn to_plain_text(&self) -> &str {
156        &self.plain_text
157    }
158
159    /// Export the fragment as HTML.
160    pub fn to_html(&self) -> String {
161        if self.data.is_empty() {
162            return String::from("<html><head><meta charset=\"utf-8\"></head><body></body></html>");
163        }
164
165        let (fragment_data, list_starts) = match fragment_from_json(&self.data) {
166            Ok(carried) => (carried.data, carried.list_starts),
167            Err(_) => {
168                return String::from(
169                    "<html><head><meta charset=\"utf-8\"></head><body></body></html>",
170                );
171            }
172        };
173
174        let mut body = String::new();
175        let blocks = &fragment_data.blocks;
176
177        // Single inline-only block with no tables: emit inline HTML without block wrapper
178        if blocks.len() == 1 && blocks[0].is_inline_only() && fragment_data.tables.is_empty() {
179            push_inline_html(&mut body, &blocks[0].elements);
180            return format!(
181                "<html><head><meta charset=\"utf-8\"></head><body>{}</body></html>",
182                body
183            );
184        }
185
186        // Sort tables by block_insert_index so we can interleave them
187        let mut sorted_tables: Vec<&FragmentTable> = fragment_data.tables.iter().collect();
188        sorted_tables.sort_by_key(|t| t.block_insert_index);
189        let mut table_cursor = 0;
190
191        let mut i = 0;
192
193        while i < blocks.len() {
194            // Insert any tables whose block_insert_index == i
195            while table_cursor < sorted_tables.len()
196                && sorted_tables[table_cursor].block_insert_index <= i
197            {
198                push_table_html(&mut body, sorted_tables[table_cursor]);
199                table_cursor += 1;
200            }
201
202            let block = &blocks[i];
203
204            if let Some(ref list) = block.list {
205                let is_ordered = is_ordered_list_style(&list.style);
206                let list_tag = if is_ordered { "ol" } else { "ul" };
207                body.push('<');
208                body.push_str(list_tag);
209                // Where the list starts, when it is not 1.
210                if let Some(start) = list_starts.get(i).filter(|_| is_ordered) {
211                    body.push_str(&format!(" start=\"{start}\""));
212                }
213                body.push('>');
214
215                while i < blocks.len() {
216                    let b = &blocks[i];
217                    match &b.list {
218                        Some(l) if is_ordered_list_style(&l.style) == is_ordered => {
219                            body.push_str("<li>");
220                            push_inline_html(&mut body, &b.elements);
221                            body.push_str("</li>");
222                            i += 1;
223                        }
224                        _ => break,
225                    }
226                }
227
228                body.push_str("</");
229                body.push_str(list_tag);
230                body.push('>');
231            } else if let Some(level) = block.heading_level {
232                let n = level.clamp(1, 6);
233                body.push_str(&format!("<h{}>", n));
234                push_inline_html(&mut body, &block.elements);
235                body.push_str(&format!("</h{}>", n));
236                i += 1;
237            } else {
238                // Emit block-level formatting as inline styles (ISSUE-19)
239                let style = block_style_attr(block);
240                if style.is_empty() {
241                    body.push_str("<p>");
242                } else {
243                    body.push_str(&format!("<p style=\"{}\">", style));
244                }
245                push_inline_html(&mut body, &block.elements);
246                body.push_str("</p>");
247                i += 1;
248            }
249        }
250
251        // Emit any remaining tables after all blocks
252        while table_cursor < sorted_tables.len() {
253            push_table_html(&mut body, sorted_tables[table_cursor]);
254            table_cursor += 1;
255        }
256
257        format!(
258            "<html><head><meta charset=\"utf-8\"></head><body>{}</body></html>",
259            body
260        )
261    }
262
263    /// Export the fragment as Markdown.
264    pub fn to_markdown(&self) -> String {
265        if self.data.is_empty() {
266            return String::new();
267        }
268
269        let (fragment_data, list_starts) = match fragment_from_json(&self.data) {
270            Ok(carried) => (carried.data, carried.list_starts),
271            Err(_) => return String::new(),
272        };
273
274        // (rendered_text, is_list_item) — used for join logic
275        let mut parts: Vec<(String, bool)> = Vec::new();
276        let mut prev_was_list = false;
277        let mut list_counter: u32 = 0;
278
279        // Sort tables by block_insert_index for interleaving
280        let mut sorted_tables: Vec<&FragmentTable> = fragment_data.tables.iter().collect();
281        sorted_tables.sort_by_key(|t| t.block_insert_index);
282        let mut table_cursor = 0;
283
284        for (blk_idx, block) in fragment_data.blocks.iter().enumerate() {
285            // Insert tables before this block index
286            while table_cursor < sorted_tables.len()
287                && sorted_tables[table_cursor].block_insert_index <= blk_idx
288            {
289                parts.push((render_table_markdown(sorted_tables[table_cursor]), false));
290                prev_was_list = false;
291                list_counter = 0;
292                table_cursor += 1;
293            }
294
295            // An ATX heading is one line, so its line breaks are spaces.
296            let line_break = if block.heading_level.is_some() {
297                " "
298            } else {
299                "\\\n"
300            };
301            let inline_text = render_inline_markdown(&block.elements, line_break);
302            let is_list = block.list.is_some();
303
304            let indent_prefix = match block.indent {
305                Some(n) if n > 0 => "  ".repeat(n as usize),
306                _ => String::new(),
307            };
308
309            if let Some(level) = block.heading_level {
310                let n = level.clamp(1, 6) as usize;
311                let prefix = "#".repeat(n);
312                parts.push((format!("{} {}", prefix, inline_text), false));
313                prev_was_list = false;
314                list_counter = 0;
315            } else if let Some(ref list) = block.list {
316                let is_ordered = is_ordered_list_style(&list.style);
317                if !prev_was_list {
318                    // From where the list starts, when it is not 1.
319                    list_counter = list_starts
320                        .get(blk_idx)
321                        .and_then(|start| u32::try_from(start.saturating_sub(1)).ok())
322                        .unwrap_or(0);
323                }
324                if is_ordered {
325                    list_counter += 1;
326                    parts.push((
327                        format!("{}{}. {}", indent_prefix, list_counter, inline_text),
328                        true,
329                    ));
330                } else {
331                    parts.push((format!("{}- {}", indent_prefix, inline_text), true));
332                }
333                prev_was_list = true;
334            } else {
335                if indent_prefix.is_empty() {
336                    parts.push((inline_text, false));
337                } else {
338                    parts.push((format!("{}{}", indent_prefix, inline_text), false));
339                }
340                prev_was_list = false;
341                list_counter = 0;
342            }
343
344            if !is_list {
345                prev_was_list = false;
346            }
347        }
348
349        // Emit remaining tables after all blocks
350        while table_cursor < sorted_tables.len() {
351            parts.push((render_table_markdown(sorted_tables[table_cursor]), false));
352            table_cursor += 1;
353        }
354
355        // Join: list items with \n, others with \n\n
356        let mut result = String::new();
357        for (idx, (text, is_list)) in parts.iter().enumerate() {
358            if idx > 0 {
359                let (_, prev_is_list) = &parts[idx - 1];
360                if *prev_is_list && *is_list {
361                    result.push('\n');
362                } else {
363                    result.push_str("\n\n");
364                }
365            }
366            result.push_str(text);
367        }
368
369        result
370    }
371
372    /// Returns true if the fragment contains no text or elements.
373    pub fn is_empty(&self) -> bool {
374        self.plain_text.is_empty()
375    }
376
377    /// Returns the serialized internal representation.
378    pub(crate) fn raw_data(&self) -> &str {
379        &self.data
380    }
381}
382
383impl Default for DocumentFragment {
384    fn default() -> Self {
385        Self::new()
386    }
387}
388
389// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
390// Shared helpers (used by both to_html and to_markdown)
391// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
392
393fn is_ordered_list_style(style: &ListStyle) -> bool {
394    matches!(
395        style,
396        ListStyle::Decimal
397            | ListStyle::LowerAlpha
398            | ListStyle::UpperAlpha
399            | ListStyle::LowerRoman
400            | ListStyle::UpperRoman
401    )
402}
403
404// ── HTML helpers ────────────────────────────────────────────────
405
406fn escape_html(s: &str) -> String {
407    let mut out = String::with_capacity(s.len());
408    for c in s.chars() {
409        match c {
410            '&' => out.push_str("&amp;"),
411            '<' => out.push_str("&lt;"),
412            '>' => out.push_str("&gt;"),
413            '"' => out.push_str("&quot;"),
414            '\'' => out.push_str("&#x27;"),
415            // A raw CR in text content is normalised to LF by the HTML5 input
416            // preprocessor on re-import (CR-from-`&#xD;` survives, literal CR
417            // does not), which breaks serialiser idempotency. Emit it as a
418            // numeric reference so it round-trips losslessly.
419            '\r' => out.push_str("&#13;"),
420            _ => out.push(c),
421        }
422    }
423    out
424}
425
426/// Build a CSS `style` attribute value from block-level formatting (ISSUE-19).
427fn block_style_attr(block: &FragmentBlock) -> String {
428    use crate::Alignment;
429
430    let mut parts = Vec::new();
431    if let Some(ref alignment) = block.alignment {
432        let value = match alignment {
433            Alignment::Left => "left",
434            Alignment::Right => "right",
435            Alignment::Center => "center",
436            Alignment::Justify => "justify",
437        };
438        parts.push(format!("text-align: {}", value));
439    }
440    if let Some(n) = block.indent
441        && n > 0
442    {
443        parts.push(format!("margin-left: {}em", n));
444    }
445    if let Some(px) = block.text_indent
446        && px != 0
447    {
448        parts.push(format!("text-indent: {}px", px));
449    }
450    if let Some(px) = block.top_margin {
451        parts.push(format!("margin-top: {}px", px));
452    }
453    if let Some(px) = block.bottom_margin {
454        parts.push(format!("margin-bottom: {}px", px));
455    }
456    if let Some(px) = block.left_margin {
457        parts.push(format!("margin-left: {}px", px));
458    }
459    if let Some(px) = block.right_margin {
460        parts.push(format!("margin-right: {}px", px));
461    }
462    parts.join("; ")
463}
464
465fn push_inline_html(out: &mut String, elements: &[FragmentElement]) {
466    for elem in elements {
467        let text = match &elem.content {
468            InlineContent::Text(t) => escape_html(t).replace(LINE_SEPARATOR, "<br>"),
469            // A reference carried onto the clipboard keeps its marker; the note
470            // body is not part of the fragment.
471            InlineContent::FootnoteRef { label } => {
472                let id = escape_html(label);
473                out.push_str(&format!(
474                    "<a epub:type=\"noteref\" role=\"doc-noteref\" href=\"#fn-{id}\"><sup>{id}</sup></a>"
475                ));
476                continue;
477            }
478            InlineContent::Image {
479                name,
480                alt,
481                width,
482                height,
483                ..
484            } => {
485                // This is the HTML written to the OS clipboard, so it is what
486                // another application receives on paste. It carried no `alt` at
487                // all, which made every copied image inaccessible in the target
488                // document.
489                let mut tag = format!(
490                    "<img src=\"{}\" alt=\"{}\"",
491                    escape_html(name),
492                    escape_html(alt)
493                );
494                if *width > 0 {
495                    tag.push_str(&format!(" width=\"{width}\""));
496                }
497                if *height > 0 {
498                    tag.push_str(&format!(" height=\"{height}\""));
499                }
500                tag.push('>');
501                tag
502            }
503            InlineContent::Empty => String::new(),
504        };
505
506        let is_monospace = elem
507            .fmt_font_family
508            .as_deref()
509            .is_some_and(|f| f == "monospace");
510        let is_bold = elem.fmt_font_bold.unwrap_or(false);
511        let is_italic = elem.fmt_font_italic.unwrap_or(false);
512        let is_underline = elem.fmt_font_underline.unwrap_or(false);
513        let is_strikeout = elem.fmt_font_strikeout.unwrap_or(false);
514        let is_anchor = elem.fmt_is_anchor.unwrap_or(false);
515        // Superscript and subscript are character formatting like the rest, and
516        // an exponent or a chemical index that lands on the baseline is simply
517        // the wrong text — so they belong in the clipboard payload alongside
518        // bold and italic rather than being dropped on the way out.
519        let vertical = elem.fmt_vertical_alignment.as_ref();
520
521        let mut result = text;
522
523        if is_monospace {
524            result = format!("<code>{}</code>", result);
525        }
526        if is_bold {
527            result = format!("<strong>{}</strong>", result);
528        }
529        if is_italic {
530            result = format!("<em>{}</em>", result);
531        }
532        if is_underline {
533            result = format!("<u>{}</u>", result);
534        }
535        if is_strikeout {
536            result = format!("<s>{}</s>", result);
537        }
538        match vertical {
539            Some(CharVerticalAlignment::SuperScript) => result = format!("<sup>{result}</sup>"),
540            Some(CharVerticalAlignment::SubScript) => result = format!("<sub>{result}</sub>"),
541            _ => {}
542        }
543        if is_anchor && let Some(ref href) = elem.fmt_anchor_href {
544            result = format!("<a href=\"{}\">{}</a>", escape_html(href), result);
545        }
546
547        out.push_str(&result);
548    }
549}
550
551/// Emit an HTML `<table>` for a `FragmentTable`.
552fn push_table_html(out: &mut String, table: &FragmentTable) {
553    out.push_str("<table>");
554    for row in 0..table.rows {
555        out.push_str("<tr>");
556        for col in 0..table.columns {
557            if let Some(cell) = table.cells.iter().find(|c| c.row == row && c.column == col) {
558                out.push_str("<td");
559                if cell.row_span > 1 {
560                    out.push_str(&format!(" rowspan=\"{}\"", cell.row_span));
561                }
562                if cell.column_span > 1 {
563                    out.push_str(&format!(" colspan=\"{}\"", cell.column_span));
564                }
565                out.push('>');
566                for (i, block) in cell.blocks.iter().enumerate() {
567                    if i > 0 {
568                        out.push_str("<br>");
569                    }
570                    push_inline_html(out, &block.elements);
571                }
572                out.push_str("</td>");
573            }
574            // Skip positions covered by spans — the HTML renderer handles them.
575        }
576        out.push_str("</tr>");
577    }
578    out.push_str("</table>");
579}
580
581// ── Markdown helpers ────────────────────────────────────────────
582
583fn escape_markdown(s: &str) -> String {
584    let mut out = String::with_capacity(s.len());
585    for c in s.chars() {
586        if matches!(
587            c,
588            '\\' | '`'
589                | '*'
590                | '_'
591                | '{'
592                | '}'
593                | '['
594                | ']'
595                | '('
596                | ')'
597                | '#'
598                | '+'
599                | '-'
600                | '.'
601                | '!'
602                | '|'
603                | '~'
604                | '<'
605                | '>'
606        ) {
607            out.push('\\');
608        }
609        out.push(c);
610    }
611    out
612}
613
614/// A block's inline content as Markdown, each line break inside it
615/// ([`LINE_SEPARATOR`]) written as `line_break`: a backslash hard break in a block, a
616/// space in a heading or a table cell, which are one line.
617///
618/// A break at the very end of the block is left out: a backslash there is not a hard
619/// break to a reader but the text `\`, and Markdown has nothing else to write it as. A
620/// code span cannot hold a hard break, so one over a break is written as one code span a
621/// line.
622fn render_inline_markdown(elements: &[FragmentElement], line_break: &str) -> String {
623    // The breaks the block ends with, in whichever elements hold them.
624    let mut trailing = 0usize;
625    for elem in elements.iter().rev() {
626        match &elem.content {
627            InlineContent::Text(t) => {
628                let kept = t.trim_end_matches(LINE_SEPARATOR);
629                trailing += t.len() - kept.len();
630                if !kept.is_empty() {
631                    break;
632                }
633            }
634            InlineContent::Empty => {}
635            _ => break,
636        }
637    }
638    let mut to_drop = trailing;
639    let trimmed: Vec<FragmentElement> = elements
640        .iter()
641        .rev()
642        .map(|elem| match &elem.content {
643            InlineContent::Text(t) if to_drop > 0 => {
644                let keep = t.len().saturating_sub(to_drop);
645                to_drop -= t.len() - keep;
646                FragmentElement {
647                    content: InlineContent::Text(t[..keep].to_string()),
648                    ..elem.clone()
649                }
650            }
651            _ => elem.clone(),
652        })
653        .collect::<Vec<_>>()
654        .into_iter()
655        .rev()
656        .collect();
657    let mut out = String::new();
658    for elem in &trimmed {
659        let raw_text = match &elem.content {
660            InlineContent::Text(t) => t.clone(),
661            // `name` was used as both alt and source, so a pasted image
662            // described itself with its filename.
663            InlineContent::Image { name, alt, .. } => format!("![{alt}]({name})"),
664            InlineContent::FootnoteRef { label } => format!("[^{label}]"),
665            InlineContent::Empty => String::new(),
666        };
667
668        let is_monospace = elem
669            .fmt_font_family
670            .as_deref()
671            .is_some_and(|f| f == "monospace");
672        let is_bold = elem.fmt_font_bold.unwrap_or(false);
673        let is_italic = elem.fmt_font_italic.unwrap_or(false);
674        let is_strikeout = elem.fmt_font_strikeout.unwrap_or(false);
675        let is_anchor = elem.fmt_is_anchor.unwrap_or(false);
676
677        if is_monospace {
678            // A code span is one line: one a line, the break between them.
679            let spans: Vec<String> = raw_text
680                .split(LINE_SEPARATOR)
681                .map(|line| {
682                    if line.is_empty() {
683                        String::new()
684                    } else {
685                        format!("`{line}`")
686                    }
687                })
688                .collect();
689            out.push_str(&spans.join(line_break));
690        } else {
691            let mut text = escape_markdown(&raw_text).replace(LINE_SEPARATOR, line_break);
692            if is_bold && is_italic {
693                text = format!("***{}***", text);
694            } else if is_bold {
695                text = format!("**{}**", text);
696            } else if is_italic {
697                text = format!("*{}*", text);
698            }
699            if is_strikeout {
700                text = format!("~~{}~~", text);
701            }
702            if is_anchor {
703                let href = elem.fmt_anchor_href.as_deref().unwrap_or("");
704                out.push_str(&format!("[{}]({})", text, href));
705            } else {
706                out.push_str(&text);
707            }
708        }
709    }
710    out
711}
712
713/// Render a `FragmentTable` as a pipe-delimited Markdown table.
714fn render_table_markdown(table: &FragmentTable) -> String {
715    let mut rows: Vec<Vec<String>> = vec![vec![String::new(); table.columns]; table.rows];
716
717    for cell in &table.cells {
718        let text: String = cell
719            .blocks
720            .iter()
721            .map(|b| render_inline_markdown(&b.elements, " "))
722            .collect::<Vec<_>>()
723            .join(" ");
724        if cell.row < table.rows && cell.column < table.columns {
725            rows[cell.row][cell.column] = text;
726        }
727    }
728
729    let mut out = String::new();
730    for (i, row) in rows.iter().enumerate() {
731        out.push_str("| ");
732        out.push_str(&row.join(" | "));
733        out.push_str(" |");
734        if i == 0 {
735            // Header separator
736            out.push('\n');
737            out.push('|');
738            for _ in 0..table.columns {
739                out.push_str(" --- |");
740            }
741        }
742        if i + 1 < rows.len() {
743            out.push('\n');
744        }
745    }
746    out
747}
748
749// ── Fragment construction from parsed content ───────────────────
750
751/// Convert parsed blocks (from HTML or Markdown parser) into a `DocumentFragment`.
752/// Convert a `ParsedSpan` to a `FragmentElement`.
753/// The plain text of a run of parsed spans, with an image standing as its
754/// `U+FFFC` sentinel.
755///
756/// That sentinel is the convention the *extract* side already writes (see
757/// `extract_fragment_uc`), and it is what `insert_fragment_uc` positions each
758/// `ImageAnchor` against — an image with no character in the text has nothing
759/// to anchor to, and every format run after it lands three bytes early.
760fn spans_plain_text(spans: &[ParsedSpan]) -> String {
761    spans
762        .iter()
763        .map(|s| {
764            // Every inline object contributes its sentinel: the anchors this
765            // fragment carries are positioned against *this* string, so an
766            // object missing from it puts every anchor after it out by three
767            // bytes.
768            if s.image.is_some() || s.footnote_ref.is_some() {
769                "\u{FFFC}"
770            } else {
771                s.text.as_str()
772            }
773        })
774        .collect()
775}
776
777fn span_to_fragment_element(span: &ParsedSpan) -> FragmentElement {
778    // An image span carries no text — the picture *is* the content. Emitting
779    // `Text("")` for it, which is what this did, produced a fragment with an
780    // empty element and no image anywhere: `insert_djot` of an image markup
781    // returned `Ok` and inserted nothing at all.
782    //
783    // A footnote reference is the same shape and the same trap: its span carries
784    // no text either, so the identical `Text("")` would have made
785    // `insert_footnote_reference` a no-op that reported success.
786    let content = match (&span.footnote_ref, &span.image) {
787        (Some(label), _) => InlineContent::FootnoteRef {
788            label: label.clone(),
789        },
790        (None, Some(img)) => InlineContent::Image {
791            name: img.src.clone(),
792            alt: img.alt.clone(),
793            width: img.width,
794            height: img.height,
795            // The source states a display size or it does not; nothing here
796            // re-encodes the image, so there is no quality to carry.
797            quality: 100,
798        },
799        (None, None) => InlineContent::Text(span.text.clone()),
800    };
801    let fmt_font_family = if span.code {
802        Some("monospace".into())
803    } else {
804        None
805    };
806    let fmt_font_bold = if span.bold { Some(true) } else { None };
807    let fmt_font_italic = if span.italic { Some(true) } else { None };
808    let fmt_font_underline = if span.underline { Some(true) } else { None };
809    let fmt_font_strikeout = if span.strikeout { Some(true) } else { None };
810    let (fmt_anchor_href, fmt_is_anchor) = if let Some(ref href) = span.link_href {
811        (Some(href.clone()), Some(true))
812    } else {
813        (None, None)
814    };
815    // Raised and lowered text is character formatting like bold and italic, and
816    // dropping it here is what silently flattened an exponent or a chemical
817    // index on the way in: the parsers set the flags, `character_format_from_span`
818    // maps them, and only this conversion — the one every `insert_html` /
819    // `insert_markdown` / `insert_djot` goes through — threw them away.
820    let fmt_vertical_alignment = if span.superscript {
821        Some(CharVerticalAlignment::SuperScript)
822    } else if span.subscript {
823        Some(CharVerticalAlignment::SubScript)
824    } else {
825        None
826    };
827
828    FragmentElement {
829        content,
830        fmt_font_family,
831        fmt_font_point_size: None,
832        fmt_font_weight: None,
833        fmt_font_bold,
834        fmt_font_italic,
835        fmt_font_underline,
836        fmt_font_overline: None,
837        fmt_font_strikeout,
838        fmt_letter_spacing: None,
839        fmt_word_spacing: None,
840        fmt_anchor_href,
841        fmt_anchor_names: vec![],
842        fmt_is_anchor,
843        fmt_tooltip: None,
844        fmt_underline_style: None,
845        fmt_vertical_alignment,
846    }
847}
848
849/// A parsed block as a fragment block: its text and spans, and everything a load of the
850/// same text keeps of the block, so a text put back by selecting all and inserting it reads
851/// as the text loaded: its alignment, marker, spacing and list markers, and its code-block
852/// format when `keep_code_block` says so. Only the heading and the line attributes were
853/// carried, and every code block of a restored version came back as a plain paragraph. The
854/// quotations the block stands in travel beside it (see [`FragmentQuoting`]).
855fn parsed_block_to_fragment_block(pb: ParsedBlock, keep_code_block: bool) -> FragmentBlock {
856    use frontend::common::parser_tools::fragment_schema::FragmentList;
857
858    let elements: Vec<FragmentElement> = pb.spans.iter().map(span_to_fragment_element).collect();
859    let plain_text: String = spans_plain_text(&pb.spans);
860    let list = pb.list_style.map(|style| FragmentList {
861        style,
862        indent: pb.list_indent as i64,
863        prefix: pb.list_prefix,
864        suffix: pb.list_suffix,
865    });
866    let is_code_block = keep_code_block && pb.is_code_block;
867    FragmentBlock {
868        plain_text,
869        elements,
870        heading_level: pb.heading_level,
871        list,
872        alignment: pb.alignment,
873        indent: None,
874        text_indent: pb.text_indent,
875        marker: pb.marker,
876        top_margin: pb.top_margin,
877        bottom_margin: None,
878        left_margin: None,
879        right_margin: None,
880        tab_positions: vec![],
881        line_height: pb.line_height,
882        non_breakable_lines: pb.non_breakable_lines,
883        page_break_before: pb.page_break_before,
884        direction: pb.direction,
885        background_color: pb.background_color,
886        is_code_block: is_code_block.then_some(true),
887        code_language: if is_code_block {
888            pb.code_language
889        } else {
890            None
891        },
892        hyphenate: None,
893        language: None,
894    }
895}
896
897/// What a fragment was parsed from, which decides what an insertion makes of it.
898#[derive(Debug, Clone, Copy, PartialEq, Eq)]
899enum ParsedFrom {
900    /// A text in a syntax a text is saved in, Djot or Markdown: a whole text, whose code
901    /// blocks are code blocks. Inserted over a selection holding the whole of a text, it
902    /// reads as the same text loaded (see `whole_text_to_json`).
903    SavedSyntax,
904    /// HTML, as a clipboard carries a passage from a web page or another application. A
905    /// `<pre>` is read as paragraphs, one for each of its lines: a code block keeps no
906    /// emphasis, takes no footnote reference, and a host with no command to make it prose
907    /// again left the writer with a passage set as code.
908    Html,
909}
910
911/// Convert parsed elements (blocks + tables) into a `DocumentFragment`,
912/// preserving table structure as `FragmentTable` entries.
913fn parsed_elements_to_fragment(parsed: Vec<ParsedElement>, from: ParsedFrom) -> DocumentFragment {
914    let mut blocks: Vec<FragmentBlock> = Vec::new();
915    let mut tables: Vec<FragmentTable> = Vec::new();
916    // The quotations each block and table stands in, carried beside them.
917    let mut quoting = FragmentQuoting::default();
918    // The number each list starts at, carried beside its items: every item of a list
919    // starting past 1 names it, and an insertion reads it where it makes the list.
920    let mut list_starts = FragmentListStarts::default();
921
922    for elem in parsed {
923        match elem {
924            // A clipboard fragment carries prose, not note bodies: a definition
925            // has no position in the flow being copied, and pasting one would
926            // splice a note's text into the middle of a sentence. The reference
927            // travels; the body stays where it is defined.
928            ParsedElement::FootnoteDefinition { .. } => {}
929            // A code block of a text in a saved syntax is a code block, and holds its lines.
930            // Any other block is prose, and a `\n` in prose is a new block: one line a
931            // block, as a plain-text paste gives. A line break inside the block (a
932            // `LINE_SEPARATOR`) is not one of those, and stays. A pasted `<pre>` is prose too (see
933            // `ParsedFrom::Html`). Kept in one block, it held line breaks, which the Djot
934            // writer used to write as they were, and its lines read back as whatever their
935            // markers spelled.
936            ParsedElement::Block(pb) => {
937                let depth = pb.blockquote_depth;
938                // The quotations opening at the block open at its first line.
939                let mut opens = pb.blockquote_opens;
940                // The quotation's role rides on its first block, as the parsers put it.
941                let mut role = pb.semantic_role.clone();
942                let keep_code_block = pb.is_code_block && from == ParsedFrom::SavedSyntax;
943                let lines = if keep_code_block {
944                    vec![pb]
945                } else {
946                    split_block_at_line_breaks(pb)
947                };
948                for pb in lines {
949                    if let Some(start) = pb.list_start.filter(|_| pb.list_style.is_some()) {
950                        list_starts.set(blocks.len(), start);
951                    }
952                    blocks.push(parsed_block_to_fragment_block(pb, keep_code_block));
953                    if depth > 0 {
954                        quoting.set_block(blocks.len() - 1, depth, role.take());
955                        quoting.set_block_opens(blocks.len() - 1, std::mem::take(&mut opens));
956                    }
957                }
958            }
959            ParsedElement::Table(pt) => {
960                let block_insert_index = blocks.len();
961                let num_columns = pt.column_count();
962                let num_rows = pt.rows.len();
963
964                let mut frag_cells: Vec<FragmentTableCell> = Vec::new();
965                for (row_idx, row) in pt.rows.iter().enumerate() {
966                    // A row shorter than the widest is completed with empty cells, as a
967                    // load completes it: a table with holes in its grid is one no edit
968                    // expects.
969                    for col_idx in 0..num_columns {
970                        let spans = row
971                            .get(col_idx)
972                            .map_or(&[][..], |cell| cell.spans.as_slice());
973                        let cell_elements: Vec<FragmentElement> =
974                            spans.iter().map(span_to_fragment_element).collect();
975                        let cell_text: String = spans_plain_text(spans);
976
977                        frag_cells.push(FragmentTableCell {
978                            row: row_idx,
979                            column: col_idx,
980                            row_span: 1,
981                            column_span: 1,
982                            blocks: vec![FragmentBlock {
983                                plain_text: cell_text,
984                                elements: cell_elements,
985                                heading_level: None,
986                                list: None,
987                                alignment: None,
988                                indent: None,
989                                text_indent: None,
990                                marker: None,
991                                top_margin: None,
992                                bottom_margin: None,
993                                left_margin: None,
994                                right_margin: None,
995                                tab_positions: vec![],
996                                line_height: None,
997                                non_breakable_lines: None,
998                                page_break_before: None,
999                                direction: None,
1000                                background_color: None,
1001                                is_code_block: None,
1002                                code_language: None,
1003                                hyphenate: None,
1004                                language: None,
1005                            }],
1006                            fmt_padding: None,
1007                            fmt_border: None,
1008                            fmt_vertical_alignment: None,
1009                            fmt_background_color: None,
1010                        });
1011                    }
1012                }
1013
1014                tables.push(FragmentTable {
1015                    rows: num_rows,
1016                    columns: num_columns,
1017                    cells: frag_cells,
1018                    block_insert_index,
1019                    fmt_border: None,
1020                    fmt_cell_spacing: None,
1021                    fmt_cell_padding: None,
1022                    fmt_width: None,
1023                    fmt_alignment: None,
1024                    column_widths: vec![],
1025                });
1026                quoting.set_table(tables.len() - 1, pt.blockquote_depth);
1027                quoting.set_table_opens(tables.len() - 1, pt.blockquote_opens);
1028            }
1029        }
1030    }
1031
1032    // The blocks' own text, read before they are serialized: reading it back out of the
1033    // JSON parsed the whole fragment a second time.
1034    let plain_text = blocks
1035        .iter()
1036        .map(|b| b.plain_text.as_str())
1037        .collect::<Vec<_>>()
1038        .join("\n");
1039    let data = FragmentData { blocks, tables };
1040    let data = fragment_with_list_starts_to_json(
1041        &data,
1042        &quoting,
1043        &list_starts,
1044        from == ParsedFrom::SavedSyntax,
1045    )
1046    .expect("fragment serialization should not fail");
1047
1048    DocumentFragment { data, plain_text }
1049}
1050
1051#[cfg(test)]
1052mod tests {
1053    use super::*;
1054
1055    /// A paragraph as a manuscript holds it: emphasis, a link, a note reference.
1056    fn paragraph(i: usize) -> String {
1057        format!(
1058            "Paragraph {i} with *emphasis* and a [link](https://example.org/{i}) in it, and a \
1059             note[^n{i}] after the words that run on to a full line of prose."
1060        )
1061    }
1062
1063    /// A fragment is the text a cursor inserts, carried as JSON, and an insertion refuses
1064    /// JSON past 64 MiB. Every field of every block and every run was written, `null` or
1065    /// empty as most are: the JSON came to about 18 times the Djot it was made from, and
1066    /// putting back a whole text of 26,000 paragraphs failed. Fields holding nothing are
1067    /// left out now.
1068    #[test]
1069    fn a_fragment_holds_little_more_than_its_text() {
1070        let djot: String = (0..200).map(|i| format!("{}\n\n", paragraph(i))).collect();
1071        let fragment = DocumentFragment::from_djot(&djot);
1072        let ratio = fragment.raw_data().len() as f64 / djot.len() as f64;
1073        println!("fragment JSON: {ratio:.2} times the Djot");
1074        assert!(
1075            ratio < 4.0,
1076            "the fragment's JSON is {ratio:.1} times the Djot it was made from"
1077        );
1078        assert_eq!(fragment.to_plain_text().lines().count(), 200);
1079    }
1080
1081    /// A fragment written in full, as the builds before the compact form wrote it, still
1082    /// reads: every field left out reads as holding nothing.
1083    #[test]
1084    fn a_fragment_written_in_full_still_reads() {
1085        let full = r#"{"blocks":[{"plain_text":"Old","elements":[{"content":{"Text":"Old"},
1086            "fmt_font_family":null,"fmt_font_point_size":null,"fmt_font_weight":null,
1087            "fmt_font_bold":true,"fmt_font_italic":null,"fmt_font_underline":null,
1088            "fmt_font_overline":null,"fmt_font_strikeout":null,"fmt_letter_spacing":null,
1089            "fmt_word_spacing":null,"fmt_anchor_href":null,"fmt_anchor_names":[],
1090            "fmt_is_anchor":null,"fmt_tooltip":null,"fmt_underline_style":null,
1091            "fmt_vertical_alignment":null}],"heading_level":2,"list":null,"alignment":null,
1092            "indent":null,"text_indent":null,"marker":null,"top_margin":null,
1093            "bottom_margin":null,"left_margin":null,"right_margin":null,"tab_positions":[],
1094            "line_height":null,"non_breakable_lines":null,"direction":null,
1095            "background_color":null,"is_code_block":null,"code_language":null}]}"#;
1096        let data: FragmentData = serde_json::from_str(full).unwrap();
1097        assert_eq!(data.blocks[0].heading_level, Some(2));
1098        assert_eq!(data.blocks[0].elements[0].fmt_font_bold, Some(true));
1099        let compact = serde_json::to_string(&data).unwrap();
1100        let again: FragmentData = serde_json::from_str(&compact).unwrap();
1101        assert_eq!(again.blocks[0].heading_level, Some(2));
1102        assert_eq!(again.blocks[0].elements[0].fmt_font_bold, Some(true));
1103        assert!(compact.len() < full.len() / 3, "{compact}");
1104    }
1105}