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