Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/files/src/StylesXml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class StylesXml extends XmlFile {
}
)
)
)
)
: null,
})
)
Expand Down Expand Up @@ -390,7 +390,7 @@ export class StylesXml extends XmlFile {
}),
{}
),
}
}
: {}),
},
};
Expand Down Expand Up @@ -421,7 +421,13 @@ export class StylesXml extends XmlFile {
location: string
): Promise<StylesXml> {
if (archive.hasFile(location)) {
const theme = await ThemeXml.fromArchive(archive);
let theme: ThemeXml | undefined;
try {
theme = await ThemeXml.fromArchive(archive);
} catch (_) {
// no-op

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a warning or something similar?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could add a console.warn or something similar here, but it won't be captured.

// something happened, the theme document couldn't be read.
}
const dom = await archive.readXml(location);
return this.fromDom(dom, location, theme);
}
Expand Down
40 changes: 40 additions & 0 deletions lib/files/test/StylesXml.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { expect } from 'std/expect';
import { describe, it } from 'std/testing/bdd';

import { Docx } from '../../Docx.ts';
import { Paragraph } from '../../components/document/src/Paragraph.ts';
import { Section } from '../../components/document/src/Section.ts';
import { Text } from '../../components/document/src/Text.ts';
import { serialize } from '../../utilities/src/dom.ts';
import { pt } from '../../utilities/src/length.ts';
import { StylesXml } from '../src/StylesXml.ts';

describe('Styles', () => {
Expand Down Expand Up @@ -83,4 +88,39 @@ describe('Styles', () => {
color: '000000',
});
});

it('can read a document with no theme', async () => {
const docx = Docx.fromNothing();
const style = docx.document.styles.add({
id: 'Text',
name: 'Text',
type: 'paragraph',
text: {
fontSize: pt(9),
},
});
docx.document.set([
new Section({}, new Paragraph({ style }, new Text({}, 'foo'))),
]);
const archive = await docx.toArchive();

// word/theme/theme1.xml is the default location for the first theme that is included in a document.
let theme: string | null;
try {
theme = await archive.readText('word/theme/theme1.xml');
} catch (_) {
theme = null;
}
// It shouldn't be there.
expect(theme).toBeFalsy();

// styles.xml is the default location for the first theme that is included in a document.
// This call shouldn't crash if there's no theme.
const styles = await StylesXml.fromArchive(archive, 'styles.xml');
expect(styles).toBeTruthy();

// Same here, shouldn't crash when loading the whole doc.
const fromArchive = Docx.fromArchive(archive);
expect(fromArchive).toBeTruthy();
});
});