Rework the way attributes are stored in ImportedXmlComponent to match elsewhere (required allowing for a null xmlKeys in the XmlAttributeComponent interface). Rework the way paragraphs get added to the end of table cells if needed. The goal in both reworks is to not mess around with the objects output from `prepForXml` if we can avoid it. Made the output of RunProperties, ParagraphProperties, TableCellProperties, TableRowProperties, and TableProperties all optional based on whether they contain any attributes or children. Changed code in PageBorders, TableCellMargin, and TableCellBorders that implemented this same thing by overriding `prepForXml` so that it uses the new XmlComponent subclass instead. Removed commented out code that attempted to fix-up XML output and make proper empty elements. Fixed all affected tests. Turn off `no-null-keyword` in the linter as we need to use null to signal to the `xml` library to create an empty element with no attributes (`undefined` will not work in its place). Fixes #306
79 lines
2.7 KiB
TypeScript
79 lines
2.7 KiB
TypeScript
import { assert } from "chai";
|
|
|
|
import { Formatter } from "../export/formatter";
|
|
import * as file from "../file";
|
|
import { CoreProperties } from "../file/core-properties";
|
|
import { Attributes } from "../file/xml-components";
|
|
import { Utility } from "../tests/utility";
|
|
|
|
describe("Formatter", () => {
|
|
let formatter: Formatter;
|
|
|
|
beforeEach(() => {
|
|
formatter = new Formatter();
|
|
});
|
|
|
|
describe("#format()", () => {
|
|
it("should format simple paragraph", () => {
|
|
const paragraph = new file.Paragraph();
|
|
const newJson = formatter.format(paragraph);
|
|
assert.isDefined(newJson["w:p"]);
|
|
});
|
|
|
|
it("should remove xmlKeys", () => {
|
|
const paragraph = new file.Paragraph();
|
|
const newJson = formatter.format(paragraph);
|
|
const stringifiedJson = JSON.stringify(newJson);
|
|
assert(stringifiedJson.indexOf("xmlKeys") < 0);
|
|
});
|
|
|
|
it("should format simple paragraph with bold text", () => {
|
|
const paragraph = new file.Paragraph();
|
|
paragraph.addRun(new file.TextRun("test").bold());
|
|
const newJson = formatter.format(paragraph);
|
|
assert.isDefined(newJson["w:p"][0]["w:r"][0]["w:rPr"][0]["w:b"]._attr["w:val"]);
|
|
});
|
|
|
|
it("should format attributes (rsidSect)", () => {
|
|
const attributes = new Attributes({
|
|
rsidSect: "test2",
|
|
});
|
|
let newJson = formatter.format(attributes);
|
|
newJson = Utility.jsonify(newJson);
|
|
if (newJson._attr === undefined) {
|
|
assert.fail();
|
|
return;
|
|
}
|
|
assert.isDefined(newJson._attr["w:rsidSect"]);
|
|
});
|
|
|
|
it("should format attributes (val)", () => {
|
|
const attributes = new Attributes({
|
|
val: "test",
|
|
});
|
|
let newJson = formatter.format(attributes);
|
|
newJson = Utility.jsonify(newJson);
|
|
if (newJson._attr === undefined) {
|
|
assert.fail();
|
|
return;
|
|
}
|
|
assert.isDefined(newJson._attr["w:val"]);
|
|
});
|
|
|
|
it("should should change 'p' tag into 'w:p' tag", () => {
|
|
const paragraph = new file.Paragraph();
|
|
const newJson = formatter.format(paragraph);
|
|
assert.isDefined(newJson["w:p"]);
|
|
});
|
|
|
|
it("should format Properties object correctly", () => {
|
|
const properties = new CoreProperties({
|
|
title: "test document",
|
|
creator: "Dolan",
|
|
});
|
|
const newJson = formatter.format(properties);
|
|
assert.isDefined(newJson["cp:coreProperties"]);
|
|
});
|
|
});
|
|
});
|