diff --git a/crates/odf/src/text/kept.rs b/crates/odf/src/text/kept.rs index 6372ce55a3..f3c68c47ec 100644 --- a/crates/odf/src/text/kept.rs +++ b/crates/odf/src/text/kept.rs @@ -333,7 +333,8 @@ pub(super) fn with_common_as_read( _ => Some(d.to_string()), }; // A style of WordCraft's own derives from (or is followed by) the file's style a style was - // read from, not an unread namesake of it ([`read_names`]). + // read from, under the file's name ([`file_names`]): not WordCraft's copy of it, nor an + // unread namesake. let fresh = |k: &str, d: &str| { let family = if k.ends_with("|text") { "|text" } else { "" }; renamed(d, &|attr, v| (attr != "style:name").then(|| read.get(&format!("{v}{family}")).cloned()).flatten()) @@ -417,6 +418,22 @@ pub(super) fn rewritten_defs( out } +/// The file's names for the styles the import read under another name than WordCraft writes them +/// under (`ours`): by that name with its family's key suffix (`BaseStyle`, `BaseStyle|text`), +/// unescaped, the first the import read into it where several were. +pub(super) fn file_names(ids: &BTreeMap, ours: &dyn Fn(&str) -> String) -> std::collections::HashMap { + let mut out = std::collections::HashMap::new(); + for (key, id) in ids { + let Some(rest) = key.strip_prefix("common:") else { continue }; + let (name, family) = rest.strip_suffix("|text").map_or((rest, ""), |n| (n, "|text")); + let mine = format!("{}{family}", ours(id)); + if mine != rest { + out.entry(mine).or_insert_with(|| name.to_string()); + } + } + out +} + /// The file's names for the styles the import read whose name as WordCraft writes them (`ours`) /// is a style of the file's the import didn't read (`MyCustom` beside `My_20_Custom`): by that /// name with its family's key suffix (`MyCustom`, `MyCustom|text`), unescaped. That namesake keeps diff --git a/crates/odf/src/text/package.rs b/crates/odf/src/text/package.rs index 87ee24c553..cc4023fa4c 100644 --- a/crates/odf/src/text/package.rs +++ b/crates/odf/src/text/package.rs @@ -848,7 +848,7 @@ fn styles_as_read(doc: &Document, kept: Option<&craft_wordmodel::kept::KeptXml>, // 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(&now, &k.defs, &changed, &k.style_ids, &ours); - let read = super::kept::read_names(&k.defs, &k.style_ids, &ours); + let read = super::kept::file_names(&k.style_ids, &ours); super::kept::with_common_as_read(&styles, &k.defs, &k.order, &changed, &rewritten, &read) } diff --git a/crates/odf/src/text/tests_more/kept.rs b/crates/odf/src/text/tests_more/kept.rs index c3b778896a..fd75609542 100644 --- a/crates/odf/src/text/tests_more/kept.rs +++ b/crates/odf/src/text/tests_more/kept.rs @@ -493,6 +493,30 @@ fn a_new_style_based_on_one_beside_an_unused_namesake_derives_from_it() { assert_eq!(got, 9.0, "{saved}\n{}", content_of(&bytes)); } +/// A style made after reading (`NewChild`), based on a file's style WordCraft names otherwise +/// (`Base_20_Style`, WordCraft's `BaseStyle`), derives from the file's style, with what WordCraft +/// doesn't model of it. +#[test] +fn a_new_style_based_on_a_renamed_one_derives_from_the_files() { + use std::sync::Arc; + let s = r#""#; + let mut d = opened(s, &["Base_20_Style"]); + let id = d.body[0].as_para().unwrap().props.style.clone().unwrap(); + let mut child = d.styles.get(&id).unwrap().clone(); + (child.id, child.name, child.based_on, child.next) = ("NewChild".into(), "NewChild".into(), Some(id.clone()), None); + (child.para, child.chr) = Default::default(); + Arc::make_mut(&mut d.styles).upsert(child); + d.restyle(&id, Some("NewChild".to_string())); + let bytes = export(&d).unwrap(); + let saved = styles_of(&bytes); + let new = saved.split("').next()); + assert!(new.is_some_and(|n| n.contains(r#"style:parent-style-name="Base_20_Style""#)), "{saved}"); + 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(); + let got = back.styles.resolve_char(back.body[0].as_para().unwrap().props.style.as_deref(), &Default::default()).size; + assert_eq!(got, 30.0, "{saved}"); +} + /// The same for a character style: edited text's automatic style derives from the file's /// character style it was read with (`My_20_Accent`), not an unused namesake (`MyAccent`). #[test]