diff --git a/.nycrc b/.nycrc index 24dc1f5d439..3dc8ad302a9 100644 --- a/.nycrc +++ b/.nycrc @@ -1,8 +1,8 @@ { "check-coverage": true, - "statements": 99.73, - "branches": 98.32, - "functions": 99.83, + "statements": 99.79, + "branches": 98.41, + "functions": 100, "lines": 99.73, "include": [ "src/**/*.ts" diff --git a/demo/2-declaritive-styles.ts b/demo/2-declaritive-styles.ts index ce0c03b0adc..352edca0433 100644 --- a/demo/2-declaritive-styles.ts +++ b/demo/2-declaritive-styles.ts @@ -236,6 +236,18 @@ const doc = new Document({ new TextRun({ text: " Another Hello World", }), + new TextRun({ + scale: 50, + text: " Scaled text", + }), + ], + }), + new Paragraph({ + scale: 200, + children: [ + new TextRun({ + text: "Scaled paragraph", + }), ], }), ], diff --git a/src/file/paragraph/formatting/width.ts b/src/file/paragraph/formatting/width.ts new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/file/paragraph/properties.ts b/src/file/paragraph/properties.ts index 09886ceec34..48bcf242b02 100644 --- a/src/file/paragraph/properties.ts +++ b/src/file/paragraph/properties.ts @@ -52,6 +52,7 @@ export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOp readonly frame?: IFrameOptions; readonly suppressLineNumbers?: boolean; readonly wordWrap?: boolean; + readonly scale?: number; } export class ParagraphProperties extends IgnoreIfEmptyXmlComponent { diff --git a/src/file/paragraph/run/properties.ts b/src/file/paragraph/run/properties.ts index b83e0b2c8d6..19593b62dd1 100644 --- a/src/file/paragraph/run/properties.ts +++ b/src/file/paragraph/run/properties.ts @@ -1,7 +1,14 @@ import { BorderElement, IBorderOptions } from "@file/border"; import { IShadingAttributesProperties, Shading } from "@file/shading"; import { ChangeAttributes, IChangedAttributesProperties } from "@file/track-revision/track-revision"; -import { HpsMeasureElement, IgnoreIfEmptyXmlComponent, OnOffElement, StringValueElement, XmlComponent } from "@file/xml-components"; +import { + HpsMeasureElement, + IgnoreIfEmptyXmlComponent, + NumberValueElement, + OnOffElement, + StringValueElement, + XmlComponent, +} from "@file/xml-components"; import { EmphasisMark, EmphasisMarkType } from "./emphasis-mark"; import { CharacterSpacing, Color, Highlight, HighlightComplexScript } from "./formatting"; @@ -47,6 +54,7 @@ export interface IRunStylePropertiesOptions { readonly border?: IBorderOptions; readonly vanish?: boolean; readonly specVanish?: boolean; + readonly scale?: number; } export interface IRunPropertiesOptions extends IRunStylePropertiesOptions { @@ -228,6 +236,10 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent { // https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_specVanish_topic_ID0EIE1O.html this.push(new OnOffElement("w:specVanish", options.vanish)); } + + if (options.scale !== undefined) { + this.push(new NumberValueElement("w:w", options.scale)); + } } public push(item: XmlComponent): void { diff --git a/src/file/paragraph/run/run.spec.ts b/src/file/paragraph/run/run.spec.ts index 3fd6de2d0dc..b1785364356 100644 --- a/src/file/paragraph/run/run.spec.ts +++ b/src/file/paragraph/run/run.spec.ts @@ -556,5 +556,29 @@ describe("Run", () => { ], }); }); + + describe("#scale", () => { + it("should correctly set the border", () => { + const run = new Run({ + scale: 200, + }); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + { + "w:rPr": [ + { + "w:w": { + _attr: { + "w:val": 200, + }, + }, + }, + ], + }, + ], + }); + }); + }); }); }); diff --git a/src/file/table-of-contents/alias.ts b/src/file/table-of-contents/alias.ts deleted file mode 100644 index 3bcdabc1eb7..00000000000 --- a/src/file/table-of-contents/alias.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; - -class AliasAttributes extends XmlAttributeComponent<{ readonly alias: string }> { - protected readonly xmlKeys = { alias: "w:val" }; -} - -export class Alias extends XmlComponent { - public constructor(alias: string) { - super("w:alias"); - this.root.push(new AliasAttributes({ alias })); - } -} diff --git a/src/file/table-of-contents/sdt-properties.ts b/src/file/table-of-contents/sdt-properties.ts index b94407400c6..f1f2c563043 100644 --- a/src/file/table-of-contents/sdt-properties.ts +++ b/src/file/table-of-contents/sdt-properties.ts @@ -1,10 +1,9 @@ // http://www.datypic.com/sc/ooxml/e-w_sdtPr-1.html -import { XmlComponent } from "@file/xml-components"; -import { Alias } from "./alias"; +import { StringValueElement, XmlComponent } from "@file/xml-components"; export class StructuredDocumentTagProperties extends XmlComponent { public constructor(alias: string) { super("w:sdtPr"); - this.root.push(new Alias(alias)); + this.root.push(new StringValueElement("w:alias", alias)); } } diff --git a/src/file/table/table-properties/index.ts b/src/file/table/table-properties/index.ts index c5b5935d7f6..aefcc9e0aa3 100644 --- a/src/file/table/table-properties/index.ts +++ b/src/file/table/table-properties/index.ts @@ -2,4 +2,3 @@ export * from "./table-properties"; export * from "./table-float-properties"; export * from "./table-layout"; export * from "./table-borders"; -export * from "./table-overlap"; diff --git a/src/file/table/table-properties/table-float-properties.spec.ts b/src/file/table/table-properties/table-float-properties.spec.ts index 88efe2821f1..ccc8cb77ef6 100644 --- a/src/file/table/table-properties/table-float-properties.spec.ts +++ b/src/file/table/table-properties/table-float-properties.spec.ts @@ -2,8 +2,13 @@ import { expect } from "chai"; import { Formatter } from "@export/formatter"; -import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType, TableFloatProperties } from "./table-float-properties"; -import { OverlapType } from "./table-overlap"; +import { + OverlapType, + RelativeHorizontalPosition, + RelativeVerticalPosition, + TableAnchorType, + TableFloatProperties, +} from "./table-float-properties"; describe("Table Float Properties", () => { describe("#constructor", () => { diff --git a/src/file/table/table-properties/table-float-properties.ts b/src/file/table/table-properties/table-float-properties.ts index d2ec4d3ab18..fdcf696d070 100644 --- a/src/file/table/table-properties/table-float-properties.ts +++ b/src/file/table/table-properties/table-float-properties.ts @@ -1,8 +1,6 @@ -import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; +import { StringEnumValueElement, XmlAttributeComponent, XmlComponent } from "@file/xml-components"; import { signedTwipsMeasureValue, twipsMeasureValue } from "@util/values"; -import { OverlapType, TableOverlap } from "./table-overlap"; - export enum TableAnchorType { MARGIN = "margin", PAGE = "page", @@ -26,6 +24,17 @@ export enum RelativeVerticalPosition { TOP = "top", } +// +// +// +// +// +// +export enum OverlapType { + NEVER = "never", + OVERLAP = "overlap", +} + export interface ITableFloatOptions { /* cSpell:disable */ /** @@ -171,7 +180,10 @@ export class TableFloatProperties extends XmlComponent { ); if (options.overlap) { - this.root.push(new TableOverlap(options.overlap)); + // + // + // + this.root.push(new StringEnumValueElement("w:tblOverlap", options.overlap)); } } } diff --git a/src/file/table/table-properties/table-overlap.spec.ts b/src/file/table/table-properties/table-overlap.spec.ts deleted file mode 100644 index c4d3830c5e7..00000000000 --- a/src/file/table/table-properties/table-overlap.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { expect } from "chai"; - -import { Formatter } from "@export/formatter"; - -import { OverlapType, TableOverlap } from "./table-overlap"; - -describe("TableOverlap", () => { - describe("#constructor", () => { - it("sets the width attribute to the value given", () => { - const tableOverlap = new TableOverlap(OverlapType.OVERLAP); - const tree = new Formatter().format(tableOverlap); - - expect(tree).to.deep.equal({ - "w:tblOverlap": { - _attr: { - "w:val": "overlap", - }, - }, - }); - }); - }); -}); diff --git a/src/file/table/table-properties/table-overlap.ts b/src/file/table/table-properties/table-overlap.ts deleted file mode 100644 index 42e8d4ce750..00000000000 --- a/src/file/table/table-properties/table-overlap.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; - -// -// -// -// -// -// -export enum OverlapType { - NEVER = "never", - OVERLAP = "overlap", -} - -// -// -// -class TableOverlapAttributes extends XmlAttributeComponent<{ readonly val: OverlapType }> { - protected readonly xmlKeys = { val: "w:val" }; -} - -export class TableOverlap extends XmlComponent { - public constructor(type: OverlapType) { - super("w:tblOverlap"); - this.root.push(new TableOverlapAttributes({ val: type })); - } -} diff --git a/src/file/xml-components/imported-xml-component.spec.ts b/src/file/xml-components/imported-xml-component.spec.ts index a031409a00b..ae0440d6623 100644 --- a/src/file/xml-components/imported-xml-component.spec.ts +++ b/src/file/xml-components/imported-xml-component.spec.ts @@ -3,7 +3,7 @@ import { Element, xml2js } from "xml-js"; import { EMPTY_OBJECT } from "@file/xml-components"; -import { convertToXmlComponent, ImportedXmlComponent } from "./imported-xml-component"; +import { convertToXmlComponent, ImportedRootElementAttributes, ImportedXmlComponent } from "./imported-xml-component"; import { IContext } from "./base"; const xmlString = ` @@ -90,5 +90,29 @@ describe("ImportedXmlComponent", () => { const converted = convertToXmlComponent(xmlObj); expect(converted).to.deep.equal(convertedXmlElement); }); + + it("should return undefined if xml type is invalid", () => { + const xmlObj = { type: "invalid" } as Element; + const converted = convertToXmlComponent(xmlObj); + expect(converted).to.equal(undefined); + }); + }); +}); + +describe("ImportedRootElementAttributes", () => { + let attributes: ImportedRootElementAttributes; + + beforeEach(() => { + attributes = new ImportedRootElementAttributes({}); + }); + + describe("#prepForXml()", () => { + it("should work", () => { + // tslint:disable-next-line: no-object-literal-type-assertion + const converted = attributes.prepForXml({} as IContext); + expect(converted).to.deep.equal({ + _attr: {}, + }); + }); }); }); diff --git a/src/file/xml-components/simple-elements.ts b/src/file/xml-components/simple-elements.ts index 73bac12d051..aca3355c282 100644 --- a/src/file/xml-components/simple-elements.ts +++ b/src/file/xml-components/simple-elements.ts @@ -53,6 +53,13 @@ export class NumberValueElement extends XmlComponent { } } +export class StringEnumValueElement extends XmlComponent { + public constructor(name: string, val: T) { + super(name); + this.root.push(new Attributes({ val })); + } +} + // Simple nodes containing text. // // new StringContainer("hello", "world")