Allow XmlComponent to take strings and remove XmlUnit
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
import { XmlUnitComponent } from "../xml-components";
|
||||
import { XmlComponent } from "../xml-components";
|
||||
|
||||
export class Text extends XmlUnitComponent {
|
||||
export class Text extends XmlComponent {
|
||||
|
||||
constructor(text: string) {
|
||||
super("w:t");
|
||||
this.root = text;
|
||||
if (text) {
|
||||
this.root.push(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,10 @@ import * as _ from "lodash";
|
||||
import { BaseXmlComponent } from "./base";
|
||||
|
||||
export abstract class XmlAttributeComponent extends BaseXmlComponent {
|
||||
protected root: Object;
|
||||
private xmlKeys: Object;
|
||||
protected root: object;
|
||||
private xmlKeys: object;
|
||||
|
||||
constructor(xmlKeys: Object, properties: Object) {
|
||||
constructor(xmlKeys: object, properties: object) {
|
||||
super("_attr");
|
||||
this.xmlKeys = xmlKeys;
|
||||
|
||||
|
@ -2,7 +2,7 @@ import * as _ from "lodash";
|
||||
import { BaseXmlComponent } from "./base";
|
||||
|
||||
export abstract class XmlComponent extends BaseXmlComponent {
|
||||
protected root: BaseXmlComponent[];
|
||||
protected root: Array<BaseXmlComponent | string>;
|
||||
|
||||
constructor(rootKey: string) {
|
||||
super(rootKey);
|
||||
@ -26,4 +26,3 @@ export abstract class XmlComponent extends BaseXmlComponent {
|
||||
|
||||
export * from "./attributes"
|
||||
export * from "./default-attributes";
|
||||
export * from "./unit";
|
||||
|
@ -1,16 +0,0 @@
|
||||
import {BaseXmlComponent} from "./base";
|
||||
|
||||
export abstract class XmlUnitComponent extends BaseXmlComponent {
|
||||
protected root: string;
|
||||
|
||||
constructor(rootKey: string) {
|
||||
super(rootKey);
|
||||
}
|
||||
|
||||
public replaceKey(): void {
|
||||
if (this.root !== undefined) {
|
||||
this[this.rootKey] = this.root;
|
||||
delete this.root;
|
||||
}
|
||||
}
|
||||
}
|
@ -43,7 +43,9 @@ export class AbstractNumbering extends XmlComponent {
|
||||
|
||||
public clearVariables(): void {
|
||||
_.forEach(this.root, (element) => {
|
||||
element.clearVariables();
|
||||
if (element instanceof XmlComponent) {
|
||||
element.clearVariables();
|
||||
}
|
||||
});
|
||||
delete this.id;
|
||||
}
|
||||
|
@ -90,7 +90,9 @@ export class Numbering extends XmlComponent {
|
||||
public clearVariables(): void {
|
||||
super.clearVariables();
|
||||
_.forEach(this.root, (element) => {
|
||||
element.clearVariables();
|
||||
if (element instanceof XmlComponent) {
|
||||
element.clearVariables();
|
||||
}
|
||||
});
|
||||
delete this.nextId;
|
||||
}
|
||||
|
@ -1,66 +1,64 @@
|
||||
import { DocumentAttributes } from "../docx/document/document-attributes";
|
||||
import { XmlUnitComponent } from "../docx/xml-components";
|
||||
import { XmlComponent } from "../docx/xml-components";
|
||||
|
||||
export class Title extends XmlUnitComponent {
|
||||
export class Title extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("dc:title");
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
export class Subject extends XmlUnitComponent {
|
||||
export class Subject extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("dc:subject");
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
export class Creator extends XmlUnitComponent {
|
||||
export class Creator extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("dc:creator");
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
export class Keywords extends XmlUnitComponent {
|
||||
export class Keywords extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("cp:keywords");
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
export class Description extends XmlUnitComponent {
|
||||
export class Description extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("dc:description");
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
export class LastModifiedBy extends XmlUnitComponent {
|
||||
export class LastModifiedBy extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("cp:lastModifiedBy");
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
export class Revision extends XmlUnitComponent {
|
||||
export class Revision extends XmlComponent {
|
||||
|
||||
constructor(value: string) {
|
||||
super("cp:revision");
|
||||
const revision = value;
|
||||
this.root = value;
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class DateComponent extends XmlComponent {
|
||||
protected getCurrentDate(): any {
|
||||
protected getCurrentDate(): string {
|
||||
const date = new Date();
|
||||
const year = date.getFullYear();
|
||||
const month = ("0" + (date.getMonth() + 1)).slice(-2);
|
||||
|
@ -23,13 +23,27 @@ export class Properties extends XmlComponent {
|
||||
dcmitype: "http://purl.org/dc/dcmitype/",
|
||||
xsi: "http://www.w3.org/2001/XMLSchema-instance",
|
||||
}));
|
||||
this.root.push(new Title(options.title));
|
||||
this.root.push(new Subject(options.subject));
|
||||
this.root.push(new Creator(options.creator));
|
||||
this.root.push(new Keywords(options.keywords));
|
||||
this.root.push(new Description(options.description));
|
||||
this.root.push(new LastModifiedBy(options.lastModifiedBy));
|
||||
this.root.push(new Revision(options.revision));
|
||||
if (options.title) {
|
||||
this.root.push(new Title(options.title));
|
||||
}
|
||||
if (options.subject) {
|
||||
this.root.push(new Subject(options.subject));
|
||||
}
|
||||
if (options.creator) {
|
||||
this.root.push(new Creator(options.creator));
|
||||
}
|
||||
if (options.keywords) {
|
||||
this.root.push(new Keywords(options.keywords));
|
||||
}
|
||||
if (options.description) {
|
||||
this.root.push(new Description(options.description));
|
||||
}
|
||||
if (options.lastModifiedBy) {
|
||||
this.root.push(new LastModifiedBy(options.lastModifiedBy));
|
||||
}
|
||||
if (options.revision) {
|
||||
this.root.push(new Revision(options.revision));
|
||||
}
|
||||
this.root.push(new Created());
|
||||
this.root.push(new Modified());
|
||||
}
|
||||
|
@ -32,7 +32,9 @@ export class Styles extends XmlComponent {
|
||||
|
||||
public clearVariables(): void {
|
||||
this.root.forEach((element) => {
|
||||
element.clearVariables();
|
||||
if (element instanceof XmlComponent) {
|
||||
element.clearVariables();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1,36 +0,0 @@
|
||||
import { XmlUnitComponent } from "../../../docx/xml-components";
|
||||
import { assert } from "chai";
|
||||
|
||||
function jsonify(obj: Object) {
|
||||
let stringifiedJson = JSON.stringify(obj);
|
||||
return JSON.parse(stringifiedJson);
|
||||
}
|
||||
|
||||
class TestComponent extends XmlUnitComponent {
|
||||
|
||||
}
|
||||
|
||||
describe("XmlUnitComponent", () => {
|
||||
let xmlComponent: TestComponent;
|
||||
|
||||
beforeEach(() => {
|
||||
xmlComponent = new TestComponent("w:test");
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
|
||||
it("should create an Xml Component which has the correct rootKey", () => {
|
||||
let newJson = jsonify(xmlComponent);
|
||||
assert.equal(newJson.rootKey, "w:test");
|
||||
});
|
||||
});
|
||||
|
||||
describe("#replaceKey", () => {
|
||||
|
||||
it("should not replace the key to the specified root key as root is null", () => {
|
||||
xmlComponent.replaceKey();
|
||||
let newJson = jsonify(xmlComponent);
|
||||
assert.isUndefined(newJson["w:test"]);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,25 +1,74 @@
|
||||
import { Properties } from "../properties";
|
||||
import { assert } from "chai";
|
||||
import { expect } from "chai";
|
||||
|
||||
function jsonify(obj: Object) {
|
||||
let stringifiedJson = JSON.stringify(obj);
|
||||
return JSON.parse(stringifiedJson);
|
||||
}
|
||||
import { Formatter } from "../export/formatter";
|
||||
import { Properties } from "../properties";
|
||||
|
||||
describe("Properties", () => {
|
||||
let properties: Properties;
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
it("should create properties with a title", () => {
|
||||
properties = new Properties({
|
||||
title: "test document"
|
||||
it("sets the appropriate attributes on the top-level", () => {
|
||||
const properties = new Properties({});
|
||||
const tree = new Formatter().format(properties);
|
||||
expect(Object.keys(tree)).to.deep.equal(["cp:coreProperties"]);
|
||||
expect(tree["cp:coreProperties"]).to.be.an.instanceof(Array);
|
||||
expect(tree["cp:coreProperties"][0]).to.deep.equal({
|
||||
_attr: {
|
||||
"xmlns:cp": "http://schemas.openxmlformats.org/package/2006/metadata/core-properties",
|
||||
"xmlns:dc": "http://purl.org/dc/elements/1.1/",
|
||||
"xmlns:dcmitype": "http://purl.org/dc/dcmitype/",
|
||||
"xmlns:dcterms": "http://purl.org/dc/terms/",
|
||||
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
|
||||
},
|
||||
});
|
||||
let newJson = jsonify(properties);
|
||||
assert(newJson.root[1].root === "test document");
|
||||
});
|
||||
|
||||
it("should create properties with a title", () => {
|
||||
const properties = new Properties({title: "test document"});
|
||||
const tree = new Formatter().format(properties);
|
||||
expect(Object.keys(tree)).to.deep.equal(["cp:coreProperties"]);
|
||||
expect(tree["cp:coreProperties"]).to.be.an.instanceof(Array);
|
||||
expect(Object.keys(tree["cp:coreProperties"][0])).to.deep.equal(["_attr"]);
|
||||
expect(tree["cp:coreProperties"][1]).to.deep.equal(
|
||||
{"dc:title": ["test document"]},
|
||||
);
|
||||
});
|
||||
|
||||
it("should create properties with all the attributes given", () => {
|
||||
const properties = new Properties({
|
||||
title: "test document",
|
||||
subject: "test subject",
|
||||
creator: "me",
|
||||
keywords: "test docx",
|
||||
description: "testing document",
|
||||
lastModifiedBy: "the author",
|
||||
revision: "123",
|
||||
});
|
||||
const tree = new Formatter().format(properties);
|
||||
expect(Object.keys(tree)).to.deep.equal(["cp:coreProperties"]);
|
||||
expect(tree["cp:coreProperties"]).to.be.an.instanceof(Array);
|
||||
const key = (obj) => Object.keys(obj)[0];
|
||||
const props = tree["cp:coreProperties"].map(key).sort();
|
||||
expect(props).to.deep.equal([
|
||||
"_attr",
|
||||
"cp:keywords",
|
||||
"cp:lastModifiedBy",
|
||||
"cp:revision",
|
||||
"dc:creator",
|
||||
"dc:description",
|
||||
"dc:subject",
|
||||
"dc:title",
|
||||
"dcterms:created",
|
||||
"dcterms:modified",
|
||||
]);
|
||||
expect(tree["cp:coreProperties"].slice(1, -2).sort((a, b) => key(a) < key(b) ? -1 : 1)).to.deep.equal([
|
||||
{"cp:keywords": ["test docx"]},
|
||||
{"cp:lastModifiedBy": ["the author"]},
|
||||
{"cp:revision": ["123"]},
|
||||
{"dc:creator": ["me"]},
|
||||
{"dc:description": ["testing document"]},
|
||||
{"dc:subject": ["test subject"]},
|
||||
{"dc:title": ["test document"]},
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user