GitCorner

Code

ossartcraftdesigncraft๐ŸŒ
88f5a00e389b
//! Structure and XML: tags on frames (Tags panel), paragraph styles mapped to tags, the structure
//! tree, XML export and import (content flows into the frames with matching tags).

use designcraft_doc::{Content, Document, Item, ItemId, ParaFormat, Story, StoryId, XmlTag};
use serde_json::{Value, json};

use super::{CommandSpec, bad, cmd, has_doc, ids_param, ok, str_param};
use crate::{Result, Session};

const COLORS: [[u8; 3]; 6] = [[230, 120, 40], [60, 140, 220], [70, 170, 90], [200, 70, 160], [150, 110, 220], [200, 170, 40]];

fn root_name(d: &Document) -> String {
    if d.xml.root.is_empty() { "Root".into() } else { d.xml.root.clone() }
}

fn valid_name(n: &str) -> bool {
    let mut c = n.chars();
    c.next().is_some_and(|f| f.is_alphabetic() || f == '_') && n.chars().all(|ch| ch.is_alphanumeric() || matches!(ch, '_' | '-' | '.'))
}

/// Tagged objects in reading order (pages, then top to bottom, left to right), nested ones included.
fn tagged(d: &Document) -> Vec<ItemId> {
    let mut keyed: Vec<((usize, i64, i64), ItemId)> = Vec::new();
    for (si, sp) in d.spreads.iter().enumerate() {
        let first = d.first_page_of_spread(si);
        for top in &sp.items {
            top.walk(&mut |it: &Item| {
                if !it.xml_tag.is_empty() {
                    let b = it.bounds();
                    let page = first + sp.page_at_x(b.center().x).unwrap_or(0);
                    keyed.push(((page, (b.y0 * 10.0) as i64, (b.x0 * 10.0) as i64), it.id));
                }
            });
        }
    }
    keyed.sort_by_key(|(k, _)| *k);
    keyed.into_iter().map(|(_, i)| i).collect()
}

/// Escapes markup characters and drops characters XML 1.0 cannot hold.
fn esc(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\t' | '\n' | '\r' => out.push(c),
            c if (c as u32) < 0x20 || c == '\u{FFFE}' || c == '\u{FFFF}' => {}
            c => out.push(c),
        }
    }
    out
}

/// Plain text of a story (markers dropped, forced breaks as newlines).
fn plain(t: &str) -> String {
    t.chars().filter(|c| !('\u{E000}'..='\u{E1FF}').contains(c)).map(|c| if c == '\u{2028}' { '\n' } else { c }).collect()
}

/// A paragraph's text with inline-tagged runs as elements.
/// `runs` are the story's runs in order (collected once per story): only those reaching the
/// paragraph are visited, found by binary search.
fn para_xml(st: &designcraft_doc::Story, runs: &[(std::ops::Range<usize>, &designcraft_doc::CharFormat)], r: std::ops::Range<usize>) -> String {
    let mut out = String::new();
    let mut open: Option<String> = None;
    let first = runs.partition_point(|(rr, _)| rr.end <= r.start);
    for (rr, f) in runs.iter().skip(first).take_while(|(rr, _)| rr.start < r.end) {
        let (a, b) = (rr.start.max(r.start), rr.end.min(r.end));
        if a >= b {
            continue;
        }
        let tag = f.over.xml_tag.clone().filter(|t| !t.is_empty());
        if tag != open {
            if let Some(t) = open.take() {
                out.push_str(&format!("</{t}>"));
            }
            if let Some(t) = &tag {
                out.push_str(&format!("<{t}>"));
            }
            open = tag;
        }
        out.push_str(&esc(&plain(&st.text[a..b])));
    }
    if let Some(t) = open {
        out.push_str(&format!("</{t}>"));
    }
    out
}

