fix: unwrap the imported style XML so w:style lands directly in w:styles

ImportedXmlComponent.fromXmlString returns a nameless wrapper that
serialized as a literal <undefined> element around every table and
numbering style. LibreOffice skips the unknown element; Word declares
styles.xml unreadable and rebuilds it with its own defaults, so the
recovered document also lost the 0-spacing Normal style. Regression
assertions on both DOCX style paths.
This commit is contained in:
Steffen Becker committed 2026-08-26 14:28:53 +00:00
1 parent 525c95f3a8
commit c566950258
2 files changed
+12 -2

No files matched your search

+9 -2
View File
@@ -2312,10 +2312,17 @@ function characterStyleOf(style: Style): ICharacterStyleOptions {
return { id: docxStyleId(style.name), name: style.name, quickFormat: true, run };
}
// fromXmlString returns a nameless wrapper around the parsed root that would serialize
// as a literal <undefined> element — LibreOffice skips it, Word rejects the whole part.
function styleXmlComponent(xml: string): ImportedXmlComponent {
const wrapped = ImportedXmlComponent.fromXmlString(xml);
return (wrapped as unknown as { root: ImportedXmlComponent[] }).root[0];
}
// Word needs a referenced table style to exist. ODF/our model hold the banding, and the
// look is baked into the cells, so a name-only definition is enough (no w:tblStylePr).
function tableStyleXml(name: string): ImportedXmlComponent {
return ImportedXmlComponent.fromXmlString(
return styleXmlComponent(
`<w:style w:type="table" w:styleId="${docxStyleId(name)}">`
+ `<w:name w:val="${escapeXml(name)}"/><w:basedOn w:val="TableNormal"/><w:uiPriority w:val="59"/>`
+ '</w:style>',
@@ -2337,7 +2344,7 @@ function usedTableStyles(doc: TiptapNode, sheet: StyleSheet): string[] {
// Word needs a referenced numbering style to exist; its w:numId placeholder (0) is
// rewritten by applyListStylesDocx once the packed numbering ids are known.
function numberingStyleXml(name: string): ImportedXmlComponent {
return ImportedXmlComponent.fromXmlString(
return styleXmlComponent(
`<w:style w:type="numbering" w:styleId="${docxStyleId(name)}">`
+ `<w:name w:val="${escapeXml(name)}"/><w:uiPriority w:val="99"/>`
+ '<w:pPr><w:numPr><w:numId w:val="0"/></w:numPr></w:pPr>'
+3
View File
@@ -150,6 +150,7 @@ describe('DOCX export → import round trip', () => {
const stylesXml = strFromU8(files['word/styles.xml']);
expect(xml).toContain('<w:tblStyle w:val="BoxListBlue"');
expect(stylesXml).toContain('w:type="table"');
expect(stylesXml, 'the imported style is a direct w:styles child').not.toContain('<undefined');
// The header region's white bold is presentational in the editor, baked here.
expect(xml).toContain('<w:color w:val="FFFFFF"');
@@ -1249,6 +1250,8 @@ describe('DOCX named list styles', () => {
expect(style).toContain('<w:name w:val="Prüfliste"/>');
expect(/<w:numId w:val="[1-9]\d*"\/>/.test(style), 'placeholder numId resolved').toBe(true);
expect(styles).toContain('w:styleId="DiamondBullets"');
// fromXmlString's nameless wrapper would serialize as <undefined> — Word refuses that.
expect(styles).not.toContain('<undefined');
});
it('round-trips the assignment and the definition', async () => {