clean up style & components attributes

This commit is contained in:
felipe
2017-03-09 10:54:40 +01:00
parent 18ca93e50a
commit e6e0658812
4 changed files with 97 additions and 53 deletions

View File

@ -1,23 +0,0 @@
import { XmlAttributeComponent, XmlComponent } from "../../docx/xml-components";
interface IStyleAttributesProperties {
type?: string;
styleId?: string;
default?: string;
customStyle?: string;
val?: string;
}
export class StyleAttributes extends XmlAttributeComponent {
private _attr: IStyleAttributesProperties;
constructor(properties: IStyleAttributesProperties) {
super({
type: "w:type",
styleId: "w:styleId",
default: "w:default",
customStyle: "w:customStyle",
val: "w:val",
}, properties);
}
}

View File

@ -1,13 +1,22 @@
import { XmlComponent } from "../../docx/xml-components"; import { XmlAttributeComponent, XmlComponent } from "../../docx/xml-components";
import { StyleAttributes } from "./attributes";
interface IComponentAttributes {
val: string;
}
class ComponentAttributes extends XmlAttributeComponent {
private _attr: IComponentAttributes;
constructor(properties: IComponentAttributes) {
super({val: "w:val"}, properties);
}
}
export class Name extends XmlComponent { export class Name extends XmlComponent {
constructor(value: string) { constructor(value: string) {
super("w:name"); super("w:name");
this.root.push(new StyleAttributes({ this.root.push(new ComponentAttributes({val: value}));
val: value,
}));
} }
} }
@ -15,9 +24,7 @@ export class BasedOn extends XmlComponent {
constructor(value: string) { constructor(value: string) {
super("w:basedOn"); super("w:basedOn");
this.root.push(new StyleAttributes({ this.root.push(new ComponentAttributes({val: value}));
val: value,
}));
} }
} }
@ -25,10 +32,7 @@ export class Next extends XmlComponent {
constructor(value: string) { constructor(value: string) {
super("w:next"); super("w:next");
this.root.push(new StyleAttributes({ this.root.push(new ComponentAttributes({val: value}));
styleId: "1",
val: value,
}));
} }
} }
@ -36,9 +40,7 @@ export class Link extends XmlComponent {
constructor(value: string) { constructor(value: string) {
super("w:link"); super("w:link");
this.root.push(new StyleAttributes({ this.root.push(new ComponentAttributes({val: value}));
val: value,
}));
} }
} }
@ -46,9 +48,8 @@ export class UiPriority extends XmlComponent {
constructor(value: string) { constructor(value: string) {
super("w:uiPriority"); super("w:uiPriority");
this.root.push(new StyleAttributes({ // TODO: this value should be a ST_DecimalNumber
val: value, this.root.push(new ComponentAttributes({val: value}));
}));
} }
} }

View File

@ -1,15 +1,34 @@
import { ParagraphProperties } from "../../docx/paragraph/properties"; import { ParagraphProperties } from "../../docx/paragraph/properties";
import { RunProperties } from "../../docx/run/properties"; import { RunProperties } from "../../docx/run/properties";
import { XmlComponent } from "../../docx/xml-components"; import { XmlAttributeComponent, XmlComponent } from "../../docx/xml-components";
import { StyleAttributes } from "./attributes";
import { BasedOn, Name, Next, QuickFormat } from "./components"; import { BasedOn, Name, Next, QuickFormat } from "./components";
export interface IStyleAttributes {
type?: string;
styleId?: string;
default?: boolean;
customStyle?: string;
}
class StyleAttributes extends XmlAttributeComponent {
private _attr: IStyleAttributes;
constructor(properties: IStyleAttributes) {
super({
type: "w:type",
styleId: "w:styleId",
default: "w:default",
customStyle: "w:customStyle",
}, properties);
}
}
export class Style extends XmlComponent { export class Style extends XmlComponent {
constructor(attributes: StyleAttributes) { constructor(attributes: IStyleAttributes) {
super("w:style"); super("w:style");
this.root.push(attributes); this.root.push(new StyleAttributes(attributes));
} }
public push(styleSegment: XmlComponent): void { public push(styleSegment: XmlComponent): void {
@ -23,12 +42,7 @@ export class ParagraphStyle extends Style {
private runProperties: RunProperties; private runProperties: RunProperties;
constructor(styleId: string) { constructor(styleId: string) {
super({type: "paragraph", styleId: styleId});
const attributes = new StyleAttributes({
type: "paragraph",
styleId: styleId,
});
super(attributes);
this.paragraphProperties = new ParagraphProperties(); this.paragraphProperties = new ParagraphProperties();
this.runProperties = new RunProperties(); this.runProperties = new RunProperties();
this.root.push(this.paragraphProperties); this.root.push(this.paragraphProperties);

View File

@ -1,5 +1,8 @@
import { assert } from "chai"; import { assert, expect } from "chai";
import { Formatter } from "../export/formatter";
import { Styles } from "../styles"; import { Styles } from "../styles";
import { Style } from "../styles/style";
import * as components from "../styles/style/components";
describe("Styles", () => { describe("Styles", () => {
let styles: Styles; let styles: Styles;
@ -9,10 +12,59 @@ describe("Styles", () => {
}); });
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create styles with correct rootKey", () => { it("should create styles with correct rootKey", () => {
const newJson = JSON.parse(JSON.stringify(styles)); const newJson = JSON.parse(JSON.stringify(styles));
assert.equal(newJson.rootKey, "w:styles"); assert.equal(newJson.rootKey, "w:styles");
}); });
}); });
}); });
describe("Style", () => {
describe("#constructor()", () => {
it("should set the given properties", () => {
const style = new Style({
type: "paragraph",
styleId: "myStyleId",
default: true,
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId", "w:default": true}},
],
});
});
});
});
describe("Style components", () => {
it("Name#constructor", () => {
const style = new components.Name("Style Name");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({"w:name": [{_attr: {"w:val": "Style Name"}}]});
});
it("BasedOn#constructor", () => {
const style = new components.BasedOn("otherId");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({"w:basedOn": [{_attr: {"w:val": "otherId"}}]});
});
it("Next#constructor", () => {
const style = new components.Next("otherId");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({"w:next": [{_attr: {"w:val": "otherId"}}]});
});
it("Link#constructor", () => {
const style = new components.Link("otherId");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({"w:link": [{_attr: {"w:val": "otherId"}}]});
});
it("UiPriority#constructor", () => {
const style = new components.UiPriority("123");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({"w:uiPriority": [{_attr: {"w:val": "123"}}]});
});
});