craft-drawingml: keep the default namespace on kept fragments

A part written with a default namespace (`<theme xmlns="…/main">`, a slide with
`xmlns="…/presentationml/2006/main"`) has unprefixed names. `keep_with` copied
only prefixed declarations, so a kept fragment (theme custClrLst, extLst,
objectDefaults, effect 3-D, slide/layout/master extLst) lost its namespace when
written under a root that binds no default. A kept fragment now gets the part
root's `xmlns` when one of its unprefixed names is not bound inside it; prefixed
fragments are written as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76andClaude Opus 5.5 committed 2026-11-15 22:06:07 +01:00
1 parent d5b4dbef88
commit e7627d396c
2 files changed
+143 -3

No files matched your search

+47 -3
View File
@@ -156,7 +156,20 @@ fn used_prefixes(el: &El) -> HashSet<&str> {
used
}
/// Serialize `el`, declaring every prefix it uses that the source root declared.
/// Does `el` or a descendant have an unprefixed name that no `xmlns` inside `el` binds? Such a
/// name is in the default namespace of the part it was read from. Attributes never are.
fn uses_outer_default(el: &El) -> bool {
fn rec(e: &El, depth: usize) -> bool {
if depth > MAX_DEPTH || e.attrs.iter().any(|(k, _)| k == "xmlns") {
return false;
}
!e.name.contains(':') || e.elements().any(|c| rec(c, depth + 1))
}
rec(el, 0)
}
/// Serialize `el`, declaring every prefix it uses that the source root declared, and the
/// root's default namespace when an unprefixed name in `el` depends on it.
pub fn keep_xml(el: &El, ns: &[(String, String)]) -> String {
keep_with(el, &[ns], None)
}
@@ -166,11 +179,15 @@ pub fn keep_xml(el: &El, ns: &[(String, String)]) -> String {
/// [`MAX_COPIED_NS_BYTES`]).
pub fn keep_with(el: &El, decls: &[&[(String, String)]], budget: Option<&Cell<usize>>) -> String {
let used = used_prefixes(el);
let default = uses_outer_default(el);
let mut have: HashSet<&str> = el.attrs.iter().map(|(a, _)| a.as_str()).collect();
let mut extra = Vec::new();
for (k, v) in decls.iter().flat_map(|d| d.iter()) {
let p = k.strip_prefix("xmlns:").unwrap_or("");
if !p.is_empty() && used.contains(p) && v.len() <= MAX_NS_VALUE && !have.contains(k.as_str()) {
let wanted = match k.strip_prefix("xmlns:") {
Some(p) => !p.is_empty() && used.contains(p),
None => k == "xmlns" && default,
};
if wanted && v.len() <= MAX_NS_VALUE && !have.contains(k.as_str()) {
let cost = k.len() + v.len();
if let Some(b) = budget {
if b.get() < cost {
@@ -324,6 +341,33 @@ mod tests {
assert!(!has_rel_refs(root.child(Local("z")).unwrap()));
}
/// A part written with a default namespace (`<theme xmlns="…/main">`): an unprefixed
/// fragment, or one with an unprefixed descendant, carries that namespace; a prefixed one
/// and one that declares its own default are written as before.
#[test]
fn a_kept_fragment_carries_the_default_namespace_its_names_depend_on() {
let root = parse(
br#"<theme xmlns="D" xmlns:r="R"><custClrLst><custClr name="x"><srgbClr val="FF0000"/></custClr></custClrLst><r:y/><r:z><w/></r:z><own xmlns="O"><v/></own><r:u><t xmlns=""/></r:u></theme>"#,
)
.unwrap();
let ns = ns_decls(&root);
let kid = |n: &str| root.child(Local(n)).unwrap();
assert_eq!(keep_xml(kid("custClrLst"), &ns), r#"<custClrLst xmlns="D"><custClr name="x"><srgbClr val="FF0000"/></custClr></custClrLst>"#);
assert_eq!(keep_xml(kid("y"), &ns), r#"<r:y xmlns:r="R"/>"#, "prefixed: as before");
assert_eq!(keep_xml(kid("z"), &ns), r#"<r:z xmlns="D" xmlns:r="R"><w/></r:z>"#, "an unprefixed descendant");
assert_eq!(keep_xml(kid("own"), &ns), r#"<own xmlns="O"><v/></own>"#, "its own default wins");
assert_eq!(keep_xml(kid("u"), &ns), r#"<r:u xmlns:r="R"><t xmlns=""/></r:u>"#, "an undeclared default stays");
// A part with no default namespace: unprefixed names stay in no namespace.
let plain = parse(br#"<p:sld xmlns:p="P"><x/></p:sld>"#).unwrap();
assert_eq!(keep_xml(plain.child(Local("x")).unwrap(), &ns_decls(&plain)), "<x/>");
// The default declaration spends the budget like the others.
let budget = Cell::new(3);
assert_eq!(
keep_with(kid("custClrLst"), &[&ns], Some(&budget)),
r#"<custClrLst><custClr name="x"><srgbClr val="FF0000"/></custClr></custClrLst>"#
);
}
#[test]
fn inner_declarations_are_found_and_clashing_ones_dropped() {
let root = parse(br#"<r xmlns:a="A"><x xmlns:b="B"><y xmlns:c="C1"/></x><z xmlns:c="C2" xmlns:a="A"/></r>"#).unwrap();
+96
View File
@@ -1,5 +1,7 @@
//! The writer, read back with the reader.
mod common;
use std::sync::Arc;
use craft_pptx::table::{Cell, Table};
@@ -193,6 +195,100 @@ fn reads_the_theme_of_a_pptx_and_of_a_thmx() {
}
}
const NS_A: &str = "http://schemas.openxmlformats.org/drawingml/2006/main";
const NS_P: &str = "http://schemas.openxmlformats.org/presentationml/2006/main";
const NS_THM15: &str = "http://schemas.microsoft.com/office/thememl/2012/main";
/// The namespace of every element named `local` in `xml`, resolved through the declarations in
/// scope (`""` for none).
fn namespaces_of(xml: &str, local: &str) -> Vec<String> {
fn rec(e: &craft_xml::El, scope: &mut Vec<(String, String)>, local: &str, out: &mut Vec<String>) {
let n = scope.len();
scope.extend(e.ns_decls().map(|(p, u)| (p.to_string(), u.to_string())));
if e.local() == local {
out.push(scope.iter().rev().find(|(p, _)| p == e.prefix()).map(|(_, u)| u.clone()).unwrap_or_default());
}
for c in e.elements() {
rec(c, scope, local, out);
}
scope.truncate(n);
}
let root = craft_xml::Options::default().strict().parse(xml.as_bytes()).unwrap_or_else(|e| panic!("not well-formed: {e}\n{xml}"));
let mut out = vec![];
rec(&root, &mut vec![], local, &mut out);
out
}
/// A theme with a custom colour list and a theme family id, with the `a:` prefix.
const PREFIXED_THEME: &str = r#"<a:theme xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" name="Harbour"><a:themeElements><a:clrScheme name="Harbour"><a:dk1><a:srgbClr val="000000"/></a:dk1><a:lt1><a:srgbClr val="FFFFFF"/></a:lt1><a:dk2><a:srgbClr val="1F1F1F"/></a:dk2><a:lt2><a:srgbClr val="EEEEEE"/></a:lt2><a:accent1><a:srgbClr val="123456"/></a:accent1><a:accent2><a:srgbClr val="222222"/></a:accent2><a:accent3><a:srgbClr val="333333"/></a:accent3><a:accent4><a:srgbClr val="444444"/></a:accent4><a:accent5><a:srgbClr val="555555"/></a:accent5><a:accent6><a:srgbClr val="666666"/></a:accent6><a:hlink><a:srgbClr val="0000FF"/></a:hlink><a:folHlink><a:srgbClr val="800080"/></a:folHlink></a:clrScheme><a:fontScheme name="Harbour"><a:majorFont><a:latin typeface="Lato"/><a:ea typeface=""/><a:cs typeface=""/></a:majorFont><a:minorFont><a:latin typeface="Calibri"/><a:ea typeface=""/><a:cs typeface=""/></a:minorFont></a:fontScheme></a:themeElements><a:objectDefaults/><a:extraClrSchemeLst/><a:custClrLst><a:custClr name="Brand"><a:srgbClr val="C00000"/></a:custClr></a:custClrLst><a:extLst><a:ext uri="{05A4C25C-085E-4340-85A3-A5531E510DB2}"><thm15:themeFamily xmlns:thm15="http://schemas.microsoft.com/office/thememl/2012/main" name="Harbour" id="{62F939B6-93AF-4DB8-9C6B-D6C7DFDC589F}" vid="{4A3C46E8-61CC-4603-A589-7422A47A8E4A}"/></a:ext></a:extLst></a:theme>"#;
/// The deck with its theme part replaced by `theme`.
fn with_theme(theme: &str) -> Vec<u8> {
let pptx = craft_pptx::export(&deck(), &Plain).unwrap();
common::rewrite(&pptx, |name, data| Some(if name == "ppt/theme/theme1.xml" { theme.as_bytes().to_vec() } else { data.to_vec() }), &[])
}
/// A theme written with a default namespace instead of the `a:` prefix: the parts kept as
/// written (custom colours, theme family id) stay in the DrawingML namespace on save.
#[test]
fn a_default_namespace_theme_keeps_its_kept_parts_in_the_drawingml_namespace() {
let theme = PREFIXED_THEME.replace("<a:", "<").replace("</a:", "</").replace("xmlns:a=", "xmlns=");
assert!(!theme.contains("a:"), "{theme}");
let p = craft_pptx::import(&with_theme(&theme), &Plain).unwrap();
assert_eq!(p.masters[0].theme.colors.get(SchemeSlot::Accent1), Rgba::rgb(0x12, 0x34, 0x56));
let once = craft_pptx::export(&p, &Plain).unwrap();
let xml = part(&once, "ppt/theme/theme1.xml");
// The writer may add an `a:extLst` of its own to the colour scheme, so every one is checked.
for local in ["objectDefaults", "custClrLst", "custClr", "extLst", "ext", "srgbClr"] {
let ns = namespaces_of(&xml, local);
assert!(!ns.is_empty() && ns.iter().all(|n| n == NS_A), "{local}: {ns:?}\n{xml}");
}
assert_eq!(namespaces_of(&xml, "themeFamily"), [NS_THM15], "{xml}");
// The second save writes the same kept parts.
let kept = |x: &str| x[x.find("</a:themeElements>").expect("themeElements")..].to_string();
let twice = craft_pptx::export(&craft_pptx::import(&once, &Plain).unwrap(), &Plain).unwrap();
assert_eq!(kept(&part(&twice, "ppt/theme/theme1.xml")), kept(&xml));
}
/// The same theme with the `a:` prefix: the kept parts are written as before (each declaring
/// the `a:` it uses), with no default namespace added.
#[test]
fn a_prefixed_theme_keeps_its_kept_parts_unchanged() {
let p = craft_pptx::import(&with_theme(PREFIXED_THEME), &Plain).unwrap();
let xml = part(&craft_pptx::export(&p, &Plain).unwrap(), "ppt/theme/theme1.xml");
assert!(!xml.contains(r#" xmlns=""#), "{xml}");
let a = format!(r#"xmlns:a="{NS_A}""#);
let kept = format!(
r#"<a:objectDefaults {a}/><a:extraClrSchemeLst {a}/><a:custClrLst {a}><a:custClr name="Brand"><a:srgbClr val="C00000"/></a:custClr></a:custClrLst><a:extLst {a}><a:ext uri="{{05A4C25C-085E-4340-85A3-A5531E510DB2}}"><thm15:themeFamily xmlns:thm15="{NS_THM15}" name="Harbour" id="{{62F939B6-93AF-4DB8-9C6B-D6C7DFDC589F}}" vid="{{4A3C46E8-61CC-4603-A589-7422A47A8E4A}}"/></a:ext></a:extLst></a:theme>"#
);
assert!(xml.contains(&kept), "{xml}");
}
/// A slide written with a default PresentationML namespace: its kept extension list stays in
/// that namespace under the writer's `p:extLst`.
#[test]
fn a_default_namespace_slide_keeps_its_extensions_in_the_presentationml_namespace() {
let pptx = craft_pptx::export(&deck(), &Plain).unwrap();
let ext = r#"<extLst><ext uri="{11111111-2222-3333-4444-555555555555}"><t:data xmlns:t="urn:test" v="1"/></ext></extLst>"#;
let doc = common::rewrite(
&pptx,
|name, data| {
if name != "ppt/slides/slide1.xml" {
return Some(data.to_vec());
}
let xml = String::from_utf8(data.to_vec()).unwrap();
assert!(xml.contains("<p:sld ") && xml.contains("</p:sld>") && !xml.contains("extLst"), "{xml}");
Some(xml.replacen("<p:sld ", &format!(r#"<p:sld xmlns="{NS_P}" "#), 1).replace("</p:sld>", &format!("{ext}</p:sld>")).into_bytes())
},
&[],
);
let p = craft_pptx::import(&doc, &Plain).unwrap();
let xml = part(&craft_pptx::export(&p, &Plain).unwrap(), "ppt/slides/slide1.xml");
assert!(xml.contains("{11111111-2222-3333-4444-555555555555}"), "{xml}");
assert_eq!(namespaces_of(&xml, "ext"), [NS_P], "{xml}");
assert_eq!(namespaces_of(&xml, "data"), ["urn:test"], "{xml}");
}
#[test]
fn templates_and_shows_differ_in_the_main_content_type() {
let p = deck();