2020-10-05 16:46:18 +03:00
|
|
|
const w = require("../dist/node");
|
|
|
|
const { readFileSync, writeFileSync } = require("fs");
|
|
|
|
const Zip = require("adm-zip");
|
|
|
|
|
|
|
|
describe("reader", () => {
|
|
|
|
test("should read lvlOverride docx", () => {
|
2020-12-21 11:44:31 +02:00
|
|
|
const buffer = readFileSync("../fixtures/lvl_override/override.docx");
|
|
|
|
const json = w.readDocx(buffer);
|
2020-10-09 08:11:46 +03:00
|
|
|
expect(json).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should read gridAfter docx", () => {
|
2020-12-21 11:44:31 +02:00
|
|
|
const buffer = readFileSync("../fixtures/grid_after/grid_after.docx");
|
|
|
|
const json = w.readDocx(buffer);
|
2020-10-30 13:29:06 +02:00
|
|
|
expect(json).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2021-03-22 16:30:58 +02:00
|
|
|
test("should read tr2bl docx", () => {
|
|
|
|
const buffer = readFileSync("../fixtures/tr2bl/tr2bl.docx");
|
|
|
|
const json = w.readDocx(buffer);
|
|
|
|
writeFileSync("../output/tr2bl.json", JSON.stringify(json, null, 2));
|
|
|
|
expect(json).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2021-06-23 12:39:01 +03:00
|
|
|
test("should read custom docx", () => {
|
|
|
|
const buffer = readFileSync("../fixtures/custom/custom.docx");
|
|
|
|
const json = w.readDocx(buffer);
|
|
|
|
writeFileSync("../output/custom.json", JSON.stringify(json, null, 2));
|
|
|
|
expect(json).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2020-10-30 13:29:06 +02:00
|
|
|
test("should read table style docx", () => {
|
2020-12-21 11:44:31 +02:00
|
|
|
const buffer = readFileSync("../fixtures/table_style/table_style.docx");
|
|
|
|
const json = w.readDocx(buffer);
|
2020-10-05 16:46:18 +03:00
|
|
|
expect(json).toMatchSnapshot();
|
2020-12-15 15:33:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test("should read extended comments docx", () => {
|
2020-12-21 11:44:31 +02:00
|
|
|
const buffer = readFileSync(
|
2020-12-18 09:09:48 +02:00
|
|
|
"../fixtures/extended_comments/extended_comments.docx"
|
|
|
|
);
|
2020-12-21 11:44:31 +02:00
|
|
|
const json = w.readDocx(buffer);
|
2020-12-15 15:33:01 +02:00
|
|
|
expect(json).toMatchSnapshot();
|
2020-12-21 10:30:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test("should read multi paragraph comments docx", () => {
|
|
|
|
const buf = readFileSync(
|
|
|
|
"../fixtures/multi_paragraph_comment/multi_paragraph_comment.docx"
|
|
|
|
);
|
|
|
|
const json = w.readDocx(buf);
|
|
|
|
expect(json).toMatchSnapshot();
|
2020-10-05 16:46:18 +03:00
|
|
|
});
|
2021-04-09 05:30:50 +03:00
|
|
|
|
|
|
|
test("should read div docx", () => {
|
|
|
|
const buffer = readFileSync("../fixtures/div/div.docx");
|
|
|
|
const json = w.readDocx(buffer);
|
|
|
|
expect(json).toMatchSnapshot();
|
|
|
|
});
|
2021-06-07 18:10:44 +03:00
|
|
|
|
|
|
|
test("should read vertAlign docx", () => {
|
|
|
|
const buffer = readFileSync("../fixtures/vert_align/vert_align.docx");
|
|
|
|
const json = w.readDocx(buffer);
|
|
|
|
expect(json).toMatchSnapshot();
|
|
|
|
});
|
2020-10-05 16:46:18 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("writer", () => {
|
|
|
|
test("should write hello", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!"));
|
2020-12-21 11:44:31 +02:00
|
|
|
const buffer = new w.Docx().addParagraph(p).build();
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
2020-10-05 16:46:18 +03:00
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should write lvlOverride with level", () => {
|
|
|
|
const p = new w.Paragraph()
|
|
|
|
.addRun(new w.Run().addText("Hello world!!"))
|
|
|
|
.numbering(0, 0);
|
|
|
|
const num = new w.Numbering(0, 0);
|
|
|
|
num.addOverride(
|
|
|
|
new w.LevelOverride(0).overrideLevel(
|
|
|
|
new w.Level(0, 3, "decimal", "%1", "left")
|
|
|
|
)
|
|
|
|
);
|
2020-12-21 11:44:31 +02:00
|
|
|
const buffer = new w.Docx()
|
2020-10-05 16:46:18 +03:00
|
|
|
.addParagraph(p)
|
|
|
|
.addAbstractNumbering(new w.AbstractNumbering(0))
|
|
|
|
.addNumbering(num)
|
|
|
|
.build();
|
|
|
|
|
2020-12-21 11:44:31 +02:00
|
|
|
const z = new Zip(Buffer.from(buffer));
|
2020-10-05 16:46:18 +03:00
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-10-09 14:30:55 +03:00
|
|
|
|
|
|
|
test("should write page size", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!"));
|
2020-12-21 11:44:31 +02:00
|
|
|
const buffer = new w.Docx().addParagraph(p).pageSize(400, 800).build();
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
2020-10-09 14:30:55 +03:00
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-10-12 18:04:09 +03:00
|
|
|
|
2021-03-15 09:47:22 +02:00
|
|
|
test("should write nested table", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!"));
|
|
|
|
const childTable = new w.Table().addRow(
|
|
|
|
new w.TableRow().addCell(new w.TableCell().addParagraph(p))
|
|
|
|
);
|
|
|
|
const parentTable = new w.Table().addRow(
|
|
|
|
new w.TableRow().addCell(new w.TableCell().addTable(childTable))
|
|
|
|
);
|
|
|
|
const buffer = new w.Docx().addTable(parentTable).build();
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeFileSync("../output/nested_table.docx", buffer);
|
|
|
|
});
|
|
|
|
|
2021-03-16 09:09:39 +02:00
|
|
|
test("should write tl2br and tr2bl cells", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello!!"));
|
|
|
|
const table = new w.Table().addRow(
|
|
|
|
new w.TableRow()
|
|
|
|
.addCell(
|
|
|
|
new w.TableCell()
|
|
|
|
.setBorder(new w.TableCellBorder("tl2br"))
|
|
|
|
.addParagraph(p)
|
|
|
|
)
|
|
|
|
.addCell(
|
|
|
|
new w.TableCell()
|
|
|
|
.setBorder(new w.TableCellBorder("tr2bl"))
|
|
|
|
.addParagraph(p)
|
|
|
|
)
|
|
|
|
.addCell(
|
|
|
|
new w.TableCell()
|
|
|
|
.setBorder(new w.TableCellBorder("tr2bl"))
|
|
|
|
.setBorder(new w.TableCellBorder("tl2br"))
|
|
|
|
.addParagraph(p)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const buffer = new w.Docx().addTable(table).build();
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeFileSync("../output/cell_borders.docx", buffer);
|
|
|
|
});
|
|
|
|
|
2021-03-18 12:02:28 +02:00
|
|
|
test("should write cell shading", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello!!"));
|
|
|
|
const table = new w.Table().addRow(
|
|
|
|
new w.TableRow().addCell(
|
2021-03-19 05:31:36 +02:00
|
|
|
new w.TableCell().addParagraph(p).shading("clear", "auto", "FF0000")
|
2021-03-18 12:02:28 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
const buffer = new w.Docx().addTable(table).build();
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeFileSync("../output/cell_shading.docx", buffer);
|
|
|
|
});
|
|
|
|
|
2020-10-12 18:04:09 +03:00
|
|
|
test("should write page margin", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!"));
|
2020-12-21 11:44:31 +02:00
|
|
|
const buffer = new w.Docx()
|
2020-10-12 18:04:09 +03:00
|
|
|
.addParagraph(p)
|
|
|
|
.pageMargin({ top: 1000, left: 2000 })
|
|
|
|
.build();
|
2020-12-21 11:44:31 +02:00
|
|
|
const z = new Zip(Buffer.from(buffer));
|
2020-12-14 08:01:23 +02:00
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should write default font", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!!!"));
|
|
|
|
const fonts = new w.RunFonts()
|
|
|
|
.eastAsia("Arial")
|
|
|
|
.ascii("Arial")
|
|
|
|
.hiAnsi("Arial");
|
2020-12-21 11:44:31 +02:00
|
|
|
const buffer = new w.Docx()
|
2020-12-14 08:01:23 +02:00
|
|
|
.addParagraph(p)
|
|
|
|
.defaultSize(40)
|
|
|
|
.defaultFonts(fonts)
|
|
|
|
.build();
|
2020-12-21 11:44:31 +02:00
|
|
|
writeFileSync("../output/default_font.docx", buffer);
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
2020-12-15 08:38:17 +02:00
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should write doc vars", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!!!"));
|
2021-03-15 09:47:22 +02:00
|
|
|
const buffer = new w.Docx().addParagraph(p).addDocVar("foo", "bar").build();
|
2020-12-21 11:44:31 +02:00
|
|
|
writeFileSync("../output/doc_vars.docx", buffer);
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
2020-10-12 18:04:09 +03:00
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-03-19 18:38:22 +02:00
|
|
|
|
|
|
|
test("should write doc grid", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!!!"));
|
|
|
|
const buffer = new w.Docx().addParagraph(p).docGrid("default", 360).build();
|
|
|
|
writeFileSync("../output/doc_grid.docx", buffer);
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-03-20 17:16:43 +02:00
|
|
|
|
2021-03-23 05:01:09 +02:00
|
|
|
test("should write table layout", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello!!"));
|
|
|
|
const table = new w.Table()
|
|
|
|
.addRow(new w.TableRow().addCell(new w.TableCell().addParagraph(p)))
|
|
|
|
.layout("fixed");
|
|
|
|
const buffer = new w.Docx().addTable(table).build();
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeFileSync("../output/table_layout.docx", buffer);
|
|
|
|
});
|
|
|
|
|
2021-03-20 17:16:43 +02:00
|
|
|
test("should write text border", () => {
|
|
|
|
const p = new w.Paragraph()
|
|
|
|
.addRun(new w.Run().addText("Hello "))
|
|
|
|
.addRun(new w.Run().addText("World!").textBorder("single", 4, 0, "auto"));
|
|
|
|
const buffer = new w.Docx().addParagraph(p).build();
|
|
|
|
writeFileSync("../output/text_border.docx", buffer);
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-03-24 09:51:11 +02:00
|
|
|
|
|
|
|
test("should write page orientation", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello "));
|
|
|
|
const buffer = new w.Docx()
|
|
|
|
.addParagraph(p)
|
|
|
|
.pageSize(16838, 11906)
|
|
|
|
.pageOrientation("landscape")
|
|
|
|
.build();
|
|
|
|
writeFileSync("../output/page_orientation.docx", buffer);
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-06-23 12:39:01 +03:00
|
|
|
|
|
|
|
test("should write custom props", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello!!"));
|
|
|
|
const buffer = new w.Docx()
|
|
|
|
.addParagraph(p)
|
2021-06-30 16:37:13 +03:00
|
|
|
.customProperty("hello", '{"world": 0}')
|
2021-06-23 12:39:01 +03:00
|
|
|
.build();
|
|
|
|
writeFileSync("../output/custom.docx", buffer);
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/document.xml|numbering.xml|custom.xml/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-06-30 16:37:13 +03:00
|
|
|
|
|
|
|
test("should write webextension", () => {
|
|
|
|
const p = new w.Paragraph().addRun(new w.Run().addText("Hello!!"));
|
|
|
|
const buffer = new w.Docx()
|
|
|
|
.addParagraph(p)
|
|
|
|
.taskpanes()
|
|
|
|
.webextension(
|
|
|
|
new w.WebExtension(
|
|
|
|
"7f33b723-fb58-4524-8733-dbedc4b7c095",
|
|
|
|
"1.0.0.0",
|
|
|
|
"developer",
|
|
|
|
"Registry"
|
|
|
|
).property("hello", '"world"')
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
writeFileSync("../output/webextension.docx", buffer);
|
|
|
|
const z = new Zip(Buffer.from(buffer));
|
|
|
|
for (const e of z.getEntries()) {
|
|
|
|
if (e.entryName.match(/webextension1.xml|_rels|taskpanes.xml.rel/)) {
|
|
|
|
expect(z.readAsText(e)).toMatchSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-10-05 16:46:18 +03:00
|
|
|
});
|