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:
@ -11,68 +11,58 @@ import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType }
|
||||
const DEFAULT_TABLE_PROPERTIES = {
|
||||
"w:tblCellMar": [
|
||||
{
|
||||
"w:bottom": [
|
||||
{
|
||||
_attr: {
|
||||
"w:type": "auto",
|
||||
"w:w": 0,
|
||||
},
|
||||
"w:bottom": {
|
||||
_attr: {
|
||||
"w:type": "auto",
|
||||
"w:w": 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
"w:top": [
|
||||
{
|
||||
_attr: {
|
||||
"w:type": "auto",
|
||||
"w:w": 0,
|
||||
},
|
||||
"w:top": {
|
||||
_attr: {
|
||||
"w:type": "auto",
|
||||
"w:w": 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
"w:left": [
|
||||
{
|
||||
_attr: {
|
||||
"w:type": "auto",
|
||||
"w:w": 0,
|
||||
},
|
||||
"w:left": {
|
||||
_attr: {
|
||||
"w:type": "auto",
|
||||
"w:w": 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
"w:right": [
|
||||
{
|
||||
_attr: {
|
||||
"w:type": "auto",
|
||||
"w:w": 0,
|
||||
},
|
||||
"w:right": {
|
||||
_attr: {
|
||||
"w:type": "auto",
|
||||
"w:w": 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const BORDERS = {
|
||||
"w:tblBorders": [
|
||||
{ "w:top": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
|
||||
{ "w:left": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
|
||||
{ "w:bottom": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
|
||||
{ "w:right": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
|
||||
{ "w:insideH": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
|
||||
{ "w:insideV": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
|
||||
{ "w:top": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
|
||||
{ "w:left": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
|
||||
{ "w:bottom": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
|
||||
{ "w:right": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
|
||||
{ "w:insideH": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
|
||||
{ "w:insideV": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
|
||||
],
|
||||
};
|
||||
|
||||
const WIDTHS = {
|
||||
"w:tblW": [
|
||||
{
|
||||
_attr: {
|
||||
"w:type": "auto",
|
||||
"w:w": 100,
|
||||
},
|
||||
"w:tblW": {
|
||||
_attr: {
|
||||
"w:type": "auto",
|
||||
"w:w": 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
// const f = {
|
||||
@ -81,45 +71,43 @@ const WIDTHS = {
|
||||
// "w:tblPr": [
|
||||
// {
|
||||
// "w:tblCellMar": [
|
||||
// { "w:bottom": [{ _attr: { "w:type": "auto", "w:w": 0 } }] },
|
||||
// { "w:top": [{ _attr: { "w:type": "auto", "w:w": 0 } }] },
|
||||
// { "w:left": [{ _attr: { "w:type": "auto", "w:w": 0 } }] },
|
||||
// { "w:right": [{ _attr: { "w:type": "auto", "w:w": 0 } }] },
|
||||
// { "w:bottom": { _attr: { "w:type": "auto", "w:w": 0 } } },
|
||||
// { "w:top": { _attr: { "w:type": "auto", "w:w": 0 } } },
|
||||
// { "w:left": { _attr: { "w:type": "auto", "w:w": 0 } } },
|
||||
// { "w:right": { _attr: { "w:type": "auto", "w:w": 0 } } },
|
||||
// ],
|
||||
// },
|
||||
// {
|
||||
// "w:tblBorders": [
|
||||
// { "w:top": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
|
||||
// { "w:left": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
|
||||
// { "w:bottom": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
|
||||
// { "w:right": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
|
||||
// { "w:insideH": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
|
||||
// { "w:insideV": [{ _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } }] },
|
||||
// { "w:top": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
|
||||
// { "w:left": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
|
||||
// { "w:bottom": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
|
||||
// { "w:right": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
|
||||
// { "w:insideH": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
|
||||
// { "w:insideV": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
|
||||
// ],
|
||||
// },
|
||||
// { "w:tblW": [{ _attr: { "w:type": "auto", "w:w": 100 } }] },
|
||||
// { "w:tblW": { _attr: { "w:type": "auto", "w:w": 100 } } },
|
||||
// {
|
||||
// "w:tblpPr": [
|
||||
// {
|
||||
// _attr: {
|
||||
// "w:horzAnchor": "margin",
|
||||
// "w:vertAnchor": "page",
|
||||
// "w:tblpX": 10,
|
||||
// "w:tblpXSpec": "center",
|
||||
// "w:tblpY": 20,
|
||||
// "w:tblpYSpec": "bottom",
|
||||
// "w:bottomFromText": 30,
|
||||
// "w:topFromText": 40,
|
||||
// "w:leftFromText": 50,
|
||||
// "w:rightFromText": 60,
|
||||
// },
|
||||
// "w:tblpPr": {
|
||||
// _attr: {
|
||||
// "w:horzAnchor": "margin",
|
||||
// "w:vertAnchor": "page",
|
||||
// "w:tblpX": 10,
|
||||
// "w:tblpXSpec": "center",
|
||||
// "w:tblpY": 20,
|
||||
// "w:tblpYSpec": "bottom",
|
||||
// "w:bottomFromText": 30,
|
||||
// "w:topFromText": 40,
|
||||
// "w:leftFromText": 50,
|
||||
// "w:rightFromText": 60,
|
||||
// },
|
||||
// ],
|
||||
// },
|
||||
// },
|
||||
// ],
|
||||
// },
|
||||
// { "w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }] },
|
||||
// { "w:tr": [{ "w:trPr": [] }, { "w:tc": [{ "w:tcPr": [] }, { "w:p": [{ "w:pPr": [] }] }] }] },
|
||||
// { "w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }] },
|
||||
// { "w:tr": [{ "w:tc": [{ "w:p": null }] }] },
|
||||
// ],
|
||||
// };
|
||||
|
||||
@ -131,16 +119,16 @@ describe("Table", () => {
|
||||
columns: 2,
|
||||
});
|
||||
const tree = new Formatter().format(table);
|
||||
const cell = { "w:tc": [{ "w:tcPr": [] }, { "w:p": [{ "w:pPr": [] }] }] };
|
||||
const cell = { "w:tc": [{ "w:p": null }] };
|
||||
expect(tree).to.deep.equal({
|
||||
"w:tbl": [
|
||||
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] },
|
||||
{
|
||||
"w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }],
|
||||
"w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }, { "w:gridCol": { _attr: { "w:w": 100 } } }],
|
||||
},
|
||||
{ "w:tr": [{ "w:trPr": [] }, cell, cell] },
|
||||
{ "w:tr": [{ "w:trPr": [] }, cell, cell] },
|
||||
{ "w:tr": [{ "w:trPr": [] }, cell, cell] },
|
||||
{ "w:tr": [cell, cell] },
|
||||
{ "w:tr": [cell, cell] },
|
||||
{ "w:tr": [cell, cell] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -172,9 +160,8 @@ describe("Table", () => {
|
||||
const tree = new Formatter().format(table);
|
||||
const cell = (c) => ({
|
||||
"w:tc": [
|
||||
{ "w:tcPr": [] },
|
||||
{
|
||||
"w:p": [{ "w:pPr": [] }, { "w:r": [{ "w:rPr": [] }, { "w:t": [{ _attr: { "xml:space": "preserve" } }, c] }] }],
|
||||
"w:p": [{ "w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, c] }] }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -182,10 +169,10 @@ describe("Table", () => {
|
||||
"w:tbl": [
|
||||
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] },
|
||||
{
|
||||
"w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }],
|
||||
"w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }, { "w:gridCol": { _attr: { "w:w": 100 } } }],
|
||||
},
|
||||
{ "w:tr": [{ "w:trPr": [] }, cell("A1"), cell("B1")] },
|
||||
{ "w:tr": [{ "w:trPr": [] }, cell("A2"), cell("B2")] },
|
||||
{ "w:tr": [cell("A1"), cell("B1")] },
|
||||
{ "w:tr": [cell("A2"), cell("B2")] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -222,9 +209,8 @@ describe("Table", () => {
|
||||
const tree = new Formatter().format(table);
|
||||
const cell = (c) => ({
|
||||
"w:tc": [
|
||||
{ "w:tcPr": [] },
|
||||
{
|
||||
"w:p": [{ "w:pPr": [] }, { "w:r": [{ "w:rPr": [] }, { "w:t": [{ _attr: { "xml:space": "preserve" } }, c] }] }],
|
||||
"w:p": [{ "w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, c] }] }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -232,10 +218,10 @@ describe("Table", () => {
|
||||
"w:tbl": [
|
||||
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] },
|
||||
{
|
||||
"w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 100 } }] }, { "w:gridCol": [{ _attr: { "w:w": 100 } }] }],
|
||||
"w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }, { "w:gridCol": { _attr: { "w:w": 100 } } }],
|
||||
},
|
||||
{ "w:tr": [{ "w:trPr": [] }, cell("A1"), cell("B1")] },
|
||||
{ "w:tr": [{ "w:trPr": [] }, cell("A2"), cell("B2")] },
|
||||
{ "w:tr": [cell("A1"), cell("B1")] },
|
||||
{ "w:tr": [cell("A2"), cell("B2")] },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -250,7 +236,7 @@ describe("Table", () => {
|
||||
// .which.is.an("array")
|
||||
// .with.has.length.at.least(1);
|
||||
// expect(tree["w:tbl"][0]).to.deep.equal({
|
||||
// "w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": [{ _attr: { "w:type": "pct", "w:w": "1000%" } }] }],
|
||||
// "w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": { _attr: { "w:type": "pct", "w:w": "1000%" } } }],
|
||||
// });
|
||||
// });
|
||||
|
||||
@ -259,7 +245,7 @@ describe("Table", () => {
|
||||
// const tree = new Formatter().format(table);
|
||||
|
||||
// expect(tree["w:tbl"][0]).to.deep.equal({
|
||||
// "w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": [{ _attr: { "w:type": "auto", "w:w": 1000 } }] }],
|
||||
// "w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": { _attr: { "w:type": "auto", "w:w": 1000 } } }],
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
@ -276,7 +262,7 @@ describe("Table", () => {
|
||||
.which.is.an("array")
|
||||
.with.has.length.at.least(1);
|
||||
expect(tree["w:tbl"][0]).to.deep.equal({
|
||||
"w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS, { "w:tblLayout": [{ _attr: { "w:type": "fixed" } }] }],
|
||||
"w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS, { "w:tblLayout": { _attr: { "w:type": "fixed" } } }],
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -298,7 +284,7 @@ describe("Table", () => {
|
||||
.to.be.an("array")
|
||||
.which.has.length.at.least(1);
|
||||
expect(row["w:tr"].find((x) => x["w:tc"])).to.deep.equal({
|
||||
"w:tc": [{ "w:tcPr": [] }, { "w:p": [{ "w:pPr": [] }] }],
|
||||
"w:tc": [{ "w:p": null }],
|
||||
});
|
||||
});
|
||||
|
||||
@ -325,7 +311,7 @@ describe("Table", () => {
|
||||
const cell = row["w:tr"].find((x) => x["w:tc"]);
|
||||
expect(cell).not.to.be.undefined;
|
||||
expect(cell["w:tc"][cell["w:tc"].length - 1]).to.deep.equal({
|
||||
"w:p": [{ "w:pPr": [] }],
|
||||
"w:p": null,
|
||||
});
|
||||
});
|
||||
|
||||
@ -346,12 +332,8 @@ describe("Table", () => {
|
||||
.which.has.length.at.least(1);
|
||||
expect(row["w:tr"].find((x) => x["w:tc"])).to.deep.equal({
|
||||
"w:tc": [
|
||||
{ "w:tcPr": [] },
|
||||
{
|
||||
"w:p": [
|
||||
{ "w:pPr": [] },
|
||||
{ "w:r": [{ "w:rPr": [] }, { "w:t": [{ _attr: { "xml:space": "preserve" } }, "Hello"] }] },
|
||||
],
|
||||
"w:p": [{ "w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "Hello"] }] }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -377,12 +359,10 @@ describe("Table", () => {
|
||||
.which.has.length.at.least(1);
|
||||
expect(row["w:tr"].find((x) => x["w:tc"])).to.deep.equal({
|
||||
"w:tc": [
|
||||
{ "w:tcPr": [] },
|
||||
{
|
||||
"w:p": [
|
||||
{ "w:pPr": [] },
|
||||
{
|
||||
"w:r": [{ "w:rPr": [] }, { "w:t": [{ _attr: { "xml:space": "preserve" } }, "Test paragraph"] }],
|
||||
"w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "Test paragraph"] }],
|
||||
},
|
||||
],
|
||||
},
|
||||
@ -421,22 +401,20 @@ describe("Table", () => {
|
||||
BORDERS,
|
||||
WIDTHS,
|
||||
{
|
||||
"w:tblpPr": [
|
||||
{
|
||||
_attr: {
|
||||
"w:horzAnchor": "margin",
|
||||
"w:vertAnchor": "page",
|
||||
"w:tblpX": 10,
|
||||
"w:tblpXSpec": "center",
|
||||
"w:tblpY": 20,
|
||||
"w:tblpYSpec": "bottom",
|
||||
"w:bottomFromText": 30,
|
||||
"w:topFromText": 40,
|
||||
"w:leftFromText": 50,
|
||||
"w:rightFromText": 60,
|
||||
},
|
||||
"w:tblpPr": {
|
||||
_attr: {
|
||||
"w:horzAnchor": "margin",
|
||||
"w:vertAnchor": "page",
|
||||
"w:tblpX": 10,
|
||||
"w:tblpXSpec": "center",
|
||||
"w:tblpY": 20,
|
||||
"w:tblpYSpec": "bottom",
|
||||
"w:bottomFromText": 30,
|
||||
"w:topFromText": 40,
|
||||
"w:leftFromText": 50,
|
||||
"w:rightFromText": 60,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
Reference in New Issue
Block a user