remove more duplicate classes; add additional values functions; clean up tests

This commit is contained in:
Tom Hunkapiller
2021-05-24 11:28:10 +03:00
parent a56119e7cd
commit ce2a0fb864
38 changed files with 311 additions and 362 deletions

View File

@ -1,15 +0,0 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { Bidirectional } from "./bidirectional";
describe("Bidirectional", () => {
it("should create", () => {
const bidirectional = new Bidirectional();
const tree = new Formatter().format(bidirectional);
expect(tree).to.deep.equal({
"w:bidi": {},
});
});
});

View File

@ -1,7 +0,0 @@
import { XmlComponent } from "file/xml-components";
export class Bidirectional extends XmlComponent {
constructor() {
super("w:bidi");
}
}

View File

@ -1,7 +1,6 @@
export * from "./alignment";
export * from "./border";
export * from "./indent";
export * from "./keep";
export * from "./page-break";
export * from "./spacing";
export * from "./style";

View File

@ -1,13 +0,0 @@
import { XmlComponent } from "file/xml-components";
export class KeepLines extends XmlComponent {
constructor() {
super("w:keepLines");
}
}
export class KeepNext extends XmlComponent {
constructor() {
super("w:keepNext");
}
}

View File

@ -2,7 +2,7 @@ import { expect } from "chai";
import { Formatter } from "export/formatter";
import { ContextualSpacing, Spacing } from "./spacing";
import { Spacing } from "./spacing";
describe("Spacing", () => {
describe("#constructor", () => {
@ -23,23 +23,3 @@ describe("Spacing", () => {
});
});
});
describe("ContextualSpacing", () => {
describe("#constructor", () => {
it("should create", () => {
const spacing = new ContextualSpacing(true);
const tree = new Formatter().format(spacing);
expect(tree).to.deep.equal({
"w:contextualSpacing": { _attr: { "w:val": 1 } },
});
});
it("should create with value of 0 if param is false", () => {
const spacing = new ContextualSpacing(false);
const tree = new Formatter().format(spacing);
expect(tree).to.deep.equal({
"w:contextualSpacing": { _attr: { "w:val": 0 } },
});
});
});
});

View File

