fix: Add test and support table (#379)

main
bokuweb 2021-12-02 00:39:50 +09:00 committed by GitHub
parent d8f5e19e7e
commit ad229e91ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 160 additions and 8 deletions

View File

@ -173,4 +173,5 @@ $ yarn test -- --updateSnapshot
- [x] HIstory
- [ ] Table of contents
- [ ] Section
- [ ] Textbox

View File

@ -1,8 +1,9 @@
import { Paragraph } from "./paragraph";
import { Table } from "./table";
export class Footer {
hasNumberings = false;
children: Paragraph[] = [];
children: (Paragraph | Table)[] = [];
addParagraph(p: Paragraph) {
if (p.hasNumberings) {
@ -11,4 +12,12 @@ export class Footer {
this.children.push(p);
return this;
}
addTable(t: Table) {
if (t.hasNumberings) {
this.hasNumberings = true;
}
this.children.push(t);
return this;
}
}

View File

@ -1,8 +1,9 @@
import { Paragraph } from "./paragraph";
import { Table } from "./table";
export class Header {
hasNumberings = false;
children: Paragraph[] = [];
children: (Paragraph | Table)[] = [];
addParagraph(p: Paragraph) {
if (p.hasNumberings) {
@ -11,4 +12,12 @@ export class Header {
this.children.push(p);
return this;
}
addTable(t: Table) {
if (t.hasNumberings) {
this.hasNumberings = true;
}
this.children.push(t);
return this;
}
}

View File

@ -157,11 +157,31 @@ export class Docx {
return this;
}
firstHeader(h: Header) {
this.sectionProperty._firstHeader = h;
return this;
}
evenHeader(h: Header) {
this.sectionProperty._evenHeader = h;
return this;
}
footer(f: Footer) {
this.sectionProperty._footer = f;
return this;
}
firstFooter(f: Footer) {
this.sectionProperty._firstFooter = f;
return this;
}
evenFooter(f: Footer) {
this.sectionProperty._evenFooter = f;
return this;
}
pageSize(w: number, h: number) {
this.sectionProperty.pageSize(w, h);
return this;
@ -858,7 +878,11 @@ export class Docx {
if (this.sectionProperty._header) {
let header = wasm.createHeader();
this.sectionProperty._header.children.forEach((c) => {
if (c instanceof Paragraph) {
header = header.add_paragraph(this.buildParagraph(c));
} else {
header = header.add_table(this.buildTable(c));
}
});
docx = docx.header(header);
}
@ -866,7 +890,11 @@ export class Docx {
if (this.sectionProperty._firstHeader) {
let header = wasm.createHeader();
this.sectionProperty._firstHeader.children.forEach((c) => {
if (c instanceof Paragraph) {
header = header.add_paragraph(this.buildParagraph(c));
} else {
header = header.add_table(this.buildTable(c));
}
});
docx = docx.first_header(header);
}
@ -874,7 +902,11 @@ export class Docx {
if (this.sectionProperty._evenHeader) {
let header = wasm.createHeader();
this.sectionProperty._evenHeader.children.forEach((c) => {
if (c instanceof Paragraph) {
header = header.add_paragraph(this.buildParagraph(c));
} else {
header = header.add_table(this.buildTable(c));
}
});
docx = docx.even_header(header);
}
@ -882,7 +914,11 @@ export class Docx {
if (this.sectionProperty._footer) {
let footer = wasm.createFooter();
this.sectionProperty._footer.children.forEach((c) => {
if (c instanceof Paragraph) {
footer = footer.add_paragraph(this.buildParagraph(c));
} else {
footer = footer.add_table(this.buildTable(c));
}
});
docx = docx.footer(footer);
}
@ -890,7 +926,11 @@ export class Docx {
if (this.sectionProperty._firstFooter) {
let footer = wasm.createFooter();
this.sectionProperty._firstFooter.children.forEach((c) => {
if (c instanceof Paragraph) {
footer = footer.add_paragraph(this.buildParagraph(c));
} else {
footer = footer.add_table(this.buildTable(c));
}
});
docx = docx.first_footer(footer);
}
@ -898,7 +938,11 @@ export class Docx {
if (this.sectionProperty._evenFooter) {
let footer = wasm.createFooter();
this.sectionProperty._evenFooter.children.forEach((c) => {
if (c instanceof Paragraph) {
footer = footer.add_paragraph(this.buildParagraph(c));
} else {
footer = footer.add_table(this.buildTable(c));
}
});
docx = docx.even_footer(footer);
}

View File

@ -25396,6 +25396,63 @@ exports[`writer should write doc vars 3`] = `
</w:num></w:numbering>"
`;
exports[`writer should write evenFooter with table for default section 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\\" />
<Relationship Id=\\"rIdFooter1\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer\\" Target=\\"footer1.xml\\" />
</Relationships>"
`;
exports[`writer should write evenFooter with table for default section 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: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:docGrid w:type=\\"lines\\" w:linePitch=\\"360\\" /><w:footerReference w:type=\\"even\\" r:id=\\"rIdFooter1\\" /></w:sectPr></w:body>
</w:document>"
`;
exports[`writer should write evenFooter with table for default section 3`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?>
<w:ftr xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" 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\\" mc:Ignorable=\\"w14 wp14\\"><w:tbl><w:tblPr><w:tblW w:w=\\"0\\" w:type=\\"dxa\\" /><w:jc w:val=\\"left\\" /><w:tblBorders><w:top w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:left w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:bottom w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:right w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideH w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideV w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /></w:tblBorders><w:tblCellMar>
<w:top w:w=\\"0\\" w:type=\\"dxa\\" />
<w:left w:w=\\"55\\" w:type=\\"dxa\\" />
<w:bottom w:w=\\"0\\" w:type=\\"dxa\\" />
<w:right w:w=\\"55\\" w:type=\\"dxa\\" />
</w:tblCellMar><w:tblInd w:w=\\"0\\" w:type=\\"dxa\\" /></w:tblPr><w:tblGrid /><w:tr><w:trPr /><w:tc><w:tcPr><w:tcBorders><w:top w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:left w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:bottom w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:right w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideH w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideV w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /></w:tcBorders></w:tcPr><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr><w:rFonts /></w:rPr><w:t xml:space=\\"preserve\\">Hello Footer!!</w:t></w:r></w:p></w:tc></w:tr></w:tbl></w:ftr>"
`;
exports[`writer should write firstHeader with table for default section 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\\" />
<Relationship Id=\\"rIdHeader1\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header\\" Target=\\"header1.xml\\" />
</Relationships>"
`;
exports[`writer should write firstHeader with table for default section 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: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:docGrid w:type=\\"lines\\" w:linePitch=\\"360\\" /><w:headerReference w:type=\\"first\\" r:id=\\"rIdHeader1\\" /><w:titlePg />
</w:sectPr></w:body>
</w:document>"
`;
exports[`writer should write firstHeader with table for default section 3`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?>
<w:hdr xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" 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\\" mc:Ignorable=\\"w14 wp14\\"><w:tbl><w:tblPr><w:tblW w:w=\\"0\\" w:type=\\"dxa\\" /><w:jc w:val=\\"left\\" /><w:tblBorders><w:top w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:left w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:bottom w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:right w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideH w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideV w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /></w:tblBorders><w:tblCellMar>
<w:top w:w=\\"0\\" w:type=\\"dxa\\" />
<w:left w:w=\\"55\\" w:type=\\"dxa\\" />
<w:bottom w:w=\\"0\\" w:type=\\"dxa\\" />
<w:right w:w=\\"55\\" w:type=\\"dxa\\" />
</w:tblCellMar><w:tblInd w:w=\\"0\\" w:type=\\"dxa\\" /></w:tblPr><w:tblGrid /><w:tr><w:trPr /><w:tc><w:tcPr><w:tcBorders><w:top w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:left w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:bottom w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:right w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideH w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideV w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /></w:tcBorders></w:tcPr><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr><w:rFonts /></w:rPr><w:t xml:space=\\"preserve\\">Hello Header!!</w:t></w:r></w:p></w:tc></w:tr></w:tbl></w:hdr>"
`;
exports[`writer should write footer for default section 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\">

View File

@ -404,4 +404,36 @@ describe("writer", () => {
}
}
});
test("should write firstHeader with table for default section", () => {
const p = new w.Paragraph().addRun(new w.Run().addText("Hello Header!!"));
const table = new w.Table().addRow(
new w.TableRow().addCell(new w.TableCell().addParagraph(p))
);
const header = new w.Header().addTable(table);
const buffer = new w.Docx().firstHeader(header).build();
writeFileSync("../output/first_header_with_table.docx", buffer);
const z = new Zip(Buffer.from(buffer));
for (const e of z.getEntries()) {
if (e.entryName.match(/document.xml|header[1-9].xml/)) {
expect(z.readAsText(e)).toMatchSnapshot();
}
}
});
test("should write evenFooter with table for default section", () => {
const p = new w.Paragraph().addRun(new w.Run().addText("Hello Footer!!"));
const table = new w.Table().addRow(
new w.TableRow().addCell(new w.TableCell().addParagraph(p))
);
const footer = new w.Footer().addTable(table);
const buffer = new w.Docx().evenFooter(footer).build();
writeFileSync("../output/even_footer_with_table.docx", buffer);
const z = new Zip(Buffer.from(buffer));
for (const e of z.getEntries()) {
if (e.entryName.match(/document.xml|footer[1-9].xml/)) {
expect(z.readAsText(e)).toMatchSnapshot();
}
}
});
});