mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 18:56:30 +01:00
ODT: an edited style keeps its parent's and an unread namesake's file definitions
An edited style the file names as WordCraft does (`Eigen`) was skipped by the rewrite, so its parent kept WordCraft's name (`BaseStyle`) instead of the file's (`Base_20_Style`). It is now rewritten like any other. A style the import didn't read whose name is the one WordCraft writes a read style under (`MyCustom` beside `My_20_Custom`) took that style's edit or deletion. It now keeps its own definition. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
3 files changed
+67
-13
No files matched your search
+21
-12
@@ -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<String, Arc<str>>,
|
||||
changed: &BTreeSet<String>,
|
||||
@@ -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<String, (usize, usize)> = kids.into_iter().collect();
|
||||
let (written, _) = children(styles, "office:styles", "common");
|
||||
let written: HashSet<String> = 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.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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##"<style:style style:name="Standard" style:family="paragraph" style:class="text"/><style:style style:name="My_20_Custom" style:family="paragraph" style:parent-style-name="Standard"><style:text-properties fo:font-size="9pt"/></style:style><style:style style:name="MyCustom" style:family="paragraph" style:parent-style-name="Standard"><style:text-properties fo:font-size="30pt" fo:color="#ff0000"/></style:style>"##;
|
||||
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#"<style:style style:name="Standard" style:family="paragraph" style:class="text"/><style:style style:name="Base_20_Style" style:display-name="Base Style" style:family="paragraph" style:parent-style-name="Standard" style:class="text"><style:paragraph-properties style:writing-mode="tb-rl"/><style:text-properties fo:font-size="30pt"/></style:style><style:style style:name="Eigen" style:family="paragraph" style:parent-style-name="Base_20_Style"/>"#;
|
||||
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]
|
||||
|
||||
Reference in new issue
Block a user