@ -1,5 +1,5 @@
// http://officeopenxml.com/WPspacing.php
import { Attributes, XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export enum LineRuleType {
AT_LEAST = "atLeast",
@ -30,14 +30,3 @@ export class Spacing extends XmlComponent {
this.root.push(new SpacingAttributes(options));
}
}
export class ContextualSpacing extends XmlComponent {
constructor(value: boolean) {
super("w:contextualSpacing");
this.root.push(
new Attributes({
val: value === false ? 0 : 1,
}),
);
}
}

View File

@ -1,20 +0,0 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { WidowControl } from "./widow-control";
describe("WidowControl", () => {
it("should create", () => {
const widowControl = new WidowControl(true);
const tree = new Formatter().format(widowControl);
expect(tree).to.deep.equal({
"w:widowControl": {
_attr: {
"w:val": true,
},
},
});
});
});

View File

@ -1,13 +0,0 @@
// http://www.datypic.com/sc/ooxml/e-w_widowControl-1.html
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export class WidowControlAttributes extends XmlAttributeComponent<{ readonly val: boolean }> {
protected readonly xmlKeys = { val: "w:val" };
}
export class WidowControl extends XmlComponent {
constructor(value: boolean) {
super("w:widowControl");
this.root.push(new WidowControlAttributes({ val: value }));
}
}

View File

@ -416,7 +416,7 @@ describe("Paragraph", () => {
});
describe("#contextualSpacing()", () => {
it("should add contextualSpacing to JSON, and set 1 if true", () => {
it("should add contextualSpacing", () => {
const paragraph = new Paragraph({
contextualSpacing: true,
});
@ -424,7 +424,20 @@ describe("Paragraph", () => {
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:contextualSpacing": { _attr: { "w:val": 1 } } }],
"w:pPr": [{ "w:contextualSpacing": {} }],
},
],
});
});
it("should remove contextualSpacing", () => {
const paragraph = new Paragraph({
contextualSpacing: false,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:contextualSpacing": { _attr: { "w:val": false } } }],
},
],
});

View File

@ -86,11 +86,7 @@ describe("ParagraphProperties", () => {
expect(tree).to.deep.equal({
"w:pPr": [
{
"w:widowControl": {
_attr: {
"w:val": true,
},
},
"w:widowControl": {},
},
],
});

View File

@ -1,18 +1,15 @@
// http://officeopenxml.com/WPparagraphProperties.php
import { IContext, IgnoreIfEmptyXmlComponent, IXmlableObject, XmlComponent } from "file/xml-components";
import { IContext, IgnoreIfEmptyXmlComponent, IXmlableObject, OnOffElement, XmlComponent } from "file/xml-components";
import { DocumentWrapper } from "../document-wrapper";
import { IShadingAttributesProperties, Shading } from "../shading";
import { Alignment, AlignmentType } from "./formatting/alignment";
import { Bidirectional } from "./formatting/bidirectional";
import { Border, IBordersOptions, ThematicBreak } from "./formatting/border";
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
import { KeepLines, KeepNext } from "./formatting/keep";
import { PageBreakBefore } from "./formatting/page-break";
import { ContextualSpacing, ISpacingProperties, Spacing } from "./formatting/spacing";
import { ISpacingProperties, Spacing } from "./formatting/spacing";
import { HeadingLevel, Style } from "./formatting/style";
import { LeaderType, TabStop, TabStopPosition, TabStopType } from "./formatting/tab-stop";
import { NumberProperties } from "./formatting/unordered-list";
import { WidowControl } from "./formatting/widow-control";
import { FrameProperties, IFrameOptions } from "./frame/frame-properties";
import { OutlineLevel } from "./links";
@ -84,12 +81,12 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
this.push(new Style(options.style));
}
if (options.keepNext) {
this.push(new KeepNext());
if (options.keepNext !== undefined) {
this.push(new OnOffElement("w:keepNext", options.keepNext));
}
if (options.keepLines) {
this.push(new KeepLines());
if (options.keepLines !== undefined) {
this.push(new OnOffElement("w:keepLines", options.keepLines));
}
if (options.pageBreakBefore) {
@ -100,8 +97,8 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
this.push(new FrameProperties(options.frame));
}
if (options.widowControl) {
this.push(new WidowControl(options.widowControl));
if (options.widowControl !== undefined) {
this.push(new OnOffElement("w:widowControl", options.widowControl));
}
if (options.bullet) {
@ -143,8 +140,8 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
this.push(new TabStop(TabStopType.LEFT, options.leftTabStop));
}
if (options.bidirectional) {
this.push(new Bidirectional());
if (options.bidirectional !== undefined) {
this.push(new OnOffElement("w:bidi", options.contextualSpacing));
}
if (options.spacing) {
@ -155,8 +152,8 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
this.push(new Indent(options.indent));
}
if (options.contextualSpacing) {
this.push(new ContextualSpacing(options.contextualSpacing));
if (options.contextualSpacing !== undefined) {
this.push(new OnOffElement("w:contextualSpacing", options.contextualSpacing));
}
if (options.alignment) {

View File

@ -98,18 +98,19 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
return;
}
if (options.bold) {
this.push(new OnOffElement("w:b"));
if (options.bold !== undefined) {
this.push(new OnOffElement("w:b", options.bold));
}
if ((options.boldComplexScript === undefined && options.bold) || options.boldComplexScript) {
this.push(new OnOffElement("w:bCs"));
if ((options.boldComplexScript === undefined && options.bold !== undefined) || options.boldComplexScript) {
this.push(new OnOffElement("w:bCs", options.boldComplexScript ?? options.bold));
}
if (options.italics) {
this.push(new OnOffElement("w:i"));
if (options.italics !== undefined) {
this.push(new OnOffElement("w:i", options.italics));
}
if ((options.italicsComplexScript === undefined && options.italics) || options.italicsComplexScript) {
this.push(new OnOffElement("w:iCs"));
if ((options.italicsComplexScript === undefined && options.italics !== undefined) || options.italicsComplexScript) {
this.push(new OnOffElement("w:iCs", options.italicsComplexScript ?? options.italics));
}
if (options.underline) {
@ -124,7 +125,7 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
this.push(new Color(options.color));
}
if (options.size) {
if (options.size !== undefined) {
this.push(new HpsMeasureElement("w:sz", options.size));
}
const szCs =
@ -133,23 +134,23 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
this.push(new HpsMeasureElement("w:szCs", szCs));
}
if (options.rightToLeft) {
this.push(new OnOffElement("w:rtl"));
if (options.rightToLeft !== undefined) {
this.push(new OnOffElement("w:rtl", options.rightToLeft));
}
// These two are mutually exclusive
if (options.smallCaps) {
this.push(new OnOffElement("w:smallCaps"));
} else if (options.allCaps) {
this.push(new OnOffElement("w:caps"));
if (options.smallCaps !== undefined) {
this.push(new OnOffElement("w:smallCaps", options.smallCaps));
} else if (options.allCaps !== undefined) {
this.push(new OnOffElement("w:caps", options.allCaps));
}
if (options.strike) {
this.push(new OnOffElement("w:strike"));
if (options.strike !== undefined) {
this.push(new OnOffElement("w:strike", options.strike));
}
if (options.doubleStrike) {
this.push(new OnOffElement("w:dstrike"));
if (options.doubleStrike !== undefined) {
this.push(new OnOffElement("w:dstrike", options.doubleStrike));
}
if (options.subScript) {
@ -189,12 +190,12 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
this.push(new CharacterSpacing(options.characterSpacing));
}
if (options.emboss) {
this.push(new OnOffElement("w:emboss"));
if (options.emboss !== undefined) {
this.push(new OnOffElement("w:emboss", options.emboss));
}
if (options.imprint) {
this.push(new OnOffElement("w:imprint"));
if (options.imprint !== undefined) {
this.push(new OnOffElement("w:imprint", options.imprint));
}
if (options.shading) {

View File

@ -20,13 +20,9 @@ describe("Run", () => {
"w:r": [
{
"w:rPr": [
{ "w:b": { _attr: { "w:val": true } } },
{ "w:b": {} },
{
"w:bCs": {
_attr: {
"w:val": true,
},
},
"w:bCs": {},
},
],
},
@ -45,13 +41,9 @@ describe("Run", () => {
"w:r": [
{
"w:rPr": [
{ "w:i": { _attr: { "w:val": true } } },
{ "w:i": {} },
{
"w:iCs": {
_attr: {
"w:val": true,
},
},
"w:iCs": {},
},
],
},
@ -116,7 +108,7 @@ describe("Run", () => {
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:smallCaps": { _attr: { "w:val": true } } }] }],
"w:r": [{ "w:rPr": [{ "w:smallCaps": {} }] }],
});
});
});
@ -128,7 +120,7 @@ describe("Run", () => {
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:caps": { _attr: { "w:val": true } } }] }],
"w:r": [{ "w:rPr": [{ "w:caps": {} }] }],
});
});
});
@ -140,7 +132,7 @@ describe("Run", () => {
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:strike": { _attr: { "w:val": true } } }] }],
"w:r": [{ "w:rPr": [{ "w:strike": {} }] }],
});
});
});
@ -152,7 +144,7 @@ describe("Run", () => {
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:dstrike": { _attr: { "w:val": true } } }] }],
"w:r": [{ "w:rPr": [{ "w:dstrike": {} }] }],
});
});
});
@ -164,7 +156,7 @@ describe("Run", () => {
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:emboss": { _attr: { "w:val": true } } }] }],
"w:r": [{ "w:rPr": [{ "w:emboss": {} }] }],
});
});
});
@ -176,7 +168,7 @@ describe("Run", () => {
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:imprint": { _attr: { "w:val": true } } }] }],
"w:r": [{ "w:rPr": [{ "w:imprint": {} }] }],
});
});
});
@ -367,7 +359,7 @@ describe("Run", () => {
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:rtl": { _attr: { "w:val": true } } }] }],
"w:r": [{ "w:rPr": [{ "w:rtl": {} }] }],
});
});
});

View File

@ -59,10 +59,10 @@ describe("SymbolRun", () => {
"w:r": [
{
"w:rPr": [
{ "w:b": { _attr: { "w:val": true } } },
{ "w:bCs": { _attr: { "w:val": true } } },
{ "w:i": { _attr: { "w:val": true } } },
{ "w:iCs": { _attr: { "w:val": true } } },
{ "w:b": {} },
{ "w:bCs": {} },
{ "w:i": {} },
{ "w:iCs": {} },
{ "w:u": { _attr: { "w:val": "double", "w:color": "ff0000" } } },
{ "w:em": { _attr: { "w:val": "dot" } } },
{ "w:color": { _attr: { "w:val": "00FF00" } } },