From 807d3da7805d112d8dcce1cbc8a74a7912c166b4 Mon Sep 17 00:00:00 2001 From: Xemptuous <99044751+Xemptuous@users.noreply.github.com> Date: Wed, 1 Apr 2026 15:26:46 -0700 Subject: [PATCH] ooxml ordering errors and fixes --- .../documents/elements/numbering_property.rs | 2 +- .../documents/elements/paragraph_property.rs | 1 - .../src/documents/elements/run_property.rs | 26 +++++++++---------- .../documents/elements/section_property.rs | 10 +++---- docx-core/src/documents/elements/style.rs | 14 +++++----- .../src/documents/elements/table_grid.rs | 2 +- .../src/documents/elements/table_property.rs | 8 +++--- docx-core/src/documents/settings.rs | 2 +- docx-core/src/xml_builder/elements.rs | 2 +- docx-core/src/xml_builder/macros.rs | 9 +++++++ 10 files changed, 42 insertions(+), 34 deletions(-) diff --git a/docx-core/src/documents/elements/numbering_property.rs b/docx-core/src/documents/elements/numbering_property.rs index b89f88bdd..52be2f7b4 100644 --- a/docx-core/src/documents/elements/numbering_property.rs +++ b/docx-core/src/documents/elements/numbering_property.rs @@ -41,8 +41,8 @@ impl BuildXML for NumberingProperty { ) -> crate::xml::writer::Result> { XMLBuilder::from(stream) .open_numbering_property()? - .add_optional_child(&self.id)? .add_optional_child(&self.level)? + .add_optional_child(&self.id)? .close()? .into_inner() } diff --git a/docx-core/src/documents/elements/paragraph_property.rs b/docx-core/src/documents/elements/paragraph_property.rs index 29dfdca6e..fa41b6955 100644 --- a/docx-core/src/documents/elements/paragraph_property.rs +++ b/docx-core/src/documents/elements/paragraph_property.rs @@ -226,7 +226,6 @@ impl BuildXML for ParagraphProperty { ) -> crate::xml::writer::Result> { XMLBuilder::from(stream) .open_paragraph_property()? - .add_child(&self.run_property)? .add_optional_child(&self.style)? .add_optional_child(&self.numbering_property)? .add_optional_child(&self.frame_property)? diff --git a/docx-core/src/documents/elements/run_property.rs b/docx-core/src/documents/elements/run_property.rs index f8187f579..9180f6d8b 100644 --- a/docx-core/src/documents/elements/run_property.rs +++ b/docx-core/src/documents/elements/run_property.rs @@ -220,31 +220,31 @@ impl BuildXML for RunProperty { ) -> crate::xml::writer::Result> { XMLBuilder::from(stream) .open_run_property()? - .add_optional_child(&self.sz)? - .add_optional_child(&self.sz_cs)? - .add_optional_child(&self.color)? + .add_optional_child(&self.fonts)? .add_optional_child(&self.bold)? .add_optional_child(&self.bold_cs)? - .add_optional_child(&self.caps)? .add_optional_child(&self.italic)? .add_optional_child(&self.italic_cs)? + .add_optional_child(&self.caps)? .add_optional_child(&self.strike)? .add_optional_child(&self.dstrike)? - .add_optional_child(&self.highlight)? - .add_optional_child(&self.underline)? .add_optional_child(&self.vanish)? - .add_optional_child(&self.spec_vanish)? - .add_optional_child(&self.fonts)? + .add_optional_child(&self.color)? + .add_optional_child(&self.character_spacing)? + .add_optional_child(&self.sz)? + .add_optional_child(&self.sz_cs)? + .add_optional_child(&self.underline)? .add_optional_child(&self.text_border)? + .add_optional_child(&self.shading)? + .add_optional_child(&self.fit_text)? + .add_optional_child(&self.vert_align)? + .add_optional_child(&self.spec_vanish)? .add_optional_child(&self.ins)? .add_optional_child(&self.del)? - .add_optional_child(&self.vert_align)? - .add_optional_child(&self.character_spacing)? - .add_optional_child(&self.fit_text)? - .add_optional_child(&self.stretch)? .add_optional_child(&self.style)? .add_optional_child(&self.positional_tab)? - .add_optional_child(&self.shading)? + .add_optional_child(&self.stretch)? + .add_optional_child(&self.highlight)? .close()? .into_inner() } diff --git a/docx-core/src/documents/elements/section_property.rs b/docx-core/src/documents/elements/section_property.rs index 0f4a1e4e2..ac10f6d35 100644 --- a/docx-core/src/documents/elements/section_property.rs +++ b/docx-core/src/documents/elements/section_property.rs @@ -204,22 +204,22 @@ impl BuildXML for SectionProperty { ) -> crate::xml::writer::Result> { XMLBuilder::from(stream) .open_section_property()? - .add_child(&self.page_size)? - .add_child(&self.page_margin)? - .columns(&format!("{}", &self.space), &format!("{}", &self.columns))? - .add_optional_child(&self.doc_grid)? .add_optional_child(&self.header_reference)? .add_optional_child(&self.first_header_reference)? .add_optional_child(&self.even_header_reference)? .add_optional_child(&self.footer_reference)? .add_optional_child(&self.first_footer_reference)? .add_optional_child(&self.even_footer_reference)? + .apply_opt(self.section_type, |t, b| b.type_tag(&t.to_string()))? + .add_child(&self.page_size)? + .add_child(&self.page_margin)? .add_optional_child(&self.page_num_type)? + .columns(&format!("{}", &self.space), &format!("{}", &self.columns))? .apply_if(self.text_direction != "lrTb", |b| { b.text_direction(&self.text_direction) })? - .apply_opt(self.section_type, |t, b| b.type_tag(&t.to_string()))? .apply_if(self.title_pg, |b| b.title_pg())? + .add_optional_child(&self.doc_grid)? .close()? .into_inner() } diff --git a/docx-core/src/documents/elements/style.rs b/docx-core/src/documents/elements/style.rs index 116393e44..f9c202d35 100644 --- a/docx-core/src/documents/elements/style.rs +++ b/docx-core/src/documents/elements/style.rs @@ -375,20 +375,20 @@ impl BuildXML for Style { XMLBuilder::from(stream) .open_style(self.style_type, &self.style_id)? .add_child(&self.name)? - .add_child(&self.run_property)? - .add_child(&self.paragraph_property)? - .apply_if(self.style_type == StyleType::Table, |b| { - b.add_child(&self.table_cell_property)? - .add_child(&self.table_property) - })? .add_optional_child(&self.next)? .add_optional_child(&self.link)? - .apply_if(self.q_format, |b| b.add_child(&QFormat::new()))? .apply_if(self.ui_priority.is_some(), |b| { b.ui_priority(self.ui_priority.unwrap_or_default()) })? + .apply_if(self.q_format, |b| b.add_child(&QFormat::new()))? .apply_if(self.semi_hidden, |b| b.semi_hidden())? .apply_if(self.unhide_when_used, |b| b.unhide_when_used())? + .add_child(&self.paragraph_property)? + .add_child(&self.run_property)? + .apply_if(self.style_type == StyleType::Table, |b| { + b.add_child(&self.table_cell_property)? + .add_child(&self.table_property) + })? .add_optional_child(&self.based_on)? .close()? .into_inner() diff --git a/docx-core/src/documents/elements/table_grid.rs b/docx-core/src/documents/elements/table_grid.rs index 3e12d40bf..45f6cae1a 100644 --- a/docx-core/src/documents/elements/table_grid.rs +++ b/docx-core/src/documents/elements/table_grid.rs @@ -21,7 +21,7 @@ impl BuildXML for TableGrid { ) -> crate::xml::writer::Result> { XMLBuilder::from(stream) .open_table_grid()? - .apply_each(&self.grid, |g, b| b.grid_column(*g as i32, WidthType::Dxa))? + .apply_each(&self.grid, |g, b| b.grid_column(*g as i32))? .close()? .into_inner() } diff --git a/docx-core/src/documents/elements/table_property.rs b/docx-core/src/documents/elements/table_property.rs index 13e7ea61b..9b9091d96 100644 --- a/docx-core/src/documents/elements/table_property.rs +++ b/docx-core/src/documents/elements/table_property.rs @@ -157,14 +157,14 @@ impl BuildXML for TableProperty { ) -> crate::xml::writer::Result> { XMLBuilder::from(stream) .open_table_property()? + .add_optional_child(&self.style)? + .add_optional_child(&self.position)? .add_child(&self.width)? .add_child(&self.justification)? - .add_child(&self.borders)? - .add_optional_child(&self.margins)? .add_optional_child(&self.indent)? - .add_optional_child(&self.style)? + .add_child(&self.borders)? .add_optional_child(&self.layout)? - .add_optional_child(&self.position)? + .add_optional_child(&self.margins)? .close()? .into_inner() } diff --git a/docx-core/src/documents/settings.rs b/docx-core/src/documents/settings.rs index 74c5e2916..d81e93bc7 100644 --- a/docx-core/src/documents/settings.rs +++ b/docx-core/src/documents/settings.rs @@ -78,8 +78,8 @@ impl BuildXML for Settings { XMLBuilder::from(stream) .declaration(Some(true))? .open_settings()? - .add_child(&self.default_tab_stop)? .add_child(&self.zoom)? + .add_child(&self.default_tab_stop)? .open_compat()? .space_for_ul()? .balance_single_byte_double_byte_width()? diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index 992c2e17c..0939998f5 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -373,7 +373,7 @@ impl XMLBuilder { closed_with_str!(table_style, "w:tblStyle"); closed_w_with_type_el!(table_width, "w:tblW"); closed_w_with_type_el!(table_indent, "w:tblInd"); - closed_w_with_type_el!(grid_column, "w:gridCol"); + closed_w_el!(grid_column, "w:gridCol"); closed_w_with_type_el!(table_cell_width, "w:tcW"); closed!(table_row_height, "w:trHeight", "w:val", "w:hRule"); diff --git a/docx-core/src/xml_builder/macros.rs b/docx-core/src/xml_builder/macros.rs index 1248cd606..c339a94b9 100644 --- a/docx-core/src/xml_builder/macros.rs +++ b/docx-core/src/xml_builder/macros.rs @@ -508,6 +508,15 @@ macro_rules! closed_w_with_type_el { }; } +macro_rules! closed_w_el { + ($name: ident, $el_name: expr) => { + pub(crate) fn $name(self, w: i32) -> crate::xml::writer::Result { + self.write(XmlEvent::start_element($el_name).attr("w:w", &format!("{}", w)))? + .close() + } + }; +} + macro_rules! closed_border_el { ($name: ident, $el_name: expr) => { pub(crate) fn $name(