Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Nov 21, 2024
1 parent ebf06f4 commit 96560b1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
13 changes: 8 additions & 5 deletions docx-core/src/documents/elements/table_of_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,19 @@ impl BuildXML for TableOfContents {
)
};

if let Some(paragraph_property) = self.paragraph_property.clone() {
p1.property = paragraph_property;
if let Some(ref paragraph_property) = self.paragraph_property {
p1.property = paragraph_property.clone();
}

b = b.add_child(&p1)?;

let mut p2 =
Paragraph::new().add_run(Run::new().add_field_char(FieldCharType::End, false));
if let Some(paragraph_property) = self.paragraph_property.clone() {
p2.property = paragraph_property;
}

if self.after_contents.is_empty() {
if let Some(ref paragraph_property) = self.paragraph_property {
p2.property = paragraph_property.clone();
}
b = b.add_child(&p2)?;
} else {
for (i, c) in self.after_contents.iter().enumerate() {
Expand All @@ -234,6 +234,9 @@ impl BuildXML for TableOfContents {
Run::new().add_field_char(FieldCharType::End, false),
)),
);
if let Some(ref paragraph_property) = self.paragraph_property {
new_p.property = paragraph_property.clone();
}
b = b.add_child(&new_p)?
} else {
b = b.add_child(&p)?;
Expand Down
2 changes: 1 addition & 1 deletion docx-wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docx-wasm",
"version": "0.4.18-rc22",
"version": "0.4.18-rc24",
"main": "dist/node/index.js",
"browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>",
Expand Down
4 changes: 4 additions & 0 deletions docx-wasm/test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -171045,6 +171045,10 @@ exports[`writer should write ToC with paragraphProperty 1`] = `"<?xml version=\\

exports[`writer should write ToC with paragraphProperty 2`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?><w:document xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\" xmlns:w10=\\"urn:schemas-microsoft-com:office:word\\" xmlns:wp=\\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\\" xmlns:wps=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\\" xmlns:wpg=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\\" xmlns:mc=\\"http://schemas.openxmlformats.org/markup-compatibility/2006\\" xmlns:wp14=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\\" xmlns:w14=\\"http://schemas.microsoft.com/office/word/2010/wordml\\" xmlns:w15=\\"http://schemas.microsoft.com/office/word/2012/wordml\\" mc:Ignorable=\\"w14 wp14\\"><w:body><w:sdt><w:sdtPr><w:rPr /><w:alias w:val=\\"Table of contents\\" /></w:sdtPr><w:sdtContent><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"11\\" /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType=\\"begin\\" w:dirty=\\"true\\" /><w:instrText>TOC</w:instrText><w:fldChar w:fldCharType=\\"separate\\" w:dirty=\\"false\\" /></w:r></w:p><w:p w14:paraId=\\"00000002\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"11\\" /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType=\\"end\\" w:dirty=\\"false\\" /></w:r></w:p></w:sdtContent></w:sdt><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"Heading1\\" /><w:pageBreakBefore /></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">Hello!!</w:t></w:r></w:p><w:p w14:paraId=\\"00000002\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"Heading2\\" /><w:pageBreakBefore /></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">World</w:t></w:r></w:p><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1985\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"1701\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:cols w:space=\\"425\\" w:num=\\"1\\" /></w:sectPr></w:body></w:document>"`;

exports[`writer should write ToC with paragraphProperty without sdt 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\"><Relationship Id=\\"rId1\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\\" Target=\\"styles.xml\\" /><Relationship Id=\\"rId2\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\\" Target=\\"fontTable.xml\\" /><Relationship Id=\\"rId3\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\\" Target=\\"settings.xml\\" /><Relationship Id=\\"rId5\\" Type=\\"http://schemas.microsoft.com/office/2011/relationships/commentsExtended\\" Target=\\"commentsExtended.xml\\" /></Relationships>"`;

exports[`writer should write ToC with paragraphProperty without sdt 2`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?><w:document xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\" xmlns:w10=\\"urn:schemas-microsoft-com:office:word\\" xmlns:wp=\\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\\" xmlns:wps=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\\" xmlns:wpg=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\\" xmlns:mc=\\"http://schemas.openxmlformats.org/markup-compatibility/2006\\" xmlns:wp14=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\\" xmlns:w14=\\"http://schemas.microsoft.com/office/word/2010/wordml\\" xmlns:w15=\\"http://schemas.microsoft.com/office/word/2012/wordml\\" mc:Ignorable=\\"w14 wp14\\"><w:body><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"11\\" /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType=\\"begin\\" w:dirty=\\"true\\" /><w:instrText>TOC</w:instrText><w:fldChar w:fldCharType=\\"separate\\" w:dirty=\\"false\\" /></w:r></w:p><w:p w14:paraId=\\"00000002\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"11\\" /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType=\\"end\\" w:dirty=\\"false\\" /></w:r></w:p><w:p w14:paraId=\\"00000003\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"Heading1\\" /><w:pageBreakBefore /></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">Hello!!</w:t></w:r></w:p><w:p w14:paraId=\\"00000004\\"><w:pPr><w:rPr /><w:pStyle w:val=\\"Heading2\\" /><w:pageBreakBefore /></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">World</w:t></w:r></w:p><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1985\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"1701\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:cols w:space=\\"425\\" w:num=\\"1\\" /></w:sectPr></w:body></w:document>"`;

exports[`writer should write align 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\"><Relationship Id=\\"rId1\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\\" Target=\\"styles.xml\\" /><Relationship Id=\\"rId2\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\\" Target=\\"fontTable.xml\\" /><Relationship Id=\\"rId3\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\\" Target=\\"settings.xml\\" /><Relationship Id=\\"rId5\\" Type=\\"http://schemas.microsoft.com/office/2011/relationships/commentsExtended\\" Target=\\"commentsExtended.xml\\" /></Relationships>"`;

exports[`writer should write align 2`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?><w:document xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\" xmlns:w10=\\"urn:schemas-microsoft-com:office:word\\" xmlns:wp=\\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\\" xmlns:wps=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\\" xmlns:wpg=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\\" xmlns:mc=\\"http://schemas.openxmlformats.org/markup-compatibility/2006\\" xmlns:wp14=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\\" xmlns:w14=\\"http://schemas.microsoft.com/office/word/2010/wordml\\" xmlns:w15=\\"http://schemas.microsoft.com/office/word/2012/wordml\\" mc:Ignorable=\\"w14 wp14\\"><w:body><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /><w:jc w:val=\\"both\\" /></w:pPr><w:r><w:rPr /><w:t xml:space=\\"preserve\\">Hello world!!</w:t></w:r></w:p><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1985\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"1701\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:cols w:space=\\"425\\" w:num=\\"1\\" /></w:sectPr></w:body></w:document>"`;
Expand Down
39 changes: 39 additions & 0 deletions docx-wasm/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1120,4 +1120,43 @@ describe("writer", () => {
}
}
});

test("should write ToC with paragraphProperty without sdt", () => {
const p1 = new w.Paragraph()
.addRun(new w.Run().addText("Hello!!"))
.pageBreakBefore(true)
.style("Heading1");
const style1 = new w.Style("Heading1", "paragraph").name("Heading 1");
const p2 = new w.Paragraph()
.addRun(new w.Run().addText("World"))
.pageBreakBefore(true)
.style("Heading2");
const runProperty = new w.RunProperty().bold().color("red");
const style2 = new w.Style("Heading2", "paragraph")
.name("Heading 2")
.runProperty(runProperty);
const buffer = new w.Docx()
.addTableOfContents(
new w.TableOfContents()
.alias("Table of contents")
.withoutSdt()
.dirty()
.paragraphProperty(new w.ParagraphProperty().style("11"))
)
.addParagraph(p1)
.addParagraph(p2)
.addStyle(style1)
.addStyle(style2)
.build();
writeFileSync(
"../output/js/toc_with_paragraph_property_without_sdt.docx",
buffer
);
const z = new Zip(Buffer.from(buffer));
for (const e of z.getEntries()) {
if (e.entryName.match(/document.xml/)) {
expect(z.readAsText(e)).toMatchSnapshot();
}
}
});
});

0 comments on commit 96560b1

Please sign in to comment.