From 4466145d002209ae0b2dfe1a82c67d265b73dd57 Mon Sep 17 00:00:00 2001 From: Tom Hunkapiller Date: Mon, 24 May 2021 12:00:04 +0300 Subject: [PATCH] add values checks to remaining file/table code --- src/file/border/border.ts | 10 +-- src/file/shading/shading.ts | 4 +- .../table/table-cell/table-cell-components.ts | 22 ++++++- .../table-float-properties.ts | 63 ++++++++++++------- .../table/table-properties/table-layout.ts | 12 ++-- .../table/table-properties/table-overlap.ts | 13 ++-- src/file/values.ts | 10 +++ 7 files changed, 93 insertions(+), 41 deletions(-) diff --git a/src/file/border/border.ts b/src/file/border/border.ts index b10d5a72e1..d494b48998 100644 --- a/src/file/border/border.ts +++ b/src/file/border/border.ts @@ -20,7 +20,7 @@ // // import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; -import { hexColorValue } from "../values"; +import { eighthPointMeasureValue, hexColorValue, pointMeasureValue } from "../values"; export interface IBorderOptions { readonly style: BorderStyle; @@ -30,12 +30,14 @@ export interface IBorderOptions { } export class BorderElement extends XmlComponent { - constructor(elementName: string, { color, ...options }: IBorderOptions) { + constructor(elementName: string, { color, size, space, style }: IBorderOptions) { super(elementName); this.root.push( new BordersAttributes({ - ...options, - color: color === undefined ? color : hexColorValue(color), + style, + color: color === undefined ? undefined : hexColorValue(color), + size: size === undefined ? undefined : eighthPointMeasureValue(size), + space: space === undefined ? undefined : pointMeasureValue(space), }), ); } diff --git a/src/file/shading/shading.ts b/src/file/shading/shading.ts index 89f1cb49e0..c356c2a0d7 100644 --- a/src/file/shading/shading.ts +++ b/src/file/shading/shading.ts @@ -39,8 +39,8 @@ export class Shading extends XmlComponent { super("w:shd"); this.root.push( new ShadingAttributes({ - fill: fill === undefined ? fill : hexColorValue(fill), - color: color === undefined ? color : hexColorValue(color), + fill: fill === undefined ? undefined : hexColorValue(fill), + color: color === undefined ? undefined : hexColorValue(color), val, }), ); diff --git a/src/file/table/table-cell/table-cell-components.ts b/src/file/table/table-cell/table-cell-components.ts index 9b31f3f29d..e676adab79 100644 --- a/src/file/table/table-cell/table-cell-components.ts +++ b/src/file/table/table-cell/table-cell-components.ts @@ -1,6 +1,22 @@ import { BorderElement, IBorderOptions } from "file/border"; +import { decimalNumber } from "file/values"; import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components"; +// +// +// +// +// +// +// +// +// +// +// +// +// +// + export interface ITableCellBorders { readonly top?: IBorderOptions; readonly start?: IBorderOptions; @@ -42,6 +58,10 @@ class GridSpanAttributes extends XmlAttributeComponent<{ readonly val: number }> protected readonly xmlKeys = { val: "w:val" }; } +// +// ... +// +// /** * GridSpan element. Should be used in a table cell. Pass the number of columns that this cell need to span. */ @@ -51,7 +71,7 @@ export class GridSpan extends XmlComponent { this.root.push( new GridSpanAttributes({ - val: value, + val: decimalNumber(value), }), ); } diff --git a/src/file/table/table-properties/table-float-properties.ts b/src/file/table/table-properties/table-float-properties.ts index 93bdd1a882..8e365fc93d 100644 --- a/src/file/table/table-properties/table-float-properties.ts +++ b/src/file/table/table-properties/table-float-properties.ts @@ -1,3 +1,4 @@ +import { signedTwipsMeasureValue, twipsMeasureValue } from "file/values"; import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { OverlapType, TableOverlap } from "./table-overlap"; @@ -43,7 +44,7 @@ export interface ITableFloatOptions { * If relativeHorizontalPosition is also specified, then the absoluteHorizontalPosition attribute is ignored. * If the attribute is omitted, the value is assumed to be zero. */ - readonly absoluteHorizontalPosition?: number; + readonly absoluteHorizontalPosition?: number | string; /** * Specifies a relative horizontal position for the table, relative to the horizontalAnchor attribute. @@ -74,7 +75,7 @@ export interface ITableFloatOptions { * If relativeVerticalPosition is also specified, then the absoluteVerticalPosition attribute is ignored. * If the attribute is omitted, the value is assumed to be zero. */ - readonly absoluteVerticalPosition?: number; + readonly absoluteVerticalPosition?: number | string; /** * Specifies a relative vertical position for the table, relative to the verticalAnchor attribute. @@ -92,28 +93,41 @@ export interface ITableFloatOptions { * Specifies the minimun distance to be maintained between the table and the top of text in the paragraph * below the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero. */ - readonly bottomFromText?: number; + readonly bottomFromText?: number | string; /** * Specifies the minimun distance to be maintained between the table and the bottom edge of text in the paragraph * above the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero. */ - readonly topFromText?: number; + readonly topFromText?: number | string; /** * Specifies the minimun distance to be maintained between the table and the edge of text in the paragraph * to the left of the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero. */ - readonly leftFromText?: number; + readonly leftFromText?: number | string; /** * Specifies the minimun distance to be maintained between the table and the edge of text in the paragraph * to the right of the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero. */ - readonly rightFromText?: number; + readonly rightFromText?: number | string; readonly overlap?: OverlapType; } +// +// +// +// +// +// +// +// +// +// +// +// + export class TableFloatOptionsAttributes extends XmlAttributeComponent { protected readonly xmlKeys = { horizontalAnchor: "w:horzAnchor", @@ -129,23 +143,30 @@ export class TableFloatOptionsAttributes extends XmlAttributeComponent -// -// -// -// -// -// -// -// -// -// -// - export class TableFloatProperties extends XmlComponent { - constructor(options: ITableFloatOptions) { + constructor({ + leftFromText, + rightFromText, + topFromText, + bottomFromText, + absoluteHorizontalPosition, + absoluteVerticalPosition, + ...options + }: ITableFloatOptions) { super("w:tblpPr"); - this.root.push(new TableFloatOptionsAttributes(options)); + this.root.push( + new TableFloatOptionsAttributes({ + leftFromText: leftFromText === undefined ? undefined : twipsMeasureValue(leftFromText), + rightFromText: rightFromText === undefined ? undefined : twipsMeasureValue(rightFromText), + topFromText: topFromText === undefined ? undefined : twipsMeasureValue(topFromText), + bottomFromText: bottomFromText === undefined ? undefined : twipsMeasureValue(bottomFromText), + absoluteHorizontalPosition: + absoluteHorizontalPosition === undefined ? undefined : signedTwipsMeasureValue(absoluteHorizontalPosition), + absoluteVerticalPosition: + absoluteVerticalPosition === undefined ? undefined : signedTwipsMeasureValue(absoluteVerticalPosition), + ...options, + }), + ); if (options.overlap) { this.root.push(new TableOverlap(options.overlap)); diff --git a/src/file/table/table-properties/table-layout.ts b/src/file/table/table-properties/table-layout.ts index 45fe1e6d5a..ddbec7e66a 100644 --- a/src/file/table/table-properties/table-layout.ts +++ b/src/file/table/table-properties/table-layout.ts @@ -1,5 +1,11 @@ import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; +// +// +// +// +// +// export enum TableLayoutType { AUTOFIT = "autofit", FIXED = "fixed", @@ -12,12 +18,6 @@ class TableLayoutAttributes extends XmlAttributeComponent<{ readonly type: Table // // // -// -// -// -// -// -// export class TableLayout extends XmlComponent { constructor(type: TableLayoutType) { super("w:tblLayout"); diff --git a/src/file/table/table-properties/table-overlap.ts b/src/file/table/table-properties/table-overlap.ts index fbc8fb65fb..102d2afa5d 100644 --- a/src/file/table/table-properties/table-overlap.ts +++ b/src/file/table/table-properties/table-overlap.ts @@ -1,5 +1,11 @@ import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; +// +// +// +// +// +// export enum OverlapType { NEVER = "never", OVERLAP = "overlap", @@ -8,13 +14,6 @@ export enum OverlapType { // // // -// -// -// -// -// -// - class TableOverlapAttributes extends XmlAttributeComponent<{ readonly val: OverlapType }> { protected readonly xmlKeys = { val: "w:val" }; } diff --git a/src/file/values.ts b/src/file/values.ts index c1b38f4b79..be05bb040c 100644 --- a/src/file/values.ts +++ b/src/file/values.ts @@ -136,3 +136,13 @@ export function percentageValue(val: string): string { export function measurementOrPercentValue(val: number | string): number | string { return typeof val === "number" ? decimalNumber(val) : percentageValue(val); } + +// +// +// +export const eighthPointMeasureValue = unsignedDecimalNumber; + +// +// +// +export const pointMeasureValue = unsignedDecimalNumber;