mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
docx: only a dateUtc in Word's namespace is a tracked change's UTC time
The reader looked `w16du:dateUtc` up with `El::attr`, which falls back to an unqualified attribute with the same local name. A bare `dateUtc` then told revisions apart and was saved under Word's namespace. It now reads the canonical name only (`El::attr_exact`); other attributes keep the fallback. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
be8e501ffc
commit
ffbbfcabfa
3 files changed
+46
-3
No files matched your search
@@ -74,7 +74,9 @@ impl Reader<'_> {
|
||||
pub(super) fn revision_named(&mut self, kind: RevisionKind, e: &El, move_name: Option<String>) -> u32 {
|
||||
let author = e.attr("w:author").unwrap_or("").chars().take(256).collect::<String>();
|
||||
let date = e.attr("w:date").unwrap_or("").chars().take(64).collect::<String>();
|
||||
let date_utc = e.attr("w16du:dateUtc").filter(|d| !d.is_empty()).map(|d| d.chars().take(64).collect::<String>());
|
||||
// Only the Word namespace's `dateUtc`: a bare one would be saved under that namespace
|
||||
// without having come from it.
|
||||
let date_utc = e.attr_exact("w16du:dateUtc").filter(|d| !d.is_empty()).map(|d| d.chars().take(64).collect::<String>());
|
||||
let k = match kind {
|
||||
RevisionKind::Insert => 0,
|
||||
RevisionKind::Delete => 1,
|
||||
|
||||
@@ -203,15 +203,20 @@ impl El {
|
||||
/// Attribute by canonical name (`w:val`); falls back to an unqualified attribute with the
|
||||
/// same local name (some producers omit the prefix).
|
||||
pub fn attr(&self, name: &str) -> Option<&str> {
|
||||
if let Some((_, v)) = self.attrs.iter().find(|(k, _)| k == name) {
|
||||
if let Some(v) = self.attr_exact(name) {
|
||||
return Some(v);
|
||||
}
|
||||
let local = name.rsplit(':').next().unwrap_or(name);
|
||||
if local != name {
|
||||
return self.attrs.iter().find(|(k, _)| k == local).map(|(_, v)| v.as_str());
|
||||
return self.attr_exact(local);
|
||||
}
|
||||
None
|
||||
}
|
||||
/// Attribute by canonical name only, for one whose namespace carries meaning an unqualified
|
||||
/// attribute can't claim.
|
||||
pub fn attr_exact(&self, name: &str) -> Option<&str> {
|
||||
self.attrs.iter().find(|(k, _)| k == name).map(|(_, v)| v.as_str())
|
||||
}
|
||||
/// Child elements.
|
||||
pub fn els(&self) -> impl Iterator<Item = &El> {
|
||||
self.kids.iter().filter_map(|n| match n {
|
||||
|
||||
@@ -2706,6 +2706,42 @@ fn move_revisions_round_trip() {
|
||||
check(&craft_docx::read(&out).unwrap());
|
||||
}
|
||||
|
||||
/// Two insertions by one author with one local `w:date`, told apart (or not) by `dateUtc`.
|
||||
fn two_insertions_at_one_local_time(utc: [&str; 2]) -> String {
|
||||
let ins = |id: u32, utc: &str, text: &str| {
|
||||
format!(r#"<w:ins w:id="{id}" w:author="Ada" w:date="2026-11-01T01:30:00Z" {utc}><w:r><w:t xml:space="preserve">{text}</w:t></w:r></w:ins>"#)
|
||||
};
|
||||
format!("<w:p>{}{}</w:p>", ins(1, utc[0], " One"), ins(2, utc[1], " Two"))
|
||||
}
|
||||
|
||||
/// Issue #3206: a tracked change's `dateUtc` is read under any prefix bound to Word's namespace
|
||||
/// for it, and saved under that namespace.
|
||||
#[test]
|
||||
fn a_tracked_change_utc_time_is_read_under_any_prefix_for_its_namespace() {
|
||||
let du = r#"xmlns:du="http://schemas.microsoft.com/office/word/2023/wordml/word16du""#;
|
||||
let body = two_insertions_at_one_local_time([
|
||||
&format!(r#"{du} du:dateUtc="2026-11-01T05:30:00Z""#),
|
||||
&format!(r#"{du} du:dateUtc="2026-11-01T06:30:00Z""#),
|
||||
]);
|
||||
let d = read_body(&body);
|
||||
let utc: Vec<_> = d.revisions.iter().map(|r| r.date_utc.as_deref()).collect();
|
||||
assert_eq!(utc, [Some("2026-11-01T05:30:00Z"), Some("2026-11-01T06:30:00Z")]);
|
||||
let xml = text(&unzip(&craft_docx::write(&d).unwrap()), "word/document.xml");
|
||||
assert_eq!(attr_values(&xml, "w:ins", "w16du:dateUtc"), ["2026-11-01T05:30:00Z", "2026-11-01T06:30:00Z"]);
|
||||
}
|
||||
|
||||
/// Issue #3206: an unqualified `dateUtc` doesn't say it's Word's UTC time, so it neither tells
|
||||
/// changes apart nor is saved under Word's namespace.
|
||||
#[test]
|
||||
fn an_unqualified_date_utc_is_not_a_tracked_change_utc_time() {
|
||||
let body = two_insertions_at_one_local_time([r#"dateUtc="2026-11-01T05:30:00Z""#, r#"dateUtc="2026-11-01T06:30:00Z""#]);
|
||||
let d = read_body(&body);
|
||||
assert_eq!(d.revisions.len(), 1, "one author, kind and local date is one change");
|
||||
assert_eq!(d.revisions[0].date_utc, None);
|
||||
let xml = text(&unzip(&craft_docx::write(&d).unwrap()), "word/document.xml");
|
||||
assert!(!xml.contains("dateUtc="), "{xml}");
|
||||
}
|
||||
|
||||
/// A document's own footnote separators and continuation notice are kept (ECMA-376 §17.11), and
|
||||
/// written back as Word numbers them: -1, 0 and 1, the notes after them.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user