add values checks to remaining file/table code

This commit is contained in:
Tom Hunkapiller
2021-05-24 12:00:04 +03:00
parent ce2a0fb864
commit 4466145d00
7 changed files with 93 additions and 41 deletions

View File

@ -20,7 +20,7 @@
// <xsd:attribute name="frame" type="s:ST_OnOff" use="optional"/>
// </xsd:complexType>
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),
}),
);
}

View File

@ -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,
}),
);

View File

@ -1,6 +1,22 @@
import { BorderElement, IBorderOptions } from "file/border";
import { decimalNumber } from "file/values";
import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components";
// <xsd:complexType name="CT_TcBorders">
// <xsd:sequence>
// <xsd:element name="top" type="CT_Border" minOccurs="0"/>
// <xsd:element name="start" type="CT_Border" minOccurs="0"/>
// <xsd:element name="left" type="CT_Border" minOccurs="0"/>
// <xsd:element name="bottom" type="CT_Border" minOccurs="0"/>
// <xsd:element name="end" type="CT_Border" minOccurs="0"/>
// <xsd:element name="right" type="CT_Border" minOccurs="0"/>
// <xsd:element name="insideH" type="CT_Border" minOccurs="0"/>
// <xsd:element name="insideV" type="CT_Border" minOccurs="0"/>
// <xsd:element name="tl2br" type="CT_Border" minOccurs="0"/>
// <xsd:element name="tr2bl" type="CT_Border" minOccurs="0"/>
// </xsd:sequence>
// </xsd:complexType>
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" };
}
// <xsd:complexType name="CT_TcPrBase">
// ...
// <xsd:element name="gridSpan" type="CT_DecimalNumber" minOccurs="0"/>
// </xsd>
/**
* 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),
}),
);
}

View File

@ -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;
}
// <xsd:complexType name="CT_TblPPr">
// <xsd:attribute name="leftFromText" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="rightFromText" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="topFromText" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="bottomFromText" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="vertAnchor" type="ST_VAnchor"/>
// <xsd:attribute name="horzAnchor" type="ST_HAnchor"/>
// <xsd:attribute name="tblpXSpec" type="s:ST_XAlign"/>
// <xsd:attribute name="tblpX" type="ST_SignedTwipsMeasure"/>
// <xsd:attribute name="tblpYSpec" type="s:ST_YAlign"/>
// <xsd:attribute name="tblpY" type="ST_SignedTwipsMeasure"/>
// </xsd:complexType>
export class TableFloatOptionsAttributes extends XmlAttributeComponent<ITableFloatOptions> {
protected readonly xmlKeys = {
horizontalAnchor: "w:horzAnchor",
@ -129,23 +143,30 @@ export class TableFloatOptionsAttributes extends XmlAttributeComponent<ITableFlo
};
}
// <xsd:complexType name="CT_TblPPr">
// <xsd:attribute name="leftFromText" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="rightFromText" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="topFromText" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="bottomFromText" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="vertAnchor" type="ST_VAnchor"/>
// <xsd:attribute name="horzAnchor" type="ST_HAnchor"/>
// <xsd:attribute name="tblpXSpec" type="s:ST_XAlign"/>
// <xsd:attribute name="tblpX" type="ST_SignedTwipsMeasure"/>
// <xsd:attribute name="tblpYSpec" type="s:ST_YAlign"/>
// <xsd:attribute name="tblpY" type="ST_SignedTwipsMeasure"/>
// </xsd:complexType>
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));

View File

@ -1,5 +1,11 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
// <xsd:simpleType name="ST_TblLayoutType">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="fixed"/>
// <xsd:enumeration value="autofit"/>
// </xsd:restriction>
// </xsd:simpleType>
export enum TableLayoutType {
AUTOFIT = "autofit",
FIXED = "fixed",
@ -12,12 +18,6 @@ class TableLayoutAttributes extends XmlAttributeComponent<{ readonly type: Table
// <xsd:complexType name="CT_TblLayoutType">
// <xsd:attribute name="type" type="ST_TblLayoutType"/>
// </xsd:complexType>
// <xsd:simpleType name="ST_TblLayoutType">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="fixed"/>
// <xsd:enumeration value="autofit"/>
// </xsd:restriction>
// </xsd:simpleType>
export class TableLayout extends XmlComponent {
constructor(type: TableLayoutType) {
super("w:tblLayout");

View File

@ -1,5 +1,11 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
// <xsd:simpleType name="ST_TblOverlap">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="never"/>
// <xsd:enumeration value="overlap"/>
// </xsd:restriction>
// </xsd:simpleType>
export enum OverlapType {
NEVER = "never",
OVERLAP = "overlap",
@ -8,13 +14,6 @@ export enum OverlapType {
// <xsd:complexType name="CT_TblOverlap">
// <xsd:attribute name="val" type="ST_TblOverlap" use="required"/>
// </xsd:complexType>
// <xsd:simpleType name="ST_TblOverlap">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="never"/>
// <xsd:enumeration value="overlap"/>
// </xsd:restriction>
// </xsd:simpleType>
class TableOverlapAttributes extends XmlAttributeComponent<{ readonly val: OverlapType }> {
protected readonly xmlKeys = { val: "w:val" };
}

View File

@ -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);
}
// <xsd:simpleType name="ST_EighthPointMeasure">
// <xsd:restriction base="s:ST_UnsignedDecimalNumber"/>
// </xsd:simpleType>
export const eighthPointMeasureValue = unsignedDecimalNumber;
// <xsd:simpleType name="ST_PointMeasure">
// <xsd:restriction base="s:ST_UnsignedDecimalNumber"/>
// </xsd:simpleType>
export const pointMeasureValue = unsignedDecimalNumber;