added optional name parameter to style constructor

This commit is contained in:
felipe
2017-03-09 11:09:08 +01:00
parent e6e0658812
commit b4dca79d72
2 changed files with 51 additions and 4 deletions

View File

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

View File

@ -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": []},
],
});
});
});
});