Skip to main content

text_document/
fragment.rs

1//! DocumentFragment — format-agnostic rich text interchange type.
2
3use crate::{CharVerticalAlignment, InlineContent, 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            let inline_text = render_inline_markdown(&block.elements);
296            let is_list = block.list.is_some();
297
298            let indent_prefix = match block.indent {
299                Some(n) if n > 0 => "  ".repeat(n as usize),
300                _ => String::new(),
301            };
302
303            if let Some(level) = block.heading_level {
304                let n = level.clamp(1, 6) as usize;
305                let prefix = "#".repeat(n);
306                parts.push((format!("{} {}", prefix, inline_text), false));
307                prev_was_list = false;
308                list_counter = 0;
309            } else if let Some(ref list) = block.list {
310                let is_ordered = is_ordered_list_style(&list.style);
311                if !prev_was_list {
312                    // From where the list starts, when it is not 1.
313                    list_counter = list_starts
314                        .get(blk_idx)
315                        .and_then(|start| u32::try_from(start.saturating_sub(1)).ok())
316                        .unwrap_or(0);
317                }
318                if is_ordered {
319                    list_counter += 1;
320                    parts.push((
321                        format!("{}{}. {}", indent_prefix, list_counter, inline_text),
322                        true,
323                    ));
324                } else {
325                    parts.push((format!("{}- {}", indent_prefix, inline_text), true));
326                }
327                prev_was_list = true;
328            } else {
329                if indent_prefix.is_empty() {
330                    parts.push((inline_text, false));
331                } else {
332                    parts.push((format!("{}{}", indent_prefix, inline_text), false));
333                }
334                prev_was_list = false;
335                list_counter = 0;
336            }
337
338            if !is_list {
339                prev_was_list = false;
340            }
341        }
342
343        // Emit remaining tables after all blocks
344        while table_cursor < sorted_tables.len() {
345            parts.push((render_table_markdown(sorted_tables[table_cursor]), false));
346            table_cursor += 1;
347        }
348
349        // Join: list items with \n, others with \n\n
350        let mut result = String::new();
351        for (idx, (text, is_list)) in parts.iter().enumerate() {
352            if idx > 0 {
353                let (_, prev_is_list) = &parts[idx - 1];
354                if *prev_is_list && *is_list {
355                    result.push('\n');
356                } else {
357                    result.push_str("\n\n");
358                }
359            }
360            result.push_str(text);
361        }
362
363        result
364    }
365
366    /// Returns true if the fragment contains no text or elements.
367    pub fn is_empty(&self) -> bool {
368        self.plain_text.is_empty()
369    }
370
371    /// Returns the serialized internal representation.
372    pub(crate) fn raw_data(&self) -> &str {
373        &self.data
374    }
375}
376
377impl Default for DocumentFragment {
378    fn default() -> Self {
379        Self::new()
380    }
381}
382
383// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
384// Shared helpers (used by both to_html and to_markdown)
385// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
386
387fn is_ordered_list_style(style: &ListStyle) -> bool {
388    matches!(
389        style,
390        ListStyle::Decimal
391            | ListStyle::LowerAlpha
392            | ListStyle::UpperAlpha
393            | ListStyle::LowerRoman
394            | ListStyle::UpperRoman
395    )
396}
397
398// ── HTML helpers ────────────────────────────────────────────────
399
400fn escape_html(s: &str) -> String {
401    let mut out = String::with_capacity(s.len());
402    for c in s.chars() {
403        match c {
404            '&' => out.push_str("&amp;"),
405            '<' => out.push_str("&lt;"),
406            '>' => out.push_str("&gt;"),
407            '"' => out.push_str("&quot;"),
408            '\'' => out.push_str("&#x27;"),
409            // A raw CR in text content is normalised to LF by the HTML5 input
410            // preprocessor on re-import (CR-from-`&#xD;` survives, literal CR
411            // does not), which breaks serialiser idempotency. Emit it as a
412            // numeric reference so it round-trips losslessly.
413            '\r' => out.push_str("&#13;"),
414            _ => out.push(c),
415        }
416    }
417    out
418}
419
420/// Build a CSS `style` attribute value from block-level formatting (ISSUE-19).
421fn block_style_attr(block: &FragmentBlock) -> String {
422    use crate::Alignment;
423
424    let mut parts = Vec::new();
425    if let Some(ref alignment) = block.alignment {
426        let value = match alignment {
427            Alignment::Left => "left",
428            Alignment::Right => "right",
429            Alignment::Center => "center",
430            Alignment::Justify => "justify",
431        };
432        parts.push(format!("text-align: {}", value));
433    }
434    if let Some(n) = block.indent
435        && n > 0
436    {
437        parts.push(format!("margin-left: {}em", n));
438    }
439    if let Some(px) = block.text_indent
440        && px != 0
441    {
442        parts.push(format!("text-indent: {}px", px));
443    }
444    if let Some(px) = block.top_margin {
445        parts.push(format!("margin-top: {}px", px));
446    }
447    if let Some(px) = block.bottom_margin {
448        parts.push(format!("margin-bottom: {}px", px));
449    }
450    if let Some(px) = block.left_margin {
451        parts.push(format!("margin-left: {}px", px));
452    }
453    if let Some(px) = block.right_margin {
454        parts.push(format!("margin-right: {}px", px));
455    }
456    parts.join("; ")
457}
458
459fn push_inline_html(out: &mut String, elements: &[FragmentElement]) {
460    for elem in elements {
461        let text = match &elem.content {
462            InlineContent::Text(t) => escape_html(t),
463            // A reference carried onto the clipboard keeps its marker; the note
464            // body is not part of the fragment.
465            InlineContent::FootnoteRef { label } => {
466                let id = escape_html(label);
467                out.push_str(&format!(
468                    "<a epub:type=\"noteref\" role=\"doc-noteref\" href=\"#fn-{id}\"><sup>{id}</sup></a>"
469                ));
470                continue;
471            }
472            InlineContent::Image {
473                name,
474                alt,
475                width,
476                height,
477                ..
478            } => {
479                // This is the HTML written to the OS clipboard, so it is what
480                // another application receives on paste. It carried no `alt` at
481                // all, which made every copied image inaccessible in the target
482                // document.
483                let mut tag = format!(
484                    "<img src=\"{}\" alt=\"{}\"",
485                    escape_html(name),
486                    escape_html(alt)
487                );
488                if *width > 0 {
489                    tag.push_str(&format!(" width=\"{width}\""));
490                }
491                if *height > 0 {
492                    tag.push_str(&format!(" height=\"{height}\""));
493                }
494                tag.push('>');
495                tag
496            }
497            InlineContent::Empty => String::new(),
498        };
499
500        let is_monospace = elem
501            .fmt_font_family
502            .as_deref()
503            .is_some_and(|f| f == "monospace");
504        let is_bold = elem.fmt_font_bold.unwrap_or(false);
505        let is_italic = elem.fmt_font_italic.unwrap_or(false);
506        let is_underline = elem.fmt_font_underline.unwrap_or(false);
507        let is_strikeout = elem.fmt_font_strikeout.unwrap_or(false);
508        let is_anchor = elem.fmt_is_anchor.unwrap_or(false);
509        // Superscript and subscript are character formatting like the rest, and
510        // an exponent or a chemical index that lands on the baseline is simply
511        // the wrong text — so they belong in the clipboard payload alongside
512        // bold and italic rather than being dropped on the way out.
513        let vertical = elem.fmt_vertical_alignment.as_ref();
514
515        let mut result = text;
516
517        if is_monospace {
518            result = format!("<code>{}</code>", result);
519        }
520        if is_bold {
521            result = format!("<strong>{}</strong>", result);
522        }
523        if is_italic {
524            result = format!("<em>{}</em>", result);
525        }
526        if is_underline {
527            result = format!("<u>{}</u>", result);
528        }
529        if is_strikeout {
530            result = format!("<s>{}</s>", result);
531        }
532        match vertical {
533            Some(CharVerticalAlignment::SuperScript) => result = format!("<sup>{result}</sup>"),
534            Some(CharVerticalAlignment::SubScript) => result = format!("<sub>{result}</sub>"),
535            _ => {}
536        }
537        if is_anchor && let Some(ref href) = elem.fmt_anchor_href {
538            result = format!("<a href=\"{}\">{}</a>", escape_html(href), result);
539        }
540
541        out.push_str(&result);
542    }
543}
544
545/// Emit an HTML `<table>` for a `FragmentTable`.
546fn push_table_html(out: &mut String, table: &FragmentTable) {
547    out.push_str("<table>");
548    for row in 0..table.rows {
549        out.push_str("<tr>");
550        for col in 0..table.columns {
551            if let Some(cell) = table.cells.iter().find(|c| c.row == row && c.column == col) {
552                out.push_str("<td");
553                if cell.row_span > 1 {
554                    out.push_str(&format!(" rowspan=\"{}\"", cell.row_span));
555                }
556                if cell.column_span > 1 {
557                    out.push_str(&format!(" colspan=\"{}\"", cell.column_span));
558                }
559                out.push('>');
560                for (i, block) in cell.blocks.iter().enumerate() {
561                    if i > 0 {
562                        out.push_str("<br>");
563                    }
564                    push_inline_html(out, &block.elements);
565                }
566                out.push_str("</td>");
567            }
568            // Skip positions covered by spans — the HTML renderer handles them.
569        }
570        out.push_str("</tr>");
571    }
572    out.push_str("</table>");
573}
574
575// ── Markdown helpers ────────────────────────────────────────────
576
577fn escape_markdown(s: &str) -> String {
578    let mut out = String::with_capacity(s.len());
579    for c in s.chars() {
580        if matches!(
581            c,
582            '\\' | '`'
583                | '*'
584                | '_'
585                | '{'
586                | '}'
587                | '['
588                | ']'
589                | '('
590                | ')'
591                | '#'
592                | '+'
593                | '-'
594                | '.'
595                | '!'
596                | '|'
597                | '~'
598                | '<'
599                | '>'
600        ) {
601            out.push('\\');
602        }
603        out.push(c);
604    }
605    out
606}
607
608fn render_inline_markdown(elements: &[FragmentElement]) -> String {
609    let mut out = String::new();
610    for elem in elements {
611        let raw_text = match &elem.content {
612            InlineContent::Text(t) => t.clone(),
613            // `name` was used as both alt and source, so a pasted image
614            // described itself with its filename.
615            InlineContent::Image { name, alt, .. } => format!("![{alt}]({name})"),
616            InlineContent::FootnoteRef { label } => format!("[^{label}]"),
617            InlineContent::Empty => String::new(),
618        };
619
620        let is_monospace = elem
621            .fmt_font_family
622            .as_deref()
623            .is_some_and(|f| f == "monospace");
624        let is_bold = elem.fmt_font_bold.unwrap_or(false);
625        let is_italic = elem.fmt_font_italic.unwrap_or(false);
626        let is_strikeout = elem.fmt_font_strikeout.unwrap_or(false);
627        let is_anchor = elem.fmt_is_anchor.unwrap_or(false);
628
629        if is_monospace {
630            out.push('`');
631            out.push_str(&raw_text);
632            out.push('`');
633        } else {
634            let mut text = escape_markdown(&raw_text);
635            if is_bold && is_italic {
636                text = format!("***{}***", text);
637            } else if is_bold {
638                text = format!("**{}**", text);
639            } else if is_italic {
640                text = format!("*{}*", text);
641            }
642            if is_strikeout {
643                text = format!("~~{}~~", text);
644            }
645            if is_anchor {
646                let href = elem.fmt_anchor_href.as_deref().unwrap_or("");
647                out.push_str(&format!("[{}]({})", text, href));
648            } else {
649                out.push_str(&text);
650            }
651        }
652    }
653    out
654}
655
656/// Render a `FragmentTable` as a pipe-delimited Markdown table.
657fn render_table_markdown(table: &FragmentTable) -> String {
658    let mut rows: Vec<Vec<String>> = vec![vec![String::new(); table.columns]; table.rows];
659
660    for cell in &table.cells {
661        let text: String = cell
662            .blocks
663            .iter()
664            .map(|b| render_inline_markdown(&b.elements))
665            .collect::<Vec<_>>()
666            .join(" ");
667        if cell.row < table.rows && cell.column < table.columns {
668            rows[cell.row][cell.column] = text;
669        }
670    }
671
672    let mut out = String::new();
673    for (i, row) in rows.iter().enumerate() {
674        out.push_str("| ");
675        out.push_str(&row.join(" | "));
676        out.push_str(" |");
677        if i == 0 {
678            // Header separator
679            out.push('\n');
680            out.push('|');
681            for _ in 0..table.columns {
682                out.push_str(" --- |");
683            }
684        }
685        if i + 1 < rows.len() {
686            out.push('\n');
687        }
688    }
689    out
690}
691
692// ── Fragment construction from parsed content ───────────────────
693
694/// Convert parsed blocks (from HTML or Markdown parser) into a `DocumentFragment`.
695/// Convert a `ParsedSpan` to a `FragmentElement`.
696/// The plain text of a run of parsed spans, with an image standing as its
697/// `U+FFFC` sentinel.
698///
699/// That sentinel is the convention the *extract* side already writes (see
700/// `extract_fragment_uc`), and it is what `insert_fragment_uc` positions each
701/// `ImageAnchor` against — an image with no character in the text has nothing
702/// to anchor to, and every format run after it lands three bytes early.
703fn spans_plain_text(spans: &[ParsedSpan]) -> String {
704    spans
705        .iter()
706        .map(|s| {
707            // Every inline object contributes its sentinel: the anchors this
708            // fragment carries are positioned against *this* string, so an
709            // object missing from it puts every anchor after it out by three
710            // bytes.
711            if s.image.is_some() || s.footnote_ref.is_some() {
712                "\u{FFFC}"
713            } else {
714                s.text.as_str()
715            }
716        })
717        .collect()
718}
719
720fn span_to_fragment_element(span: &ParsedSpan) -> FragmentElement {
721    // An image span carries no text — the picture *is* the content. Emitting
722    // `Text("")` for it, which is what this did, produced a fragment with an
723    // empty element and no image anywhere: `insert_djot` of an image markup
724    // returned `Ok` and inserted nothing at all.
725    //
726    // A footnote reference is the same shape and the same trap: its span carries
727    // no text either, so the identical `Text("")` would have made
728    // `insert_footnote_reference` a no-op that reported success.
729    let content = match (&span.footnote_ref, &span.image) {
730        (Some(label), _) => InlineContent::FootnoteRef {
731            label: label.clone(),
732        },
733        (None, Some(img)) => InlineContent::Image {
734            name: img.src.clone(),
735            alt: img.alt.clone(),
736            width: img.width,
737            height: img.height,
738            // The source states a display size or it does not; nothing here
739            // re-encodes the image, so there is no quality to carry.
740            quality: 100,
741        },
742        (None, None) => InlineContent::Text(span.text.clone()),
743    };
744    let fmt_font_family = if span.code {
745        Some("monospace".into())
746    } else {
747        None
748    };
749    let fmt_font_bold = if span.bold { Some(true) } else { None };
750    let fmt_font_italic = if span.italic { Some(true) } else { None };
751    let fmt_font_underline = if span.underline { Some(true) } else { None };
752    let fmt_font_strikeout = if span.strikeout { Some(true) } else { None };
753    let (fmt_anchor_href, fmt_is_anchor) = if let Some(ref href) = span.link_href {
754        (Some(href.clone()), Some(true))
755    } else {
756        (None, None)
757    };
758    // Raised and lowered text is character formatting like bold and italic, and
759    // dropping it here is what silently flattened an exponent or a chemical
760    // index on the way in: the parsers set the flags, `character_format_from_span`
761    // maps them, and only this conversion — the one every `insert_html` /
762    // `insert_markdown` / `insert_djot` goes through — threw them away.
763    let fmt_vertical_alignment = if span.superscript {
764        Some(CharVerticalAlignment::SuperScript)
765    } else if span.subscript {
766        Some(CharVerticalAlignment::SubScript)
767    } else {
768        None
769    };
770
771    FragmentElement {
772        content,
773        fmt_font_family,
774        fmt_font_point_size: None,
775        fmt_font_weight: None,
776        fmt_font_bold,
777        fmt_font_italic,
778        fmt_font_underline,
779        fmt_font_overline: None,
780        fmt_font_strikeout,
781        fmt_letter_spacing: None,
782        fmt_word_spacing: None,
783        fmt_anchor_href,
784        fmt_anchor_names: vec![],
785        fmt_is_anchor,
786        fmt_tooltip: None,
787        fmt_underline_style: None,
788        fmt_vertical_alignment,
789    }
790}
791
792/// A parsed block as a fragment block: its text and spans, and everything a load of the
793/// same text keeps of the block, so a text put back by selecting all and inserting it reads
794/// as the text loaded: its alignment, marker, spacing and list markers, and its code-block
795/// format when `keep_code_block` says so. Only the heading and the line attributes were
796/// carried, and every code block of a restored version came back as a plain paragraph. The
797/// quotations the block stands in travel beside it (see [`FragmentQuoting`]).
798fn parsed_block_to_fragment_block(pb: ParsedBlock, keep_code_block: bool) -> FragmentBlock {
799    use frontend::common::parser_tools::fragment_schema::FragmentList;
800
801    let elements: Vec<FragmentElement> = pb.spans.iter().map(span_to_fragment_element).collect();
802    let plain_text: String = spans_plain_text(&pb.spans);
803    let list = pb.list_style.map(|style| FragmentList {
804        style,
805        indent: pb.list_indent as i64,
806        prefix: pb.list_prefix,
807        suffix: pb.list_suffix,
808    });
809    let is_code_block = keep_code_block && pb.is_code_block;
810    FragmentBlock {
811        plain_text,
812        elements,
813        heading_level: pb.heading_level,
814        list,
815        alignment: pb.alignment,
816        indent: None,
817        text_indent: pb.text_indent,
818        marker: pb.marker,
819        top_margin: pb.top_margin,
820        bottom_margin: None,
821        left_margin: None,
822        right_margin: None,
823        tab_positions: vec![],
824        line_height: pb.line_height,
825        non_breakable_lines: pb.non_breakable_lines,
826        page_break_before: pb.page_break_before,
827        direction: pb.direction,
828        background_color: pb.background_color,
829        is_code_block: is_code_block.then_some(true),
830        code_language: if is_code_block {
831            pb.code_language
832        } else {
833            None
834        },
835        hyphenate: None,
836        language: None,
837    }
838}
839
840/// What a fragment was parsed from, which decides what an insertion makes of it.
841#[derive(Debug, Clone, Copy, PartialEq, Eq)]
842enum ParsedFrom {
843    /// A text in a syntax a text is saved in, Djot or Markdown: a whole text, whose code
844    /// blocks are code blocks. Inserted over a selection holding the whole of a text, it
845    /// reads as the same text loaded (see `whole_text_to_json`).
846    SavedSyntax,
847    /// HTML, as a clipboard carries a passage from a web page or another application. A
848    /// `<pre>` is read as paragraphs, one for each of its lines: a code block keeps no
849    /// emphasis, takes no footnote reference, and a host with no command to make it prose
850    /// again left the writer with a passage set as code.
851    Html,
852}
853
854/// Convert parsed elements (blocks + tables) into a `DocumentFragment`,
855/// preserving table structure as `FragmentTable` entries.
856fn parsed_elements_to_fragment(parsed: Vec<ParsedElement>, from: ParsedFrom) -> DocumentFragment {
857    let mut blocks: Vec<FragmentBlock> = Vec::new();
858    let mut tables: Vec<FragmentTable> = Vec::new();
859    // The quotations each block and table stands in, carried beside them.
860    let mut quoting = FragmentQuoting::default();
861    // The number each list starts at, carried beside its items: every item of a list
862    // starting past 1 names it, and an insertion reads it where it makes the list.
863    let mut list_starts = FragmentListStarts::default();
864
865    for elem in parsed {
866        match elem {
867            // A clipboard fragment carries prose, not note bodies: a definition
868            // has no position in the flow being copied, and pasting one would
869            // splice a note's text into the middle of a sentence. The reference
870            // travels; the body stays where it is defined.
871            ParsedElement::FootnoteDefinition { .. } => {}
872            // A code block of a text in a saved syntax is a code block, and holds its lines.
873            // Any other block is prose, and a line break in prose is a new block: one line a
874            // block, as a plain-text paste gives. A pasted `<pre>` is prose too (see
875            // `ParsedFrom::Html`). Kept in one block, it held line breaks, which the Djot
876            // writer used to write as they were, and its lines read back as whatever their
877            // markers spelled.
878            ParsedElement::Block(pb) => {
879                let depth = pb.blockquote_depth;
880                // The quotations opening at the block open at its first line.
881                let mut opens = pb.blockquote_opens;
882                // The quotation's role rides on its first block, as the parsers put it.
883                let mut role = pb.semantic_role.clone();
884                let keep_code_block = pb.is_code_block && from == ParsedFrom::SavedSyntax;
885                let lines = if keep_code_block {
886                    vec![pb]
887                } else {
888                    split_block_at_line_breaks(pb)
889                };
890                for pb in lines {
891                    if let Some(start) = pb.list_start.filter(|_| pb.list_style.is_some()) {
892                        list_starts.set(blocks.len(), start);
893                    }
894                    blocks.push(parsed_block_to_fragment_block(pb, keep_code_block));
895                    if depth > 0 {
896                        quoting.set_block(blocks.len() - 1, depth, role.take());
897                        quoting.set_block_opens(blocks.len() - 1, std::mem::take(&mut opens));
898                    }
899                }
900            }
901            ParsedElement::Table(pt) => {
902                let block_insert_index = blocks.len();
903                let num_columns = pt.column_count();
904                let num_rows = pt.rows.len();
905
906                let mut frag_cells: Vec<FragmentTableCell> = Vec::new();
907                for (row_idx, row) in pt.rows.iter().enumerate() {
908                    // A row shorter than the widest is completed with empty cells, as a
909                    // load completes it: a table with holes in its grid is one no edit
910                    // expects.
911                    for col_idx in 0..num_columns {
912                        let spans = row
913                            .get(col_idx)
914                            .map_or(&[][..], |cell| cell.spans.as_slice());
915                        let cell_elements: Vec<FragmentElement> =
916                            spans.iter().map(span_to_fragment_element).collect();
917                        let cell_text: String = spans_plain_text(spans);
918
919                        frag_cells.push(FragmentTableCell {
920                            row: row_idx,
921                            column: col_idx,
922                            row_span: 1,
923                            column_span: 1,
924                            blocks: vec![FragmentBlock {
925                                plain_text: cell_text,
926                                elements: cell_elements,
927                                heading_level: None,
928                                list: None,
929                                alignment: None,
930                                indent: None,
931                                text_indent: None,
932                                marker: None,
933                                top_margin: None,
934                                bottom_margin: None,
935                                left_margin: None,
936                                right_margin: None,
937                                tab_positions: vec![],
938                                line_height: None,
939                                non_breakable_lines: None,
940                                page_break_before: None,
941                                direction: None,
942                                background_color: None,
943                                is_code_block: None,
944                                code_language: None,
945                                hyphenate: None,
946                                language: None,
947                            }],
948                            fmt_padding: None,
949                            fmt_border: None,
950                            fmt_vertical_alignment: None,
951                            fmt_background_color: None,
952                        });
953                    }
954                }
955
956                tables.push(FragmentTable {
957                    rows: num_rows,
958                    columns: num_columns,
959                    cells: frag_cells,
960                    block_insert_index,
961                    fmt_border: None,
962                    fmt_cell_spacing: None,
963                    fmt_cell_padding: None,
964                    fmt_width: None,
965                    fmt_alignment: None,
966                    column_widths: vec![],
967                });
968                quoting.set_table(tables.len() - 1, pt.blockquote_depth);
969                quoting.set_table_opens(tables.len() - 1, pt.blockquote_opens);
970            }
971        }
972    }
973
974    // The blocks' own text, read before they are serialized: reading it back out of the
975    // JSON parsed the whole fragment a second time.
976    let plain_text = blocks
977        .iter()
978        .map(|b| b.plain_text.as_str())
979        .collect::<Vec<_>>()
980        .join("\n");
981    let data = FragmentData { blocks, tables };
982    let data = fragment_with_list_starts_to_json(
983        &data,
984        &quoting,
985        &list_starts,
986        from == ParsedFrom::SavedSyntax,
987    )
988    .expect("fragment serialization should not fail");
989
990    DocumentFragment { data, plain_text }
991}
992
993#[cfg(test)]
994mod tests {
995    use super::*;
996
997    /// A paragraph as a manuscript holds it: emphasis, a link, a note reference.
998    fn paragraph(i: usize) -> String {
999        format!(
1000            "Paragraph {i} with *emphasis* and a [link](https://example.org/{i}) in it, and a \
1001             note[^n{i}] after the words that run on to a full line of prose."
1002        )
1003    }
1004
1005    /// A fragment is the text a cursor inserts, carried as JSON, and an insertion refuses
1006    /// JSON past 64 MiB. Every field of every block and every run was written, `null` or
1007    /// empty as most are: the JSON came to about 18 times the Djot it was made from, and
1008    /// putting back a whole text of 26,000 paragraphs failed. Fields holding nothing are
1009    /// left out now.
1010    #[test]
1011    fn a_fragment_holds_little_more_than_its_text() {
1012        let djot: String = (0..200).map(|i| format!("{}\n\n", paragraph(i))).collect();
1013        let fragment = DocumentFragment::from_djot(&djot);
1014        let ratio = fragment.raw_data().len() as f64 / djot.len() as f64;
1015        println!("fragment JSON: {ratio:.2} times the Djot");
1016        assert!(
1017            ratio < 4.0,
1018            "the fragment's JSON is {ratio:.1} times the Djot it was made from"
1019        );
1020        assert_eq!(fragment.to_plain_text().lines().count(), 200);
1021    }
1022
1023    /// A fragment written in full, as the builds before the compact form wrote it, still
1024    /// reads: every field left out reads as holding nothing.
1025    #[test]
1026    fn a_fragment_written_in_full_still_reads() {
1027        let full = r#"{"blocks":[{"plain_text":"Old","elements":[{"content":{"Text":"Old"},
1028            "fmt_font_family":null,"fmt_font_point_size":null,"fmt_font_weight":null,
1029            "fmt_font_bold":true,"fmt_font_italic":null,"fmt_font_underline":null,
1030            "fmt_font_overline":null,"fmt_font_strikeout":null,"fmt_letter_spacing":null,
1031            "fmt_word_spacing":null,"fmt_anchor_href":null,"fmt_anchor_names":[],
1032            "fmt_is_anchor":null,"fmt_tooltip":null,"fmt_underline_style":null,
1033            "fmt_vertical_alignment":null}],"heading_level":2,"list":null,"alignment":null,
1034            "indent":null,"text_indent":null,"marker":null,"top_margin":null,
1035            "bottom_margin":null,"left_margin":null,"right_margin":null,"tab_positions":[],
1036            "line_height":null,"non_breakable_lines":null,"direction":null,
1037            "background_color":null,"is_code_block":null,"code_language":null}]}"#;
1038        let data: FragmentData = serde_json::from_str(full).unwrap();
1039        assert_eq!(data.blocks[0].heading_level, Some(2));
1040        assert_eq!(data.blocks[0].elements[0].fmt_font_bold, Some(true));
1041        let compact = serde_json::to_string(&data).unwrap();
1042        let again: FragmentData = serde_json::from_str(&compact).unwrap();
1043        assert_eq!(again.blocks[0].heading_level, Some(2));
1044        assert_eq!(again.blocks[0].elements[0].fmt_font_bold, Some(true));
1045        assert!(compact.len() < full.len() / 3, "{compact}");
1046    }
1047}