diff --git a/ts/styles/style/index.ts b/ts/styles/style/index.ts index 7730c039e3..f8e1974374 100644 --- a/ts/styles/style/index.ts +++ b/ts/styles/style/index.ts @@ -26,9 +26,12 @@ class StyleAttributes extends XmlAttributeComponent { export class Style extends XmlComponent { - constructor(attributes: IStyleAttributes) { + constructor(attributes: IStyleAttributes, name?: string) { super("w:style"); this.root.push(new StyleAttributes(attributes)); + if (name) { + this.root.push(new Name(name)); + } } public push(styleSegment: XmlComponent): void { @@ -41,8 +44,8 @@ export class ParagraphStyle extends Style { private paragraphProperties: ParagraphProperties; private runProperties: RunProperties; - constructor(styleId: string) { - super({type: "paragraph", styleId: styleId}); + constructor(styleId: string, name?: string) { + super({type: "paragraph", styleId: styleId}, name); this.paragraphProperties = new ParagraphProperties(); this.runProperties = new RunProperties(); this.root.push(this.paragraphProperties); diff --git a/ts/tests/stylesTest.ts b/ts/tests/stylesTest.ts index e448b3e2a1..562f655d42 100644 --- a/ts/tests/stylesTest.ts +++ b/ts/tests/stylesTest.ts @@ -1,7 +1,7 @@ import { assert, expect } from "chai"; import { Formatter } from "../export/formatter"; import { Styles } from "../styles"; -import { Style } from "../styles/style"; +import { ParagraphStyle, Style } from "../styles/style"; import * as components from "../styles/style/components"; describe("Styles", () => { @@ -34,6 +34,20 @@ describe("Style", () => { ], }); }); + + it("should set the name of the style, if given", () => { + const style = new Style({ + type: "paragraph", + styleId: "myStyleId", + }, "Style Name"); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:name": [{_attr: {"w:val": "Style Name"}}]}, + ], + }); + }); }); }); @@ -68,3 +82,33 @@ describe("Style components", () => { expect(tree).to.deep.equal({"w:uiPriority": [{_attr: {"w:val": "123"}}]}); }); }); + + +describe("ParagraphStyle", () => { + describe("#constructor", () => { + it("should set the style type to paragraph and use the given style id", () => { + const style = new ParagraphStyle("myStyleId"); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": [{_attr: {}}]}, + {"w:rPr": []}, + ], + }); + }); + + it("should set the name of the style, if given", () => { + const style = new ParagraphStyle("myStyleId", "Style Name"); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:name": [{_attr: {"w:val": "Style Name"}}]}, + {"w:pPr": [{_attr: {}}]}, + {"w:rPr": []}, + ], + }); + }); + }); +});