/// The document's tagged content as XML.
pub fn export_xml(d: &Document) -> String {
    let mut out = format!("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<{}>\n", root_name(d));
    for id in tagged(d) {
        let Some(it) = d.item(id) else { continue };
        let tag = &it.xml_tag;
        match &it.content {
            Content::Text(tf) => {
                let Some(st) = d.story(tf.story) else { continue };
                let mapped = |style: &str| d.xml.style_map.iter().find(|(s, _)| s == style).map(|(_, t)| t.clone());
                // Paragraph text with inline tags already escaped.
                let paras: Vec<(Option<String>, String)> = {
                    let runs: Vec<_> = st.runs().collect();
                    st.para_ranges().into_iter().enumerate().map(|(i, r)| (mapped(&st.paras[i].style), para_xml(st, &runs, r))).collect()
                };
                if paras.iter().any(|(m, _)| m.is_some()) {
                    out.push_str(&format!("  <{tag}>\n"));
                    for (m, text) in paras {
                        match m {
                            Some(t) => out.push_str(&format!("    <{t}>{text}</{t}>\n")),
                            None => out.push_str(&format!("    {text}\n")),
                        }
                    }
                    out.push_str(&format!("  </{tag}>\n"));
                } else {
                    out.push_str(&format!("  <{tag}>{}</{tag}>\n", paras.into_iter().map(|(_, t)| t).collect::<Vec<_>>().join("\n")));
                }
            }
            Content::Graphic(g) => {
                let name = d.assets.get(&g.asset).map(|a| a.link.clone().unwrap_or_else(|| a.name.clone())).unwrap_or_default();
                out.push_str(&format!("  <{tag} href=\"file://{}\"/>\n", esc(&name)));
            }
            _ => out.push_str(&format!("  <{tag}/>\n")),
        }
    }
    out.push_str(&format!("</{}>\n", root_name(d)));
    out
}

/// One element under the root: its name and either text or (child name, text) paragraphs.
struct Element {
    name: String,
    text: String,
    children: Vec<(String, String)>,
}

fn parse(xml: &str) -> std::result::Result<Vec<Element>, String> {
    fn descendant_text(element: &craft_xml::El, into: &mut String) {
        for node in &element.children {
            match node {
                craft_xml::Node::Text(text) | craft_xml::Node::CData(text) => into.push_str(text),
                craft_xml::Node::El(child) => descendant_text(child, into),
                craft_xml::Node::Pi { .. } | craft_xml::Node::Comment(_) => {}
            }
        }
    }

    let mut options = craft_xml::Options::local();
    options.strict = true;
    let root = options.parse(xml.as_bytes()).map_err(|e| e.to_string())?;
    root.elements()
        .map(|element| {
            let mut children = Vec::new();
            for child in element.elements() {
                let mut text = String::new();
                descendant_text(child, &mut text);
                children.push((child.name.clone(), text));
            }
            let mut text = String::new();
            for node in &element.children {
                match node {
                    craft_xml::Node::Text(value) | craft_xml::Node::CData(value) => text.push_str(value),
                    craft_xml::Node::Pi { .. } | craft_xml::Node::Comment(_) => {}
                    craft_xml::Node::El(_) => {}
                }
            }
            Ok(Element { name: element.name.clone(), text, children })
        })
        .collect()
}

