diff --git a/crates/odf/src/text/kept.rs b/crates/odf/src/text/kept.rs index 953b672cc3..3e507e5b67 100644 --- a/crates/odf/src/text/kept.rs +++ b/crates/odf/src/text/kept.rs @@ -332,17 +332,17 @@ pub(super) fn with_common_as_read( }) } -/// What becomes of the file's styles (`defs`) that `changed` touches but `styles` (the part being -/// written) doesn't have under the file's key, by those keys: WordCraft's definition from `now` -/// (all it would write) under the file's names, or `None` for a style it no longer has. `ids` -/// gives the style id the import read a file's style into ([`KeptXml::style_ids`]), `ours` the -/// name WordCraft writes an id under: paragraphs written as read name the file's. A style the -/// import didn't read is WordCraft's only under its own name, so another of the file's styles -/// WordCraft would have under the same id (`Base_20_Style` beside `BaseStyle`) is left alone. +/// What becomes of the file's styles (`defs`) that `changed` touches, by their keys: WordCraft's +/// definition from `now` (all it would write) under the file's names, its parent and next style +/// included, or `None` for a style it no longer has. `ids` gives the style id the import read a +/// file's style into ([`KeptXml::style_ids`]), `ours` the name WordCraft writes an id under: +/// paragraphs written as read name the file's. A style the import didn't read is WordCraft's only +/// under its own name, so another of the file's styles WordCraft would have under the same id +/// (`Base_20_Style` beside `BaseStyle`) is left alone, as is one the import didn't read named as +/// WordCraft names one it did (`MyCustom` beside `My_20_Custom`). /// /// [`KeptXml::style_ids`]: craft_wordmodel::kept::KeptXml::style_ids pub(super) fn rewritten_defs( - styles: &str, now: &str, defs: &BTreeMap>, changed: &BTreeSet, @@ -377,16 +377,25 @@ pub(super) fn rewritten_defs( }; styles_of.push((key, name, family, mine)); } + // The names WordCraft writes the styles the import read under, by family: one of the file's + // styles the import didn't read but named so (`MyCustom` beside `My_20_Custom`) isn't theirs. + let read: HashSet<(&str, &str)> = + styles_of.iter().filter(|(key, ..)| ids.contains_key(*key)).map(|(_, _, family, mine)| (*family, mine.as_str())).collect(); let (kids, _) = children(now, "office:styles", "common"); let kids: HashMap = kids.into_iter().collect(); - let (written, _) = children(styles, "office:styles", "common"); - let written: HashSet = written.into_iter().map(|(k, _)| k).collect(); let mut out = BTreeMap::new(); - for (key, name, family, mine) in styles_of { + for (key, name, family, mine) in &styles_of { + let (key, name, family) = (*key, name, *family); + let read_so = !ids.contains_key(key) && read.contains(&(family, mine.as_str())); let mine = format!("common:{mine}{family}"); - if !changed.contains(&mine) || (mine == *key && written.contains(key)) { + if !changed.contains(&mine) { continue; } + if read_so { + out.insert(key.clone(), defs.get(key).map(|d| d.to_string())); + continue; + } + // Rewritten even under its own name: its parent or next style may have another. let def = kids.get(&mine).and_then(|(a, b)| now.get(*a..*b)).map(|d| { renamed(d, &|attr, v| match attr { // Two of the file's names can be one of WordCraft's: a style keeps its own. diff --git a/crates/odf/src/text/package.rs b/crates/odf/src/text/package.rs index 3b91a02b52..6e1b73a3a8 100644 --- a/crates/odf/src/text/package.rs +++ b/crates/odf/src/text/package.rs @@ -847,7 +847,7 @@ fn styles_as_read(doc: &Document, kept: Option<&craft_wordmodel::kept::KeptXml>, let changed = super::kept::changed(&k.style_prints, &super::kept::prints(&now)); // The name WordCraft writes a style id under (`My_20_Custom` was read as `MyCustom`). let ours = |id: &str| super::writer::odf_name(id); - let rewritten = super::kept::rewritten_defs(&styles, &now, &k.defs, &changed, &k.style_ids, &ours); + let rewritten = super::kept::rewritten_defs(&now, &k.defs, &changed, &k.style_ids, &ours); super::kept::with_common_as_read(&styles, &k.defs, &k.order, &changed, &rewritten) } diff --git a/crates/odf/src/text/tests_more/kept.rs b/crates/odf/src/text/tests_more/kept.rs index d47ea57389..a87e38655e 100644 --- a/crates/odf/src/text/tests_more/kept.rs +++ b/crates/odf/src/text/tests_more/kept.rs @@ -418,6 +418,51 @@ fn an_unused_style_sharing_an_edited_ones_id_keeps_its_definition() { assert!(theirs.contains(r#"fo:font-size="30pt""#) && theirs.contains("#ff0000"), "as read: {theirs}"); } +/// A style nothing in the file uses, named as WordCraft names a used one (`MyCustom` beside +/// `My_20_Custom`, which WordCraft has as `MyCustom`), keeps its definition when the used one is +/// edited or deleted. +#[test] +fn an_unused_style_named_as_wordcraft_names_an_edited_one_keeps_its_definition() { + use std::sync::Arc; + let s = r##""##; + let as_read = |saved: &str| { + let theirs = def_named(saved, "MyCustom").unwrap_or_else(|| panic!("{saved}")); + assert!(theirs.contains(r#"fo:font-size="30pt""#) && theirs.contains("#ff0000"), "as read: {theirs}"); + }; + + let mut d = opened(s, &["My_20_Custom"]); + let id = d.body[0].as_para().unwrap().props.style.clone().unwrap(); + Arc::make_mut(&mut d.styles).get_mut(&id).unwrap().chr.size = Some(21.0); + let saved = styles_of(&export(&d).unwrap()); + assert!(def_named(&saved, "My_20_Custom").is_some_and(|d| d.contains(r#"fo:font-size="21pt""#)), "{saved}"); + as_read(&saved); + + let mut d = opened(s, &["My_20_Custom"]); + Arc::make_mut(&mut d.styles).remove(&id); + d.restyle(&id, None); + let saved = styles_of(&export(&d).unwrap()); + assert!(def_named(&saved, "My_20_Custom").is_none(), "{saved}"); + as_read(&saved); +} + +/// An edited style the file names as WordCraft does (`Eigen`), whose parent WordCraft names +/// otherwise (`Base_20_Style`, WordCraft's `BaseStyle`), still derives from the file's parent. +#[test] +fn an_edited_style_under_its_own_name_keeps_its_parents_file_name() { + use std::sync::Arc; + let s = r#""#; + let mut d = opened(s, &["Eigen"]); + let id = d.body[0].as_para().unwrap().props.style.clone().unwrap(); + Arc::make_mut(&mut d.styles).get_mut(&id).unwrap().chr.italic = Some(true); + let bytes = export(&d).unwrap(); + let saved = styles_of(&bytes); + let mine = def_named(&saved, "Eigen").unwrap_or_else(|| panic!("{saved}")); + assert!(mine.contains(r#"style:parent-style-name="Base_20_Style""#), "{mine}"); + assert!(def_named(&saved, "Base_20_Style").is_some_and(|d| d.contains(r#"style:writing-mode="tb-rl""#)), "as read: {saved}"); + let back = import(&bytes).unwrap(); + assert_eq!(back.styles.resolve_char(back.body[0].as_para().unwrap().props.style.as_deref(), &Default::default()).size, 30.0, "inherited"); +} + /// A character style WordCraft read under another id (`AccentChar`, as a paragraph style has /// `Accent`), edited, is written under the file's name: the text written as read names that. #[test]