Merge pull request #783 from dolanmiu/feat/compat-mode
Feat/compat mode
This commit is contained in:
@ -37,6 +37,7 @@ export interface IPropertiesOptions {
|
||||
readonly features?: {
|
||||
readonly trackRevisions?: boolean;
|
||||
};
|
||||
readonly compatabilityModeVersion?: number;
|
||||
}
|
||||
|
||||
export class CoreProperties extends XmlComponent {
|
||||
|
@ -86,7 +86,9 @@ export class File {
|
||||
this.footNotes = new FootNotes();
|
||||
this.contentTypes = new ContentTypes();
|
||||
this.document = new Document({ background: options.background || {} });
|
||||
this.settings = new Settings();
|
||||
this.settings = new Settings({
|
||||
compatabilityModeVersion: options.compatabilityModeVersion,
|
||||
});
|
||||
|
||||
this.media = fileProperties.template && fileProperties.template.media ? fileProperties.template.media : new Media();
|
||||
|
||||
|
@ -0,0 +1,23 @@
|
||||
import { expect } from "chai";
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { CompatibilitySetting } from "./compatibility-setting";
|
||||
|
||||
describe("CompatibilitySetting", () => {
|
||||
describe("#constructor", () => {
|
||||
it("creates an initially empty property object", () => {
|
||||
const compatibilitySetting = new CompatibilitySetting(15);
|
||||
|
||||
const tree = new Formatter().format(compatibilitySetting);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:compatSetting": {
|
||||
_attr: {
|
||||
"w:name": "compatibilityMode",
|
||||
"w:uri": "http://schemas.microsoft.com/office/word",
|
||||
"w:val": 15,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@ -0,0 +1,27 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
export class CompatibilitySettingAttributes extends XmlAttributeComponent<{
|
||||
readonly version: number;
|
||||
readonly name: string;
|
||||
readonly uri: string;
|
||||
}> {
|
||||
protected readonly xmlKeys = {
|
||||
version: "w:val",
|
||||
name: "w:name",
|
||||
uri: "w:uri",
|
||||
};
|
||||
}
|
||||
|
||||
export class CompatibilitySetting extends XmlComponent {
|
||||
constructor(version: number) {
|
||||
super("w:compatSetting");
|
||||
|
||||
this.root.push(
|
||||
new CompatibilitySettingAttributes({
|
||||
version,
|
||||
uri: "http://schemas.microsoft.com/office/word",
|
||||
name: "compatibilityMode",
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,13 +1,14 @@
|
||||
import { expect } from "chai";
|
||||
import { Formatter } from "export/formatter";
|
||||
import { Compatibility } from "file/settings/compatibility";
|
||||
|
||||
import { EMPTY_OBJECT } from "file/xml-components";
|
||||
|
||||
import { Compatibility } from "./compatibility";
|
||||
|
||||
describe("Compatibility", () => {
|
||||
describe("#constructor", () => {
|
||||
it("creates an initially empty property object", () => {
|
||||
const compatibility = new Compatibility();
|
||||
const compatibility = new Compatibility({});
|
||||
|
||||
const tree = new Formatter().format(compatibility);
|
||||
expect(tree).to.deep.equal({ "w:compat": EMPTY_OBJECT });
|
||||
@ -16,8 +17,9 @@ describe("Compatibility", () => {
|
||||
|
||||
describe("#doNotExpandShiftReturn", () => {
|
||||
it("should create a setting for not justifying lines ending in soft line break", () => {
|
||||
const compatibility = new Compatibility();
|
||||
compatibility.doNotExpandShiftReturn();
|
||||
const compatibility = new Compatibility({
|
||||
doNotExpandShiftReturn: true,
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(compatibility);
|
||||
expect(tree).to.deep.equal({ "w:compat": [{ "w:doNotExpandShiftReturn": EMPTY_OBJECT }] });
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { CompatibilitySetting } from "./compatibility-setting/compatibility-setting";
|
||||
|
||||
class DoNotExpandShiftReturn extends XmlComponent {
|
||||
constructor() {
|
||||
@ -6,14 +7,21 @@ class DoNotExpandShiftReturn extends XmlComponent {
|
||||
}
|
||||
}
|
||||
|
||||
export interface ICompatibilityOptions {
|
||||
readonly doNotExpandShiftReturn?: boolean;
|
||||
readonly version?: number;
|
||||
}
|
||||
|
||||
export class Compatibility extends XmlComponent {
|
||||
constructor() {
|
||||
constructor(options: ICompatibilityOptions) {
|
||||
super("w:compat");
|
||||
}
|
||||
|
||||
public doNotExpandShiftReturn(): Compatibility {
|
||||
this.root.push(new DoNotExpandShiftReturn());
|
||||
if (options.doNotExpandShiftReturn) {
|
||||
this.root.push(new DoNotExpandShiftReturn());
|
||||
}
|
||||
|
||||
return this;
|
||||
if (options.version) {
|
||||
this.root.push(new CompatibilitySetting(options.version));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import { Settings } from "./settings";
|
||||
describe("Settings", () => {
|
||||
describe("#constructor", () => {
|
||||
it("should create a empty Settings with correct rootKey", () => {
|
||||
const settings = new Settings();
|
||||
const settings = new Settings({});
|
||||
const tree = new Formatter().format(settings);
|
||||
let keys = Object.keys(tree);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
@ -15,7 +15,7 @@ describe("Settings", () => {
|
||||
expect(keys[0]).to.be.equal("w:settings");
|
||||
keys = Object.keys(tree["w:settings"]);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(2);
|
||||
expect(keys).has.length(3);
|
||||
});
|
||||
});
|
||||
describe("#addUpdateFields", () => {
|
||||
@ -27,16 +27,16 @@ describe("Settings", () => {
|
||||
expect(keys[0]).to.be.equal("w:settings");
|
||||
const rootArray = tree["w:settings"];
|
||||
expect(rootArray).is.an.instanceof(Array);
|
||||
expect(rootArray).has.length(3);
|
||||
expect(rootArray).has.length(4);
|
||||
keys = Object.keys(rootArray[0]);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("_attr");
|
||||
keys = Object.keys(rootArray[2]);
|
||||
keys = Object.keys(rootArray[3]);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("w:updateFields");
|
||||
const updateFields = rootArray[2]["w:updateFields"];
|
||||
const updateFields = rootArray[3]["w:updateFields"];
|
||||
keys = Object.keys(updateFields);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
@ -45,12 +45,12 @@ describe("Settings", () => {
|
||||
expect(updateFieldsAttr["w:val"]).to.be.equal(true);
|
||||
};
|
||||
it("should add a UpdateFields with value true", () => {
|
||||
const settings = new Settings();
|
||||
const settings = new Settings({});
|
||||
settings.addUpdateFields();
|
||||
assertSettingsWithUpdateFields(settings);
|
||||
});
|
||||
it("should add a UpdateFields with value true only once", () => {
|
||||
const settings = new Settings();
|
||||
const settings = new Settings({});
|
||||
settings.addUpdateFields();
|
||||
assertSettingsWithUpdateFields(settings);
|
||||
settings.addUpdateFields();
|
||||
@ -58,9 +58,8 @@ describe("Settings", () => {
|
||||
});
|
||||
});
|
||||
describe("#addCompatibility", () => {
|
||||
it("should add an empty Compatibility", () => {
|
||||
const settings = new Settings();
|
||||
settings.addCompatibility();
|
||||
it("should add an empty Compatibility by default", () => {
|
||||
const settings = new Settings({});
|
||||
|
||||
const tree = new Formatter().format(settings);
|
||||
let keys: string[] = Object.keys(tree);
|
||||
@ -72,15 +71,24 @@ describe("Settings", () => {
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("_attr");
|
||||
keys = Object.keys(rootArray[2]);
|
||||
keys = Object.keys(rootArray[1]);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("w:compat");
|
||||
expect(rootArray[1]["w:compat"][0]).to.deep.equal({
|
||||
"w:compatSetting": {
|
||||
_attr: {
|
||||
"w:val": 15,
|
||||
"w:uri": "http://schemas.microsoft.com/office/word",
|
||||
"w:name": "compatibilityMode",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
describe("#addTrackRevisions", () => {
|
||||
it("should add an empty Track Revisions", () => {
|
||||
const settings = new Settings();
|
||||
const settings = new Settings({});
|
||||
settings.addTrackRevisions();
|
||||
|
||||
const tree = new Formatter().format(settings);
|
||||
@ -88,12 +96,12 @@ describe("Settings", () => {
|
||||
expect(keys[0]).to.be.equal("w:settings");
|
||||
const rootArray = tree["w:settings"];
|
||||
expect(rootArray).is.an.instanceof(Array);
|
||||
expect(rootArray).has.length(3);
|
||||
expect(rootArray).has.length(4);
|
||||
keys = Object.keys(rootArray[0]);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("_attr");
|
||||
keys = Object.keys(rootArray[2]);
|
||||
keys = Object.keys(rootArray[3]);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("w:trackRevisions");
|
||||
@ -101,7 +109,7 @@ describe("Settings", () => {
|
||||
});
|
||||
describe("#addTrackRevisionsTwice", () => {
|
||||
it("should add an empty Track Revisions if called twice", () => {
|
||||
const settings = new Settings();
|
||||
const settings = new Settings({});
|
||||
settings.addTrackRevisions();
|
||||
settings.addTrackRevisions();
|
||||
|
||||
@ -110,12 +118,12 @@ describe("Settings", () => {
|
||||
expect(keys[0]).to.be.equal("w:settings");
|
||||
const rootArray = tree["w:settings"];
|
||||
expect(rootArray).is.an.instanceof(Array);
|
||||
expect(rootArray).has.length(3);
|
||||
expect(rootArray).has.length(4);
|
||||
keys = Object.keys(rootArray[0]);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("_attr");
|
||||
keys = Object.keys(rootArray[2]);
|
||||
keys = Object.keys(rootArray[3]);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("w:trackRevisions");
|
||||
|
@ -46,11 +46,15 @@ export class SettingsAttributes extends XmlAttributeComponent<ISettingsAttribute
|
||||
};
|
||||
}
|
||||
|
||||
export interface ISettingsOptions {
|
||||
readonly compatabilityModeVersion?: number;
|
||||
}
|
||||
|
||||
export class Settings extends XmlComponent {
|
||||
private readonly compatibility: Compatibility;
|
||||
private readonly trackRevisions: TrackRevisions;
|
||||
|
||||
constructor() {
|
||||
constructor(options: ISettingsOptions) {
|
||||
super("w:settings");
|
||||
this.root.push(
|
||||
new SettingsAttributes({
|
||||
@ -74,7 +78,11 @@ export class Settings extends XmlComponent {
|
||||
}),
|
||||
);
|
||||
|
||||
this.compatibility = new Compatibility();
|
||||
this.compatibility = new Compatibility({
|
||||
version: options.compatabilityModeVersion || 15,
|
||||
});
|
||||
|
||||
this.root.push(this.compatibility);
|
||||
this.trackRevisions = new TrackRevisions();
|
||||
|
||||
this.root.push(new DisplayBackgroundShape());
|
||||
@ -86,14 +94,6 @@ export class Settings extends XmlComponent {
|
||||
}
|
||||
}
|
||||
|
||||
public addCompatibility(): Compatibility {
|
||||
if (!this.root.find((child) => child instanceof Compatibility)) {
|
||||
this.addChildElement(this.compatibility);
|
||||
}
|
||||
|
||||
return this.compatibility;
|
||||
}
|
||||
|
||||
public addTrackRevisions(): TrackRevisions {
|
||||
if (!this.root.find((child) => child instanceof TrackRevisions)) {
|
||||
this.addChildElement(this.trackRevisions);
|
||||
|
Reference in New Issue
Block a user