Fix ordering of elements in table, table-cell, table-properties, table-cell-properties, table-cell-margin

This commit is contained in:
Tom Hunkapiller
2021-05-20 01:03:09 +03:00
parent 45130bed0b
commit d0a675fde6
9 changed files with 94 additions and 88 deletions

View File

@ -19,6 +19,14 @@ describe("TableCellMargin", () => {
},
},
},
{
"w:start": {
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
},
{
"w:bottom": {
_attr: {
@ -35,14 +43,6 @@ describe("TableCellMargin", () => {
},
},
},
{
"w:start": {
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
},
],
});
});
@ -65,6 +65,14 @@ describe("TableCellMargin", () => {
},
},
},
{
"w:start": {
_attr: {
"w:type": "dxa",
"w:w": 5,
},
},
},
{
"w:bottom": {
_attr: {
@ -81,14 +89,6 @@ describe("TableCellMargin", () => {
},
},
},
{
"w:start": {
_attr: {
"w:type": "dxa",
"w:w": 5,
},
},
},
],
});
});

View File

@ -14,8 +14,8 @@ export class TableCellMargin extends XmlComponent {
constructor({ top = 0, left = 0, right = 0, bottom = 0 }: ITableCellMarginOptions) {
super("w:tcMar");
this.root.push(new TopCellMargin(top));
this.root.push(new LeftCellMargin(left));
this.root.push(new BottomCellMargin(bottom));
this.root.push(new RightCellMargin(right));
this.root.push(new LeftCellMargin(left));
}
}

View File