pub fn specs() -> Vec<CommandSpec> {
    vec![
        cmd!(query "xml.tags", "Tags", [], None, "{} โ†’ {root, tags: [{name, color}], styleMap: [[style, tag]], dtd: bool}", has_doc, |s, _| {
            let d = &s.doc()?.doc;
            Ok(json!({"root": root_name(d), "tags": d.xml.tags, "styleMap": d.xml.style_map, "dtd": !d.xml.dtd.is_empty()}))
        }),
        cmd!("xml.newTag", "New Tag", ["Window", "Utilities", "Tags"], None, "{name, color?: [r, g, b]}", has_doc, |s, p| {
            let name = str_param(p, "name").unwrap_or("").trim().to_string();
            if !valid_name(&name) {
                return Err(bad("xml.newTag", format!("`{name}` isn't a valid XML name")));
            }
            s.edit(|d, _| {
                if !d.xml.tags.iter().any(|t| t.name == name) {
                    let color = COLORS[d.xml.tags.len() % COLORS.len()];
                    d.xml.tags.push(XmlTag { name: name.clone(), color });
                }
                Ok(json!({"name": name}))
            })
        }),
        cmd!("xml.deleteTag", "Delete Tag", [], None, "{name} โ€” tagged objects are untagged", has_doc, |s, p| {
            let name = str_param(p, "name").unwrap_or("").to_string();
            let ids: Vec<ItemId> = {
                let d = &s.doc()?.doc;
                tagged(d).into_iter().filter(|i| d.item(*i).is_some_and(|it| it.xml_tag == name)).collect()
            };
            s.edit(|d, _| {
                d.xml.tags.retain(|t| t.name != name);
                d.xml.style_map.retain(|(_, t)| *t != name);
                for id in &ids {
                    if let Some(it) = d.item_mut(*id) {
                        it.xml_tag.clear();
                    }
                }
                ok()
            })
        }),
        cmd!("xml.tagText", "Tag Text", [], None, "{tag | null (untag)} โ€” the selected text becomes an inline element", super::has_doc, |s, p| {
            let tag = str_param(p, "tag").map(str::to_string);
            if let Some(t) = &tag
                && !valid_name(t)
            {
                return Err(bad("xml.tagText", format!("`{t}` isn't a valid XML name")));
            }
            if s.doc()?.selection.text.is_none_or(|t| t.range().is_empty()) {
                return Err(bad("xml.tagText", "select some text"));
            }
            if let Some(t) = &tag {
                s.edit(|d, _| {
                    if !d.xml.tags.iter().any(|x| x.name == *t) {
                        let color = COLORS[d.xml.tags.len() % COLORS.len()];
                        d.xml.tags.push(XmlTag { name: t.clone(), color });
                    }
                    ok()
                })?;
            }
            s.execute("type.char", &json!({"xmlTag": tag.unwrap_or_default()}))
        }),
        cmd!("xml.tag", "Tag Frame", [], None, "{tag (made if new), ids? (default: the selection)} โ€” `tag: null` untags", has_doc, |s, p| {
            let tag = str_param(p, "tag").map(str::to_string);
            if let Some(t) = &tag
                && !valid_name(t)
            {
                return Err(bad("xml.tag", format!("`{t}` isn't a valid XML name")));
            }
            let ids = ids_param(p, "ids").unwrap_or_else(|| s.active().map(|d| d.selection.items.clone()).unwrap_or_default());
            if ids.is_empty() {
                return Err(bad("xml.tag", "select objects to tag"));
            }
            s.edit(|d, _| {
                if let Some(t) = &tag
                    && !d.xml.tags.iter().any(|x| x.name == *t)
                {
                    let color = COLORS[d.xml.tags.len() % COLORS.len()];
                    d.xml.tags.push(XmlTag { name: t.clone(), color });
                }
                for id in &ids {
                    if let Some(it) = d.item_mut(*id) {
                        it.xml_tag = tag.clone().unwrap_or_default();
                    }
                }
                ok()
            })
        }),
        cmd!(
            "xml.mapStyle",
            "Map Styles to Tags",
            [],
            None,
            "{style: paragraph style, tag | null} โ€” that style's paragraphs export as `<tag>` elements and import back with it",
            has_doc,
            |s, p| {
                let style = str_param(p, "style").unwrap_or("").to_string();
                let tag = str_param(p, "tag").map(str::to_string);
                if let Some(t) = &tag
                    && !valid_name(t)
                {
                    return Err(bad("xml.mapStyle", format!("`{t}` isn't a valid XML name")));
                }
                if s.doc()?.doc.styles.para(&style).is_none() {
                    return Err(bad("xml.mapStyle", format!("no paragraph style `{style}`")));
                }
                s.edit(|d, _| {
                    d.xml.style_map.retain(|(st, _)| *st != style);
                    if let Some(t) = tag {
                        if !d.xml.tags.iter().any(|x| x.name == t) {
                            let color = COLORS[d.xml.tags.len() % COLORS.len()];
                            d.xml.tags.push(XmlTag { name: t.clone(), color });
                        }
                        d.xml.style_map.push((style.clone(), t));
                    }
                    ok()
                })
            }
        ),
        cmd!(query "xml.structure", "Structure", [], None, "{} โ†’ {root, elements: [{tag, id, kind, text?}]} in reading order", has_doc, |s, _| {
            let d = &s.doc()?.doc;
            let elements: Vec<Value> = tagged(d)
                .into_iter()
                .filter_map(|id| d.item(id))
                .map(|it| {
                    let (kind, text) = match &it.content {
                        Content::Text(tf) => ("text", d.story(tf.story).map(|st| plain(&st.text))),
                        Content::Graphic(_) => ("graphic", None),
                        _ => ("object", None),
                    };
                    json!({"tag": it.xml_tag, "id": it.id.0, "kind": kind, "text": text})
                })
                .collect();
            Ok(json!({"root": root_name(d), "elements": elements}))
        }),
        cmd!(noundo "file.exportXml", "Export XMLโ€ฆ", ["File"], None, "{path?} โ€” the tagged content in reading order โ†’ {path, bytes} (no path: {text})", has_doc, |s, p| {
            let text = export_xml(&s.doc()?.doc);
            match str_param(p, "path") {
                Some(path) => {
                    #[cfg(not(target_arch = "wasm32"))]
                    crate::io::write_file(path, text.as_bytes()).map_err(crate::EngineError::Other)?;
                    Ok(json!({"path": path, "bytes": text.len()}))
                }
                None => Ok(json!({"text": text, "bytes": text.len()})),
            }
        }),
        cmd!(
            "xml.loadDtd",
            "Load DTDโ€ฆ",
            ["Window", "Utilities", "Tags"],
            None,
            "{path | text} โ€” keep the DTD for validation and add a tag for each declared element; the root takes the first element's name unless already named โ†’ {elements}",
            has_doc,
            |s, p| {
                let text = text_or_path(p, "xml.loadDtd")?;
                let dtd = crate::dtd::parse(&text).map_err(|e| bad("xml.loadDtd", e))?;
                let names: Vec<String> = dtd.element_names().map(str::to_string).collect();
                s.edit(|d, _| {
                    // The tags there are, then those added: one set, not a search per element.
                    let mut known: std::collections::HashSet<String> = d.xml.tags.iter().map(|t| t.name.clone()).collect();
                    for n in &names {
                        if valid_name(n) && known.insert(n.clone()) {
                            let color = COLORS[d.xml.tags.len() % COLORS.len()];
                            d.xml.tags.push(XmlTag { name: n.clone(), color });
                        }
                    }
                    if d.xml.root.is_empty()
                        && let Some(first) = names.first()
                    {
                        d.xml.root = first.clone();
                    }
                    d.xml.dtd = text.clone();
                    Ok(json!({"elements": names.len()}))
                })
            }
        ),
        cmd!(
            "xml.deleteDtd",
            "Delete DTD",
            [],
            None,
            "{}",
            |s: &Session| { if s.doc().is_ok_and(|d| !d.doc.xml.dtd.is_empty()) { Ok(()) } else { Err("no DTD loaded".into()) } },
            |s, _| s.edit(|d, _| {
                d.xml.dtd.clear();
                ok()
            })
        ),
        cmd!(
            query "xml.validate",
            "Validate from Root Element",
            [],
            None,
            "{} โ€” check the structure (as exported) against the loaded DTD โ†’ {valid, problems: [{path, message}]}",
            |s: &Session| {
                if s.doc().is_ok_and(|d| !d.doc.xml.dtd.is_empty()) { Ok(()) } else { Err("load a DTD first".into()) }
            },
            |s, _| {
                let d = &s.doc()?.doc;
                let dtd = crate::dtd::parse(&d.xml.dtd).map_err(|e| bad("xml.validate", e))?;
                let problems = crate::dtd::validate(&dtd, &export_xml(d));
                Ok(json!({"valid": problems.is_empty(), "problems": problems}))
            }
        ),
        cmd!(
            "file.importXml",
            "Import XMLโ€ฆ",
            ["File"],
            None,
            "{path | text} โ€” each element's content goes into the next text frame with that tag (child elements of mapped tags become paragraphs in their style) โ†’ {placed, unmatched}",
            has_doc,
            import_xml
        ),
    ]
}

