Files
docx-js/src/file/file.spec.ts

520 lines
21 KiB
TypeScript
Raw Normal View History

2023-06-05 00:33:43 +01:00
import { describe, expect, it } from "vitest";
2018-11-02 00:42:49 +00:00
import { Formatter } from "@export/formatter";
2018-11-02 00:42:49 +00:00
2023-03-17 00:20:55 +00:00
import { sectionMarginDefaults, sectionPageSizeDefaults } from "./document";
2018-11-02 00:42:49 +00:00
import { File } from "./file";
2019-07-31 08:48:02 +01:00
import { Footer, Header } from "./header";
import { Paragraph } from "./paragraph";
2018-11-02 00:42:49 +00:00
const PAGE_SIZE_DEFAULTS = {
"w:h": sectionPageSizeDefaults.HEIGHT,
"w:orient": sectionPageSizeDefaults.ORIENTATION,
"w:w": sectionPageSizeDefaults.WIDTH,
};
2018-11-02 00:42:49 +00:00
describe("File", () => {
describe("#constructor", () => {
2019-01-03 02:36:56 +00:00
it("should create with correct headers and footers", () => {
2021-03-19 20:53:56 +00:00
const doc = new File({
sections: [
{
headers: {
default: new Header(),
},
footers: {
default: new Footer(),
},
children: [],
},
],
2018-11-02 00:42:49 +00:00
});
const tree = new Formatter().format(doc.Document.View.Body);
2018-11-02 00:42:49 +00:00
expect(tree["w:body"][0]["w:sectPr"][0]["w:headerReference"]._attr["w:type"]).to.equal("default");
expect(tree["w:body"][0]["w:sectPr"][1]["w:footerReference"]._attr["w:type"]).to.equal("default");
2018-11-02 00:42:49 +00:00
});
2019-01-03 02:36:56 +00:00
it("should create with first headers and footers", () => {
2021-03-19 20:53:56 +00:00
const doc = new File({
sections: [
{
headers: {
first: new Header(),
},
footers: {
first: new Footer(),
},
children: [],
},
],
2019-01-03 02:36:56 +00:00
});
const tree = new Formatter().format(doc.Document.View.Body);
expect(tree["w:body"][0]["w:sectPr"][0]["w:headerReference"]._attr["w:type"]).to.equal("first");
expect(tree["w:body"][0]["w:sectPr"][1]["w:footerReference"]._attr["w:type"]).to.equal("first");
2019-01-03 02:36:56 +00:00
});
2018-11-02 00:42:49 +00:00
it("should create with correct headers", () => {
2021-03-19 20:53:56 +00:00
const doc = new File({
sections: [
{
headers: {
default: new Header(),
first: new Header(),
even: new Header(),
},
footers: {
default: new Footer(),
first: new Footer(),
even: new Footer(),
},
children: [],
},
],
2018-11-02 00:42:49 +00:00
});
const tree = new Formatter().format(doc.Document.View.Body);
2018-11-02 00:42:49 +00:00
expect(tree["w:body"][0]["w:sectPr"][0]["w:headerReference"]._attr["w:type"]).to.equal("default");
expect(tree["w:body"][0]["w:sectPr"][1]["w:headerReference"]._attr["w:type"]).to.equal("first");
expect(tree["w:body"][0]["w:sectPr"][2]["w:headerReference"]._attr["w:type"]).to.equal("even");
2018-11-02 00:42:49 +00:00
expect(tree["w:body"][0]["w:sectPr"][3]["w:footerReference"]._attr["w:type"]).to.equal("default");
expect(tree["w:body"][0]["w:sectPr"][4]["w:footerReference"]._attr["w:type"]).to.equal("first");
expect(tree["w:body"][0]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("even");
2018-11-02 00:42:49 +00:00
});
it("should add child", () => {
2021-03-19 20:53:56 +00:00
const doc = new File({
sections: [
{
children: [new Paragraph("test")],
},
],
});
const tree = new Formatter().format(doc.Document.View.Body);
expect(tree).to.deep.equal({
"w:body": [
{
"w:p": [
{
"w:r": [
{
"w:t": [
{
_attr: {
"xml:space": "preserve",
},
},
"test",
],
},
],
},
],
},
{
"w:sectPr": [
{
"w:pgSz": {
_attr: PAGE_SIZE_DEFAULTS,
},
},
{
"w:pgMar": {
_attr: {
"w:bottom": sectionMarginDefaults.BOTTOM,
"w:footer": sectionMarginDefaults.FOOTER,
"w:gutter": sectionMarginDefaults.GUTTER,
"w:header": sectionMarginDefaults.HEADER,
"w:left": sectionMarginDefaults.LEFT,
"w:right": sectionMarginDefaults.RIGHT,
"w:top": sectionMarginDefaults.TOP,
},
},
},
{
"w:pgNumType": {
_attr: {},
},
},
// {
// "w:cols": {
// _attr: {
// "w:num": 1,
// "w:sep": false,
// "w:space": 708,
// },
// },
// },
{
"w:docGrid": {
_attr: {
"w:linePitch": 360,
},
},
},
],
},
],
});
});
2018-11-02 00:42:49 +00:00
});
2018-12-16 01:56:42 +00:00
2019-01-03 02:36:56 +00:00
describe("#createFootnote", () => {
2019-12-03 23:04:48 +00:00
it("should create footnote", () => {
const wrapper = new File({
2020-01-01 20:22:42 +00:00
footnotes: {
1: {
children: [new Paragraph("hello")],
},
},
2021-03-19 20:53:56 +00:00
sections: [],
2019-12-03 23:04:48 +00:00
});
2019-01-03 02:36:56 +00:00
2021-03-01 23:35:52 +00:00
const tree = new Formatter().format(wrapper.FootNotes.View);
2019-12-03 23:04:48 +00:00
expect(tree).to.deep.equal({
"w:footnotes": [
{
_attr: {
"mc:Ignorable": "w14 w15 wp14",
"xmlns:m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
"xmlns:mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
"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:w14": "http://schemas.microsoft.com/office/word/2010/wordml",
"xmlns:w15": "http://schemas.microsoft.com/office/word/2012/wordml",
"xmlns:wne": "http://schemas.microsoft.com/office/word/2006/wordml",
"xmlns:wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
"xmlns:wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
"xmlns:wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
"xmlns:wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
"xmlns:wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
"xmlns:wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
},
},
{
"w:footnote": [
{
_attr: {
"w:id": -1,
"w:type": "separator",
},
},
{
"w:p": [
{
"w:pPr": [
{
"w:spacing": {
_attr: {
"w:after": 0,
"w:line": 240,
"w:lineRule": "auto",
},
},
},
],
},
{
"w:r": [
{
"w:rPr": [
{
"w:rStyle": {
_attr: {
"w:val": "FootnoteReference",
},
},
},
],
},
{
"w:footnoteRef": {},
},
],
},
{
"w:r": [
{
"w:separator": {},
},
],
},
],
},
],
},
{
"w:footnote": [
{
_attr: {
"w:id": 0,
"w:type": "continuationSeparator",
},
},
{
"w:p": [
{
"w:pPr": [
{
"w:spacing": {
_attr: {
"w:after": 0,
"w:line": 240,
"w:lineRule": "auto",
},
},
},
],
},
{
"w:r": [
{
"w:rPr": [
{
"w:rStyle": {
_attr: {
"w:val": "FootnoteReference",
},
},
},
],
},
{
"w:footnoteRef": {},
},
],
},
{
"w:r": [
{
"w:continuationSeparator": {},
},
],
},
],
},
],
},
{
"w:footnote": [
{
_attr: {
"w:id": 1,
},
},
{
"w:p": [
{
"w:r": [
{
"w:rPr": [
{
"w:rStyle": {
_attr: {
"w:val": "FootnoteReference",
},
},
},
],
},
{
"w:footnoteRef": {},
},
],
},
{
"w:r": [
{
"w:t": [
{
_attr: {
"xml:space": "preserve",
},
},
"hello",
],
},
],
},
],
},
],
},
],
});
2019-01-03 02:36:56 +00:00
});
});
2021-03-09 01:37:22 +00:00
it("should create default run and paragraph property document defaults", () => {
const doc = new File({
styles: {
default: {},
},
2021-03-19 20:53:56 +00:00
sections: [],
2021-03-09 01:37:22 +00:00
});
const tree = new Formatter().format(doc.Styles);
expect(tree["w:styles"][1]).to.deep.equal({
"w:docDefaults": [
{
"w:rPrDefault": {},
},
{
"w:pPrDefault": {},
},
],
});
});
2021-03-16 00:57:27 +00:00
it("should create with even and odd headers and footers", () => {
const doc = new File({
evenAndOddHeaderAndFooters: true,
2021-03-19 20:53:56 +00:00
sections: [],
2021-03-16 00:57:27 +00:00
});
const tree = new Formatter().format(doc.Settings);
expect(tree["w:settings"][2]).to.deep.equal({ "w:evenAndOddHeaders": {} });
});
describe("#comments", () => {
it("should create comments", () => {
const doc = new File({
comments: {
children: [],
},
sections: [],
});
expect(doc.Comments).to.not.be.undefined;
});
});
describe("#numbering", () => {
it("should create", () => {
const doc = new File({
numbering: { config: [] },
sections: [],
});
expect(doc.Numbering).to.not.be.undefined;
});
});
2022-09-22 07:19:14 +01:00
describe("#getters", () => {
it("should have defined getters", () => {
const doc = new File({
sections: [],
});
expect(doc.CoreProperties).to.not.be.undefined;
expect(doc.Media).to.not.be.undefined;
expect(doc.FileRelationships).to.not.be.undefined;
expect(doc.Headers).to.not.be.undefined;
expect(doc.Footers).to.not.be.undefined;
expect(doc.ContentTypes).to.not.be.undefined;
expect(doc.CustomProperties).to.not.be.undefined;
expect(doc.AppProperties).to.not.be.undefined;
expect(doc.FootNotes).to.not.be.undefined;
expect(doc.Settings).to.not.be.undefined;
expect(doc.Comments).to.not.be.undefined;
});
});
describe("#externalStyles", () => {
it("should work with external styles", () => {
const doc = new File({
sections: [],
externalStyles: `
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:styles xmlns:mc="first" xmlns:r="second">
<w:docDefaults>
<w:rPrDefault>
<w:rPr>
<w:rFonts w:ascii="Arial" w:eastAsiaTheme="minorHAnsi" w:hAnsi="Arial" w:cstheme="minorHAnsi"/>
<w:lang w:val="en-US" w:eastAsia="en-US" w:bidi="ar-SA"/>
</w:rPr>
</w:rPrDefault>
<w:pPrDefault>
<w:pPr>
<w:spacing w:after="160" w:line="259" w:lineRule="auto"/>
</w:pPr>
</w:pPrDefault>
</w:docDefaults>
<w:latentStyles w:defLockedState="1" w:defUIPriority="99">
</w:latentStyles>
<w:style w:type="paragraph" w:default="1" w:styleId="Normal">
<w:name w:val="Normal"/>
<w:qFormat/>
</w:style>
<w:style w:type="paragraph" w:styleId="Heading1">
<w:name w:val="heading 1"/>
<w:basedOn w:val="Normal"/>
<w:pPr>
<w:keepNext/>
<w:keepLines/>
<w:pBdr>
<w:bottom w:val="single" w:sz="4" w:space="1" w:color="auto"/>
</w:pBdr>
</w:pPr>
</w:style>
</w:styles>`,
2022-09-22 07:19:14 +01:00
});
expect(doc.Styles).to.not.be.undefined;
});
});
describe("#features", () => {
it("should work with updateFields", () => {
const doc = new File({
sections: [],
features: {
updateFields: true,
},
});
expect(doc.Styles).to.not.be.undefined;
});
it("should work with trackRevisions", () => {
const doc = new File({
sections: [],
features: {
trackRevisions: true,
},
});
expect(doc.Styles).to.not.be.undefined;
});
});
describe("#hyphenation", () => {
it("should work with autoHyphenation", () => {
const doc = new File({
sections: [],
hyphenation: {
autoHyphenation: true,
},
});
expect(doc.Styles).to.not.be.undefined;
});
});
2018-11-02 00:42:49 +00:00
});