From ffbbfcabfabdeaae91553157cd71a60d4027ca82 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:03:02 +0100 Subject: [PATCH] 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 --- crates/docx/src/read/parts.rs | 4 +++- crates/docx/src/xml.rs | 9 ++++++-- crates/docx/tests/it/fixtures.rs | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/crates/docx/src/read/parts.rs b/crates/docx/src/read/parts.rs index a62a482752..5c0d8ed9bc 100644 --- a/crates/docx/src/read/parts.rs +++ b/crates/docx/src/read/parts.rs @@ -74,7 +74,9 @@ impl Reader<'_> { pub(super) fn revision_named(&mut self, kind: RevisionKind, e: &El, move_name: Option) -> u32 { let author = e.attr("w:author").unwrap_or("").chars().take(256).collect::(); let date = e.attr("w:date").unwrap_or("").chars().take(64).collect::(); - let date_utc = e.attr("w16du:dateUtc").filter(|d| !d.is_empty()).map(|d| d.chars().take(64).collect::()); + // 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::()); let k = match kind { RevisionKind::Insert => 0, RevisionKind::Delete => 1, diff --git a/crates/docx/src/xml.rs b/crates/docx/src/xml.rs index 378bcbe2a8..a6d8794fb6 100644 --- a/crates/docx/src/xml.rs +++ b/crates/docx/src/xml.rs @@ -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 { self.kids.iter().filter_map(|n| match n { diff --git a/crates/docx/tests/it/fixtures.rs b/crates/docx/tests/it/fixtures.rs index 4f2b9de277..e58fb3e0a7 100644 --- a/crates/docx/tests/it/fixtures.rs +++ b/crates/docx/tests/it/fixtures.rs @@ -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#"{text}"#) + }; + format!("{}{}", 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]