From 237be76d33cf093cd5729a040739de659f11194b Mon Sep 17 00:00:00 2001 From: felipe Date: Wed, 8 Mar 2017 19:48:32 +0100 Subject: [PATCH 1/4] move run-fonts to docx/run and add a test --- ts/{numbering => docx/run}/run-fonts.ts | 6 +++--- ts/numbering/index.ts | 2 +- ts/tests/docx/run/fontTests.ts | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) rename ts/{numbering => docx/run}/run-fonts.ts (79%) create mode 100644 ts/tests/docx/run/fontTests.ts diff --git a/ts/numbering/run-fonts.ts b/ts/docx/run/run-fonts.ts similarity index 79% rename from ts/numbering/run-fonts.ts rename to ts/docx/run/run-fonts.ts index dc4a8cd77b..a3ffa89f84 100644 --- a/ts/numbering/run-fonts.ts +++ b/ts/docx/run/run-fonts.ts @@ -1,9 +1,9 @@ -import {XmlAttributeComponent, XmlComponent} from "../docx/xml-components"; +import {XmlAttributeComponent, XmlComponent} from "../xml-components"; interface IRunFontAttributesProperties { ascii: string; hAnsi: string; - hint: string; + hint?: string; } class RunFontAttributes extends XmlAttributeComponent { @@ -19,7 +19,7 @@ class RunFontAttributes extends XmlAttributeComponent { export class RunFonts extends XmlComponent { - constructor(ascii: string, hint: string) { + constructor(ascii: string, hint?: string) { super("w:rFonts"); this.root.push(new RunFontAttributes({ ascii: ascii, diff --git a/ts/numbering/index.ts b/ts/numbering/index.ts index b813c665cf..39bfda0867 100644 --- a/ts/numbering/index.ts +++ b/ts/numbering/index.ts @@ -1,11 +1,11 @@ import * as _ from "lodash"; import { DocumentAttributes } from "../docx/document/document-attributes"; +import { RunFonts } from "../docx/run/run-fonts"; import { MultiPropertyXmlComponent } from "../docx/xml-components"; import { AbstractNumbering } from "./abstract-numbering"; import { Indent } from "./indent"; import { Level } from "./level"; import { Num } from "./num"; -import { RunFonts } from "./run-fonts"; export class Numbering extends MultiPropertyXmlComponent { private nextId: number; diff --git a/ts/tests/docx/run/fontTests.ts b/ts/tests/docx/run/fontTests.ts new file mode 100644 index 0000000000..a136a6949a --- /dev/null +++ b/ts/tests/docx/run/fontTests.ts @@ -0,0 +1,22 @@ +import { expect } from "chai"; +import { RunFonts } from "../../../docx/run/run-fonts"; +import { Formatter } from "../../../export/formatter"; + +describe("RunFonts", () => { + + describe("#constructor()", () => { + it("uses the font name for both ascii and hAnsi", () => { + const tree = new Formatter().format(new RunFonts("Times")); + expect(tree).to.deep.equal({ + "w:rFonts": [{_attr: {"w:ascii": "Times", "w:hAnsi": "Times"}}], + }); + }); + + it("uses hint if given", () => { + const tree = new Formatter().format(new RunFonts("Times", "default")); + expect(tree).to.deep.equal({ + "w:rFonts": [{_attr: {"w:ascii": "Times", "w:hAnsi": "Times", "w:hint": "default"}}], + }); + }); + }); +}); From 81e0d5691885d1f3b6381cfc68b44a6a6fdc70dd Mon Sep 17 00:00:00 2001 From: felipe Date: Wed, 8 Mar 2017 20:28:59 +0100 Subject: [PATCH 2/4] fix linter warnings in runTest.ts --- ts/tests/docx/run/runTest.ts | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/ts/tests/docx/run/runTest.ts b/ts/tests/docx/run/runTest.ts index fdec45c51d..8f87e0413d 100644 --- a/ts/tests/docx/run/runTest.ts +++ b/ts/tests/docx/run/runTest.ts @@ -1,10 +1,10 @@ +import { assert } from "chai"; import { Run } from "../../../docx/run"; import { TextRun } from "../../../docx/run/text-run"; -import { assert } from "chai"; +import { Formatter } from "../../../export/formatter"; -function jsonify(obj: Object) { - let stringifiedJson = JSON.stringify(obj); - return JSON.parse(stringifiedJson); +function jsonify(obj: object) { + return JSON.parse(JSON.stringify(obj)); } describe("Run", () => { @@ -17,7 +17,7 @@ describe("Run", () => { describe("#bold()", () => { it("it should add bold to the properties", () => { run.bold(); - let newJson = jsonify(run); + const newJson = jsonify(run); assert.equal(newJson.root[0].root[0].rootKey, "w:b"); }); }); @@ -25,7 +25,7 @@ describe("Run", () => { describe("#italic()", () => { it("it should add italics to the properties", () => { run.italic(); - let newJson = jsonify(run); + const newJson = jsonify(run); assert.equal(newJson.root[0].root[0].rootKey, "w:i"); }); }); @@ -33,7 +33,7 @@ describe("Run", () => { describe("#underline()", () => { it("it should add underline to the properties", () => { run.underline(); - let newJson = jsonify(run); + const newJson = jsonify(run); assert.equal(newJson.root[0].root[0].rootKey, "w:u"); }); }); @@ -41,7 +41,7 @@ describe("Run", () => { describe("#smallCaps()", () => { it("it should add smallCaps to the properties", () => { run.smallCaps(); - let newJson = jsonify(run); + const newJson = jsonify(run); assert.equal(newJson.root[0].root[0].rootKey, "w:smallCaps"); }); }); @@ -49,7 +49,7 @@ describe("Run", () => { describe("#caps()", () => { it("it should add caps to the properties", () => { run.allCaps(); - let newJson = jsonify(run); + const newJson = jsonify(run); assert.equal(newJson.root[0].root[0].rootKey, "w:caps"); }); }); @@ -57,7 +57,7 @@ describe("Run", () => { describe("#strike()", () => { it("it should add strike to the properties", () => { run.strike(); - let newJson = jsonify(run); + const newJson = jsonify(run); assert.equal(newJson.root[0].root[0].rootKey, "w:strike"); }); }); @@ -65,26 +65,24 @@ describe("Run", () => { describe("#doubleStrike()", () => { it("it should add caps to the properties", () => { run.doubleStrike(); - let newJson = jsonify(run); + const newJson = jsonify(run); assert.equal(newJson.root[0].root[0].rootKey, "w:dstrike"); }); }); describe("#break()", () => { it("it should add break to the run", () => { - let run = new Run(); run.break(); - let newJson = jsonify(run); + const newJson = jsonify(run); assert.equal(newJson.root[1].rootKey, "w:br"); }); }); describe("#tab()", () => { it("it should add break to the run", () => { - let run = new Run(); run.tab(); - let newJson = jsonify(run); + const newJson = jsonify(run); assert.equal(newJson.root[1].rootKey, "w:tab"); }); }); -}); \ No newline at end of file +}); From 1a37242a3ddf036c5889d314df25d677eb183577 Mon Sep 17 00:00:00 2001 From: felipe Date: Wed, 8 Mar 2017 20:29:12 +0100 Subject: [PATCH 3/4] add #font method to Run --- ts/docx/run/index.ts | 9 +++++++-- ts/tests/docx/run/runTest.ts | 18 +++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/ts/docx/run/index.ts b/ts/docx/run/index.ts index 11c89c594f..0ec7c97e9d 100644 --- a/ts/docx/run/index.ts +++ b/ts/docx/run/index.ts @@ -1,4 +1,5 @@ import {XmlComponent, Attributes} from "../xml-components"; +import { RunFonts } from "./run-fonts"; import {RunProperties} from "./properties"; import {Bold, Italics} from "./formatting"; import {Tab} from "./tab"; @@ -11,7 +12,6 @@ import {Underline} from "./underline" export class Run extends XmlComponent { private properties: RunProperties; - constructor() { super("w:r"); this.properties = new RunProperties(); @@ -72,4 +72,9 @@ export class Run extends XmlComponent { this.properties.push(new SuperScript()); return this; } -} \ No newline at end of file + + font(fontName: string): Run { + this.properties.push(new RunFonts(fontName)); + return this; + } +} diff --git a/ts/tests/docx/run/runTest.ts b/ts/tests/docx/run/runTest.ts index 8f87e0413d..a57e517e5b 100644 --- a/ts/tests/docx/run/runTest.ts +++ b/ts/tests/docx/run/runTest.ts @@ -1,4 +1,4 @@ -import { assert } from "chai"; +import { assert, expect } from "chai"; import { Run } from "../../../docx/run"; import { TextRun } from "../../../docx/run/text-run"; import { Formatter } from "../../../export/formatter"; @@ -85,4 +85,20 @@ describe("Run", () => { assert.equal(newJson.root[1].rootKey, "w:tab"); }); }); + + describe("#font()", () => { + it("should allow chaining calls", () => { + expect(run.font("Times")).to.equal(run); + }); + + it("should set the font as named", () => { + run.font("Times"); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + {"w:rPr": [{"w:rFonts": [{_attr: {"w:ascii": "Times", "w:hAnsi": "Times"}}]}]}, + ], + }); + }); + }); }); From 727471b7e35b83402d1bd29be573aa3ba13f19f3 Mon Sep 17 00:00:00 2001 From: felipe Date: Wed, 8 Mar 2017 20:52:37 +0100 Subject: [PATCH 4/4] fix linter warnings in docx/run/index.ts --- ts/docx/run/index.ts | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/ts/docx/run/index.ts b/ts/docx/run/index.ts index 0ec7c97e9d..5619adf1fc 100644 --- a/ts/docx/run/index.ts +++ b/ts/docx/run/index.ts @@ -1,13 +1,14 @@ -import {XmlComponent, Attributes} from "../xml-components"; +import { Break } from "./break"; +import { Caps, SmallCaps } from "./caps"; +import { Bold, Italics } from "./formatting"; +import { RunProperties } from "./properties"; import { RunFonts } from "./run-fonts"; -import {RunProperties} from "./properties"; -import {Bold, Italics} from "./formatting"; -import {Tab} from "./tab"; -import {Break} from "./break"; -import {SmallCaps, Caps} from "./caps"; -import {Strike, DoubleStrike} from "./strike"; -import {SubScript, SuperScript} from "./script"; -import {Underline} from "./underline" +import { SubScript, SuperScript } from "./script"; +import { DoubleStrike, Strike } from "./strike"; +import { Tab } from "./tab"; +import { Underline } from "./underline"; + +import { Attributes, XmlComponent } from "../xml-components"; export class Run extends XmlComponent { private properties: RunProperties; @@ -18,62 +19,62 @@ export class Run extends XmlComponent { this.root.push(this.properties); } - bold(): Run { + public bold(): Run { this.properties.push(new Bold()); return this; } - italic(): Run { + public italic(): Run { this.properties.push(new Italics()); return this; } - underline(): Run { + public underline(): Run { this.properties.push(new Underline()); return this; } - break(): Run { + public break(): Run { this.root.splice(1, 0, new Break()); return this; } - tab(): Run { + public tab(): Run { this.root.splice(1, 0, new Tab()); return this; } - smallCaps(): Run { + public smallCaps(): Run { this.properties.push(new SmallCaps()); return this; } - allCaps(): Run { + public allCaps(): Run { this.properties.push(new Caps()); return this; } - strike(): Run { + public strike(): Run { this.properties.push(new Strike()); return this; } - doubleStrike(): Run { + public doubleStrike(): Run { this.properties.push(new DoubleStrike()); return this; } - subScript(): Run { + public subScript(): Run { this.properties.push(new SubScript()); return this; } - superScript(): Run { + public superScript(): Run { this.properties.push(new SuperScript()); return this; } - font(fontName: string): Run { + public font(fontName: string): Run { this.properties.push(new RunFonts(fontName)); return this; }