@ -90,6 +90,14 @@ describe("TableCellProperties", () => {
},
},
},
{
"w:start": {
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
},
{
"w:bottom": {
_attr: {
@ -106,14 +114,6 @@ describe("TableCellProperties", () => {
},
},
},
{
"w:start": {
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
},
],
},
],

View File

@ -21,7 +21,6 @@ export class TableCellProperties extends IgnoreIfEmptyXmlComponent {
constructor() {
super("w:tcPr");
this.cellBorder = new TableCellBorders();
this.root.push(this.cellBorder);
}
public get Borders(): TableCellBorders {
@ -69,4 +68,10 @@ export class TableCellProperties extends IgnoreIfEmptyXmlComponent {
return this;
}
public addBorders(): TableCellProperties {
this.root.push(this.cellBorder);
return this;
}
}

View File

@ -354,6 +354,14 @@ describe("TableCell", () => {
},
},
},
{
"w:start": {
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
},
{
"w:bottom": {
_attr: {
@ -370,14 +378,6 @@ describe("TableCell", () => {
},
},
},
{
"w:start": {
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
},
],
},
],

View File

@ -57,12 +57,12 @@ export class TableCell extends XmlComponent {
this.root.push(child);
}
if (options.verticalAlign) {
properties.setVerticalAlign(options.verticalAlign);
if (options.width) {
properties.setWidth(options.width.size, options.width.type);
}
if (options.textDirection) {
properties.setTextDirection(options.textDirection);
if (options.columnSpan) {
properties.addGridSpan(options.columnSpan);
}
if (options.verticalMerge) {
@ -72,23 +72,8 @@ export class TableCell extends XmlComponent {
properties.addVerticalMerge(VerticalMergeType.RESTART);
}
if (options.margins) {
properties.addMargins(options.margins);
}
if (options.shading) {
properties.setShading(options.shading);
}
if (options.columnSpan) {
properties.addGridSpan(options.columnSpan);
}
if (options.width) {
properties.setWidth(options.width.size, options.width.type);
}
if (options.borders) {
properties.addBorders();
if (options.borders.top) {
properties.Borders.addTopBorder(options.borders.top.style, options.borders.top.size, options.borders.top.color);
}
@ -102,6 +87,22 @@ export class TableCell extends XmlComponent {
properties.Borders.addRightBorder(options.borders.right.style, options.borders.right.size, options.borders.right.color);
}
}
if (options.shading) {
properties.setShading(options.shading);
}
if (options.margins) {
properties.addMargins(options.margins);
}
if (options.textDirection) {
properties.setTextDirection(options.textDirection);
}
if (options.verticalAlign) {
properties.setVerticalAlign(options.verticalAlign);
}
}
public prepForXml(context: IContext): IXmlableObject | undefined {

View File

@ -35,10 +35,6 @@ export class TableCellMargin extends IgnoreIfEmptyXmlComponent {
constructor(options: ITableCellMarginOptions) {
super("w:tblCellMar");
if (options.bottom) {
this.root.push(new BaseTableCellMargin("w:bottom", options.bottom));
}
if (options.top) {
this.root.push(new BaseTableCellMargin("w:top", options.top));
}
@ -47,6 +43,10 @@ export class TableCellMargin extends IgnoreIfEmptyXmlComponent {
this.root.push(new BaseTableCellMargin("w:left", options.left));
}
if (options.bottom) {
this.root.push(new BaseTableCellMargin("w:bottom", options.bottom));
}
if (options.right) {
this.root.push(new BaseTableCellMargin("w:right", options.right));
}

View File

@ -35,34 +35,34 @@ export class TableProperties extends IgnoreIfEmptyXmlComponent {
this.root.push(new TableStyle(options.style));
}
this.root.push(new TableCellMargin(options.cellMargin || {}));
if (options.float) {
this.root.push(new TableFloatProperties(options.float));
}
if (options.borders) {
this.root.push(new TableBorders(options.borders));
if (options.visuallyRightToLeft) {
this.root.push(new VisuallyRightToLeft());
}
if (options.width) {
this.root.push(new PreferredTableWidth(options.width.type, options.width.size));
}
if (options.float) {
this.root.push(new TableFloatProperties(options.float));
}
if (options.layout) {
this.root.push(new TableLayout(options.layout));
}
if (options.alignment) {
this.root.push(new Alignment(options.alignment));
}
if (options.borders) {
this.root.push(new TableBorders(options.borders));
}
if (options.shading) {
this.root.push(new TableShading(options.shading));
}
if (options.visuallyRightToLeft) {
this.root.push(new VisuallyRightToLeft());
if (options.layout) {
this.root.push(new TableLayout(options.layout));
}
this.root.push(new TableCellMargin(options.cellMargin || {}));
}
}

View File

@ -14,14 +14,6 @@ import { TableRow } from "./table-row";
const DEFAULT_TABLE_PROPERTIES = {
"w:tblCellMar": [
{
"w:bottom": {
_attr: {
"w:type": "auto",
"w:w": 0,
},
},
},
{
"w:top": {
_attr: {
@ -38,6 +30,14 @@ const DEFAULT_TABLE_PROPERTIES = {
},
},
},
{
"w:bottom": {
_attr: {
"w:type": "auto",
"w:w": 0,
},
},
},
{
"w:right": {
_attr: {
@ -177,7 +177,7 @@ describe("Table", () => {
};
expect(tree).to.deep.equal({
"w:tbl": [
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] },
{ "w:tblPr": [WIDTHS, BORDERS, DEFAULT_TABLE_PROPERTIES] },
{
"w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }, { "w:gridCol": { _attr: { "w:w": 100 } } }],
},
@ -223,7 +223,7 @@ describe("Table", () => {
const cellP = { "w:p": [{ "w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "hello"] }] }] };
expect(tree).to.deep.equal({
"w:tbl": [
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] },
{ "w:tblPr": [WIDTHS, BORDERS, DEFAULT_TABLE_PROPERTIES] },
{
"w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }, { "w:gridCol": { _attr: { "w:w": 100 } } }],
},
@ -270,7 +270,7 @@ describe("Table", () => {
const tree = new Formatter().format(table);
expect(tree).to.have.property("w:tbl").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": [WIDTHS, BORDERS, { "w:tblLayout": { _attr: { "w:type": "fixed" } } }, DEFAULT_TABLE_PROPERTIES],
});
});
@ -290,7 +290,7 @@ describe("Table", () => {
const tree = new Formatter().format(table);
expect(tree).to.have.property("w:tbl").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:jc": { _attr: { "w:val": "center" } } }],
"w:tblPr": [WIDTHS, { "w:jc": { _attr: { "w:val": "center" } } }, BORDERS, DEFAULT_TABLE_PROPERTIES],
});
});
@ -315,8 +315,6 @@ describe("Table", () => {
expect(tree).to.have.property("w:tbl").which.is.an("array").with.has.length.at.least(1);
expect(tree["w:tbl"][0]).to.deep.equal({
"w:tblPr": [
DEFAULT_TABLE_PROPERTIES,
BORDERS,
{
"w:tblW": {
_attr: {
@ -325,7 +323,9 @@ describe("Table", () => {
},
},
},
BORDERS,
{ "w:tblLayout": { _attr: { "w:type": "fixed" } } },
DEFAULT_TABLE_PROPERTIES,
],
});
});
@ -477,9 +477,6 @@ describe("Table", () => {
expect(tree).to.have.property("w:tbl").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:tblpPr": {
_attr: {
@ -496,6 +493,9 @@ describe("Table", () => {
},
},
},
WIDTHS,
BORDERS,
DEFAULT_TABLE_PROPERTIES,
],
});
});