From 762fc0ca9db084281b4a8b1f878d98c5c7da2c9b Mon Sep 17 00:00:00 2001 From: felipe Date: Mon, 13 Mar 2017 12:30:36 +0100 Subject: [PATCH] added run formatting methods to levels --- ts/numbering/level.ts | 63 ++++++++++++++ ts/tests/numberingTest.ts | 170 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 233 insertions(+) diff --git a/ts/numbering/level.ts b/ts/numbering/level.ts index bc59625212..4aabee238a 100644 --- a/ts/numbering/level.ts +++ b/ts/numbering/level.ts @@ -1,5 +1,6 @@ import * as paragraph from "../docx/paragraph/formatting"; import { ParagraphProperties } from "../docx/paragraph/properties"; +import * as formatting from "../docx/run/formatting"; import { RunProperties } from "../docx/run/properties"; import { Attributes, XmlAttributeComponent, XmlComponent } from "../docx/xml-components"; @@ -88,6 +89,68 @@ export class Level extends XmlComponent { return this; } + // ---------- Run formatting ---------------------- // + + public size(twips: number): Level { + this.addRunProperty(new formatting.Size(twips)); + return this; + } + + public bold(): Level { + this.addRunProperty(new formatting.Bold()); + return this; + } + + public italics(): Level { + this.addRunProperty(new formatting.Italics()); + return this; + } + + public smallCaps(): Level { + this.addRunProperty(new formatting.SmallCaps()); + return this; + } + + public allCaps(): Level { + this.addRunProperty(new formatting.Caps()); + return this; + } + + public strike(): Level { + this.addRunProperty(new formatting.Strike()); + return this; + } + + public doubleStrike(): Level { + this.addRunProperty(new formatting.DoubleStrike()); + return this; + } + + public subScript(): Level { + this.addRunProperty(new formatting.SubScript()); + return this; + } + + public superScript(): Level { + this.addRunProperty(new formatting.SuperScript()); + return this; + } + + public underline(underlineType?: string, color?: string): Level { + this.addRunProperty(new formatting.Underline(underlineType, color)); + return this; + } + + public color(color: string): Level { + this.addRunProperty(new formatting.Color(color)); + return this; + } + + public font(fontName: string): Level { + this.addRunProperty(new formatting.RunFonts(fontName)); + return this; + } + // --------------------- Paragraph formatting ------------------------ // public center(): Level { diff --git a/ts/tests/numberingTest.ts b/ts/tests/numberingTest.ts index 210cbe8a43..59a71e720e 100644 --- a/ts/tests/numberingTest.ts +++ b/ts/tests/numberingTest.ts @@ -222,5 +222,175 @@ describe("AbstractNumbering", () => { }); }); }); + + describe("formatting methods: run properties", () => { + it("#size", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .size(24); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:sz": [{_attr: {"w:val": 24}}]}, + ], + }); + }); + + it("#smallCaps", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .smallCaps(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:smallCaps": [{_attr: {"w:val": true}}]}, + ], + }); + }); + + it("#allCaps", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .allCaps(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:caps": [{_attr: {"w:val": true}}]}, + ], + }); + }); + + it("#strike", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .strike(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:strike": [{_attr: {"w:val": true}}]}, + ], + }); + }); + + it("#doubleStrike", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .doubleStrike(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:dstrike": [{_attr: {"w:val": true}}]}, + ], + }); + }); + + it("#subScript", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .subScript(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:vertAlign": [{_attr: {"w:val": "subscript"}}]}, + ], + }); + }); + + it("#superScript", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .superScript(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:vertAlign": [{_attr: {"w:val": "superscript"}}]}, + ], + }); + }); + + it("#font", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .font("Times"); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [{"w:rFonts": [{_attr: {"w:ascii": "Times", "w:hAnsi": "Times"}}]}], + }); + }); + + it("#bold", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .bold(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:b": [{_attr: {"w:val": true}}]}, + ], + }); + }); + + it("#italics", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .italics(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:i": [{_attr: {"w:val": true}}]}, + ], + }); + }); + + describe("#underline", () => { + it("should set underline to 'single' if no arguments are given", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .underline(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:u": [{_attr: {"w:val": "single"}}]}, + ], + }); + }); + + it("should set the style if given", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .underline("double"); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:u": [{_attr: {"w:val": "double"}}]}, + ], + }); + }); + + it("should set the style and color if given", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .underline("double", "005599"); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:u": [{_attr: {"w:val": "double", "w:color": "005599"}}]}, + ], + }); + }); + }); + + it("#color", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .color("123456"); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:color": [{_attr: {"w:val": "123456"}}]}, + ], + }); + }); + }); }); });