/// The `text` parameter, or the file at `path`.
fn text_or_path(p: &Value, id: &str) -> Result<String> {
    Ok(match (str_param(p, "text"), str_param(p, "path")) {
        (Some(t), _) => t.to_string(),
        (None, Some(path)) => {
            #[cfg(not(target_arch = "wasm32"))]
            let t = crate::io::read_to_string(path).map_err(crate::EngineError::Other)?;
            #[cfg(target_arch = "wasm32")]
            let t = {
                let _ = path;
                String::new()
            };
            t
        }
        _ => return Err(bad(id, "`path` or `text` required")),
    })
}

fn import_xml(s: &mut Session, p: &Value) -> Result<Value> {
    const ID: &str = "file.importXml";
    let text = text_or_path(p, ID)?;
    let elements = parse(&text).map_err(|e| bad(ID, format!("not well-formed XML: {e}")))?;
    let d = &s.doc()?.doc;
    // Frames by tag, in reading order; each element takes the next free one.
    let mut slots: Vec<(String, StoryId, bool)> =
        tagged(d).into_iter().filter_map(|id| d.item(id)).filter_map(|it| it.text_frame().map(|tf| (it.xml_tag.clone(), tf.story, false))).collect();
    let reverse: Vec<(String, String)> = d.xml.style_map.iter().map(|(st, t)| (t.clone(), st.clone())).collect();
    let mut fills: Vec<(StoryId, Vec<(Option<String>, String)>)> = Vec::new();
    let mut unmatched = 0;
    for el in elements {
        let Some(slot) = slots.iter_mut().find(|(t, _, used)| *t == el.name && !used) else {
            unmatched += 1;
            continue;
        };
        slot.2 = true;
        let paras: Vec<(Option<String>, String)> = if el.children.is_empty() {
            el.text.trim().split('\n').map(|l| (None, l.trim().to_string())).collect()
        } else {
            el.children
                .into_iter()
                .map(|(t, txt)| (reverse.iter().find(|(tag, _)| *tag == t).map(|(_, st)| st.clone()), txt.trim().to_string()))
                .collect()
        };
        fills.push((slot.1, paras));
    }
    let placed = fills.len();
    s.edit(|d, _| {
        for (sid, paras) in fills {
            let Some(st) = d.story_mut(sid) else { continue };
            let base = st.paras.first().cloned().unwrap_or_default();
            let text = paras.iter().map(|(_, t)| t.as_str()).collect::<Vec<_>>().join("\n");
            let frames = std::mem::take(&mut st.frames);
            let mut fresh = Story::with_text(sid, &text, ParaFormat { table: None, ..base.clone() });
            for (i, (style, _)) in paras.iter().enumerate() {
                if let (Some(sty), Some(pf)) = (style, fresh.paras.get_mut(i)) {
                    pf.style = sty.clone();
                }
            }
            fresh.frames = frames;
            fresh.rev = st.rev.wrapping_add(1);
            *st = fresh;
        }
        Ok(json!({"placed": placed, "unmatched": unmatched}))
    })
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use crate::Session;

    #[test]
    fn dtd_loads_tags_and_validates_the_structure() {
        let mut s = Session::new();
        s.execute("file.new", &json!({})).unwrap();
        assert!(s.execute("xml.validate", &json!({})).is_err(), "no DTD yet");
        let dtd = "<!ELEMENT article (headline, story+)><!ELEMENT headline (#PCDATA)><!ELEMENT story (#PCDATA | term)*><!ELEMENT term (#PCDATA)>";
        let r = s.execute("xml.loadDtd", &json!({"text": dtd})).unwrap();
        assert_eq!(r["elements"], 4);
        let tags = s.execute("xml.tags", &json!({})).unwrap();
        assert_eq!(tags["root"], "article");
        assert_eq!(tags["tags"].as_array().unwrap().len(), 4);
        let a = s.execute("frame.create", &json!({"rect": [72, 72, 400, 150], "content": "text", "text": "Body", "caret": false})).unwrap();
        s.execute("xml.tag", &json!({"tag": "story", "ids": [a["id"]]})).unwrap();
        let v = s.execute("xml.validate", &json!({})).unwrap();
        assert_eq!(v["valid"], false);
        assert!(v["problems"][0]["message"].as_str().unwrap().contains("the DTD asks for (headline, story+)"), "{v}");
        let h = s.execute("frame.create", &json!({"rect": [72, 20, 400, 60], "content": "text", "text": "Head", "caret": false})).unwrap();
        s.execute("xml.tag", &json!({"tag": "headline", "ids": [h["id"]]})).unwrap();
        assert_eq!(s.execute("xml.validate", &json!({})).unwrap()["valid"], true);
        assert!(s.execute("xml.loadDtd", &json!({"text": "<!ELEMENT a (b,|c)>"})).is_err());
        s.execute("xml.deleteDtd", &json!({})).unwrap();
        assert!(s.execute("xml.validate", &json!({})).is_err());
    }

    #[test]
    fn export_xml_drops_forbidden_control_chars() {
        let mut s = Session::new();
        s.execute("file.new", &json!({})).unwrap();
        let a = s
            .execute("frame.create", &json!({"rect": [72, 72, 400, 150], "content": "text", "text": "A\u{1}B\u{8}C\u{FFFE}\u{FFFF}", "caret": false}))
            .unwrap();
        s.execute("xml.tag", &json!({"tag": "story", "ids": [a["id"]]})).unwrap();
        let x = s.execute("file.exportXml", &json!({})).unwrap()["text"].as_str().unwrap().to_string();
        assert!(x.contains("<story>ABC</story>"), "{x:?}");
    }

    #[test]
    fn map_style_rejects_invalid_tag_name() {
        let mut s = Session::new();
        s.execute("file.new", &json!({})).unwrap();
        s.execute("style.paragraph.create", &json!({"name": "Head"})).unwrap();
        assert!(s.execute("xml.mapStyle", &json!({"style": "Head", "tag": "a b><x"})).is_err());
        let d = &s.doc().unwrap().doc;
        assert!(d.xml.style_map.is_empty());
        assert!(d.xml.tags.iter().all(|t| t.name != "a b><x"));
        s.execute("xml.mapStyle", &json!({"style": "Head", "tag": "title"})).unwrap();
        assert_eq!(s.doc().unwrap().doc.xml.style_map.len(), 1);
    }

    #[test]
    fn import_xml_keeps_text_of_inline_tags_in_mapped_paragraphs() {
        let mut s = Session::new();
        s.execute("file.new", &json!({})).unwrap();
        s.execute("style.paragraph.create", &json!({"name": "Head"})).unwrap();
        let a = s
            .execute("frame.create", &json!({"rect": [72, 72, 400, 150], "content": "text", "text": "See the glossary entry", "caret": false}))
            .unwrap();
        s.execute("text.select", &json!({"story": a["story"], "anchor": 0, "focus": 0})).unwrap();
        s.execute("style.paragraph.apply", &json!({"name": "Head"})).unwrap();
        s.execute("xml.tag", &json!({"tag": "story", "ids": [a["id"]]})).unwrap();
        s.execute("xml.mapStyle", &json!({"style": "Head", "tag": "title"})).unwrap();
        s.execute("text.select", &json!({"story": a["story"], "anchor": 8, "focus": 16})).unwrap();
        s.execute("xml.tagText", &json!({"tag": "term"})).unwrap();
        let x = s.execute("file.exportXml", &json!({})).unwrap()["text"].as_str().unwrap().to_string();
        assert!(x.contains("<title>See the <term>glossary</term> entry</title>"), "{x}");
        s.execute("file.importXml", &json!({"text": x})).unwrap();
        let d = &s.doc().unwrap().doc;
        assert_eq!(d.stories[&designcraft_doc::StoryId(a["story"].as_u64().unwrap())].text, "See the glossary entry");
    }

    #[test]
    fn parse_keeps_text_nested_below_a_child_element() {
        let els = super::parse("<root><story><note>See the <term>x</term> y</note></story></root>").unwrap();
        assert_eq!(els.len(), 1);
        assert_eq!(els[0].children, vec![("note".to_string(), "See the x y".to_string())]);
    }

    #[test]
    fn tag_export_and_import_xml() {
        let mut s = Session::new();
        s.execute("file.new", &json!({})).unwrap();
        s.execute("style.paragraph.create", &json!({"name": "Head"})).unwrap();
        let a = s
            .execute("frame.create", &json!({"rect": [72, 72, 400, 150], "content": "text", "text": "Title A\nBody & more", "caret": false}))
            .unwrap();
        s.execute("text.select", &json!({"story": a["story"], "anchor": 0, "focus": 0})).unwrap();
        s.execute("style.paragraph.apply", &json!({"name": "Head"})).unwrap();
        let b = s.execute("frame.create", &json!({"rect": [72, 300, 400, 350], "content": "text", "text": "Caption", "caret": false})).unwrap();
        s.execute("xml.tag", &json!({"tag": "story", "ids": [a["id"]]})).unwrap();
        s.execute("xml.tag", &json!({"tag": "caption", "ids": [b["id"]]})).unwrap();
        s.execute("xml.mapStyle", &json!({"style": "Head", "tag": "title"})).unwrap();
        let x = s.execute("file.exportXml", &json!({})).unwrap()["text"].as_str().unwrap().to_string();
        assert!(
            x.contains("<story>")
                && x.contains("<title>Title A</title>")
                && x.contains("Body &amp; more")
                && x.contains("<caption>Caption</caption>"),
            "{x}"
        );
        assert!(x.find("<story>").unwrap() < x.find("<caption>").unwrap(), "reading order");
        let st = s.execute("xml.structure", &json!({})).unwrap();
        assert_eq!(st["elements"].as_array().unwrap().len(), 2);
        // Import new content into the same structure.
        let r = s
            .execute(
                "file.importXml",
                &json!({"text": "<Root><story><title>New title</title><p>New body</p></story><caption>New caption</caption><extra/></Root>"}),
            )
            .unwrap();
        assert_eq!((r["placed"].as_u64(), r["unmatched"].as_u64()), (Some(2), Some(1)));
        let d = &s.doc().unwrap().doc;
        let sa = d.stories[&designcraft_doc::StoryId(a["story"].as_u64().unwrap())].clone();
        assert_eq!(sa.text, "New title\nNew body");
        assert_eq!(sa.paras[0].style, "Head", "mapped tag โ†’ paragraph style");
        assert_eq!(d.stories[&designcraft_doc::StoryId(b["story"].as_u64().unwrap())].text, "New caption");
        assert!(s.execute("xml.tag", &json!({"tag": "1bad", "ids": [b["id"]]})).is_err());
        // Inline tagging: part of a paragraph becomes an element.
        let f = s
            .execute("frame.create", &json!({"rect": [72, 500, 400, 560], "content": "text", "text": "See the glossary entry", "caret": false}))
            .unwrap();
        s.execute("xml.tag", &json!({"tag": "note", "ids": [f["id"]]})).unwrap();
        s.execute("text.select", &json!({"story": f["story"], "anchor": 8, "focus": 16})).unwrap();
        s.execute("xml.tagText", &json!({"tag": "term"})).unwrap();
        let x = s.execute("file.exportXml", &json!({})).unwrap()["text"].as_str().unwrap().to_string();
        assert!(x.contains("<note>See the <term>glossary</term> entry</note>"), "{x}");
        // Tag markers draw in the tag's colour (screen view only).
        let d = s.doc().unwrap().doc.clone();
        let col = d.xml.tags.iter().find(|t| t.name == "term").unwrap().color;
        let mut rr = designcraft_render::Renderer::new();
        rr.threads = 0;
        let count = |opts: &designcraft_render::RenderOptions, rr: &mut designcraft_render::Renderer| {
            let img = rr.render_page(&d, &s.cache, 0, 2.0, false, opts).unwrap();
            img.pixels
                .as_chunks::<4>()
                .0
                .iter()
                .filter(|p| {
                    (p[0] as i32 - col[0] as i32).abs() < 30 && (p[1] as i32 - col[1] as i32).abs() < 30 && (p[2] as i32 - col[2] as i32).abs() < 30
                })
                .count()
        };
        let plain = count(&Default::default(), &mut rr);
        let marked = count(&designcraft_render::RenderOptions { tag_markers: true, ..Default::default() }, &mut rr);
        assert!(marked > plain + 10, "{plain} {marked}");
    }
}