Optimize XML output by properly constructing objects to send to the xml library so that it can produce proper empty elements.
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
This commit is contained in:
@ -10,7 +10,7 @@ describe("ParagraphStyle", () => {
|
||||
const style = new ParagraphStyle("myStyleId");
|
||||
const tree = new Formatter().format(style);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:pPr": [] }, { "w:rPr": [] }],
|
||||
"w:style": { _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
});
|
||||
});
|
||||
|
||||
@ -20,9 +20,7 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:name": [{ _attr: { "w:val": "Style Name" } }] },
|
||||
{ "w:pPr": [] },
|
||||
{ "w:rPr": [] },
|
||||
{ "w:name": { _attr: { "w:val": "Style Name" } } },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -35,9 +33,7 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{ "w:rPr": [] },
|
||||
{ "w:basedOn": [{ _attr: { "w:val": "otherId" } }] },
|
||||
{ "w:basedOn": { _attr: { "w:val": "otherId" } } },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -46,12 +42,7 @@ describe("ParagraphStyle", () => {
|
||||
const style = new ParagraphStyle("myStyleId").quickFormat();
|
||||
const tree = new Formatter().format(style);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{ "w:rPr": [] },
|
||||
{ "w:qFormat": [] },
|
||||
],
|
||||
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:qFormat": null }],
|
||||
});
|
||||
});
|
||||
|
||||
@ -61,9 +52,7 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{ "w:rPr": [] },
|
||||
{ "w:next": [{ _attr: { "w:val": "otherId" } }] },
|
||||
{ "w:next": { _attr: { "w:val": "otherId" } } },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -77,9 +66,8 @@ describe("ParagraphStyle", () => {
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{
|
||||
"w:pPr": [{ "w:ind": [{ _attr: { "w:left": 720 } }] }],
|
||||
"w:pPr": [{ "w:ind": { _attr: { "w:left": 720 } } }],
|
||||
},
|
||||
{ "w:rPr": [] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -91,9 +79,8 @@ describe("ParagraphStyle", () => {
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{
|
||||
"w:pPr": [{ "w:spacing": [{ _attr: { "w:before": 50, "w:after": 150 } }] }],
|
||||
"w:pPr": [{ "w:spacing": { _attr: { "w:before": 50, "w:after": 150 } } }],
|
||||
},
|
||||
{ "w:rPr": [] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -105,9 +92,8 @@ describe("ParagraphStyle", () => {
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{
|
||||
"w:pPr": [{ "w:jc": [{ _attr: { "w:val": "center" } }] }],
|
||||
"w:pPr": [{ "w:jc": { _attr: { "w:val": "center" } } }],
|
||||
},
|
||||
{ "w:rPr": [] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -118,9 +104,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:spacing": [{ _attr: { "w:val": 24 } }] }],
|
||||
"w:rPr": [{ "w:spacing": { _attr: { "w:val": 24 } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -133,9 +118,8 @@ describe("ParagraphStyle", () => {
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{
|
||||
"w:pPr": [{ "w:jc": [{ _attr: { "w:val": "left" } }] }],
|
||||
"w:pPr": [{ "w:jc": { _attr: { "w:val": "left" } } }],
|
||||
},
|
||||
{ "w:rPr": [] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -147,9 +131,8 @@ describe("ParagraphStyle", () => {
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{
|
||||
"w:pPr": [{ "w:jc": [{ _attr: { "w:val": "right" } }] }],
|
||||
"w:pPr": [{ "w:jc": { _attr: { "w:val": "right" } } }],
|
||||
},
|
||||
{ "w:rPr": [] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -161,9 +144,8 @@ describe("ParagraphStyle", () => {
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{
|
||||
"w:pPr": [{ "w:jc": [{ _attr: { "w:val": "both" } }] }],
|
||||
"w:pPr": [{ "w:jc": { _attr: { "w:val": "both" } } }],
|
||||
},
|
||||
{ "w:rPr": [] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -179,22 +161,19 @@ describe("ParagraphStyle", () => {
|
||||
{
|
||||
"w:pBdr": [
|
||||
{
|
||||
"w:bottom": [
|
||||
{
|
||||
_attr: {
|
||||
"w:color": "auto",
|
||||
"w:space": "1",
|
||||
"w:val": "single",
|
||||
"w:sz": "6",
|
||||
},
|
||||
"w:bottom": {
|
||||
_attr: {
|
||||
"w:color": "auto",
|
||||
"w:space": "1",
|
||||
"w:val": "single",
|
||||
"w:sz": "6",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{ "w:rPr": [] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -208,11 +187,10 @@ describe("ParagraphStyle", () => {
|
||||
{
|
||||
"w:pPr": [
|
||||
{
|
||||
"w:tabs": [{ "w:tab": [{ _attr: { "w:val": "left", "w:pos": 1200 } }] }],
|
||||
"w:tabs": [{ "w:tab": { _attr: { "w:val": "left", "w:pos": 1200 } } }],
|
||||
},
|
||||
],
|
||||
},
|
||||
{ "w:rPr": [] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -226,11 +204,10 @@ describe("ParagraphStyle", () => {
|
||||
{
|
||||
"w:pPr": [
|
||||
{
|
||||
"w:tabs": [{ "w:tab": [{ _attr: { "w:val": "right", "w:pos": 9026 } }] }],
|
||||
"w:tabs": [{ "w:tab": { _attr: { "w:val": "right", "w:pos": 9026 } } }],
|
||||
},
|
||||
],
|
||||
},
|
||||
{ "w:rPr": [] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -239,11 +216,7 @@ describe("ParagraphStyle", () => {
|
||||
const style = new ParagraphStyle("myStyleId").keepLines();
|
||||
const tree = new Formatter().format(style);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [{ "w:keepLines": [] }] },
|
||||
{ "w:rPr": [] },
|
||||
],
|
||||
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:pPr": [{ "w:keepLines": null }] }],
|
||||
});
|
||||
});
|
||||
|
||||
@ -251,11 +224,7 @@ describe("ParagraphStyle", () => {
|
||||
const style = new ParagraphStyle("myStyleId").keepNext();
|
||||
const tree = new Formatter().format(style);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [{ "w:keepNext": [] }] },
|
||||
{ "w:rPr": [] },
|
||||
],
|
||||
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:pPr": [{ "w:keepNext": null }] }],
|
||||
});
|
||||
});
|
||||
|
||||
@ -265,8 +234,7 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [{ "w:outlineLvl": [{ _attr: { "w:val": "1" } }] }] },
|
||||
{ "w:rPr": [] },
|
||||
{ "w:pPr": [{ "w:outlineLvl": { _attr: { "w:val": "1" } } }] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -279,9 +247,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:sz": [{ _attr: { "w:val": 24 } }] }, { "w:szCs": [{ _attr: { "w:val": 24 } }] }],
|
||||
"w:rPr": [{ "w:sz": { _attr: { "w:val": 24 } } }, { "w:szCs": { _attr: { "w:val": 24 } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -293,9 +260,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:smallCaps": [{ _attr: { "w:val": true } }] }],
|
||||
"w:rPr": [{ "w:smallCaps": { _attr: { "w:val": true } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -307,9 +273,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:caps": [{ _attr: { "w:val": true } }] }],
|
||||
"w:rPr": [{ "w:caps": { _attr: { "w:val": true } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -321,9 +286,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:strike": [{ _attr: { "w:val": true } }] }],
|
||||
"w:rPr": [{ "w:strike": { _attr: { "w:val": true } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -335,9 +299,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:dstrike": [{ _attr: { "w:val": true } }] }],
|
||||
"w:rPr": [{ "w:dstrike": { _attr: { "w:val": true } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -349,9 +312,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:vertAlign": [{ _attr: { "w:val": "subscript" } }] }],
|
||||
"w:rPr": [{ "w:vertAlign": { _attr: { "w:val": "subscript" } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -363,9 +325,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:vertAlign": [{ _attr: { "w:val": "superscript" } }] }],
|
||||
"w:rPr": [{ "w:vertAlign": { _attr: { "w:val": "superscript" } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -377,10 +338,9 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [
|
||||
{ "w:rFonts": [{ _attr: { "w:ascii": "Times", "w:cs": "Times", "w:eastAsia": "Times", "w:hAnsi": "Times" } }] },
|
||||
{ "w:rFonts": { _attr: { "w:ascii": "Times", "w:cs": "Times", "w:eastAsia": "Times", "w:hAnsi": "Times" } } },
|
||||
],
|
||||
},
|
||||
],
|
||||
@ -393,9 +353,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:b": [{ _attr: { "w:val": true } }] }],
|
||||
"w:rPr": [{ "w:b": { _attr: { "w:val": true } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -407,9 +366,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:i": [{ _attr: { "w:val": true } }] }],
|
||||
"w:rPr": [{ "w:i": { _attr: { "w:val": true } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -422,9 +380,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:u": [{ _attr: { "w:val": "single" } }] }],
|
||||
"w:rPr": [{ "w:u": { _attr: { "w:val": "single" } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -436,9 +393,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:u": [{ _attr: { "w:val": "double" } }] }],
|
||||
"w:rPr": [{ "w:u": { _attr: { "w:val": "double" } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -450,9 +406,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:u": [{ _attr: { "w:val": "double", "w:color": "005599" } }] }],
|
||||
"w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "005599" } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -465,9 +420,8 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:rPr": [{ "w:color": [{ _attr: { "w:val": "123456" } }] }],
|
||||
"w:rPr": [{ "w:color": { _attr: { "w:val": "123456" } } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -477,12 +431,7 @@ describe("ParagraphStyle", () => {
|
||||
const style = new ParagraphStyle("myStyleId").link("MyLink");
|
||||
const tree = new Formatter().format(style);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{ "w:rPr": [] },
|
||||
{ "w:link": [{ _attr: { "w:val": "MyLink" } }] },
|
||||
],
|
||||
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:link": { _attr: { "w:val": "MyLink" } } }],
|
||||
});
|
||||
});
|
||||
|
||||
@ -490,12 +439,7 @@ describe("ParagraphStyle", () => {
|
||||
const style = new ParagraphStyle("myStyleId").semiHidden();
|
||||
const tree = new Formatter().format(style);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{ "w:rPr": [] },
|
||||
{ "w:semiHidden": [] },
|
||||
],
|
||||
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:semiHidden": null }],
|
||||
});
|
||||
});
|
||||
|
||||
@ -505,16 +449,12 @@ describe("ParagraphStyle", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{ "w:rPr": [] },
|
||||
{
|
||||
"w:uiPriority": [
|
||||
{
|
||||
_attr: {
|
||||
"w:val": "99",
|
||||
},
|
||||
"w:uiPriority": {
|
||||
_attr: {
|
||||
"w:val": "99",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -524,12 +464,7 @@ describe("ParagraphStyle", () => {
|
||||
const style = new ParagraphStyle("myStyleId").unhideWhenUsed();
|
||||
const tree = new Formatter().format(style);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
||||
{ "w:pPr": [] },
|
||||
{ "w:rPr": [] },
|
||||
{ "w:unhideWhenUsed": [] },
|
||||
],
|
||||
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:unhideWhenUsed": null }],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user