Merge pull request #31 from felipeochoa/spacing-bugfix

Spacing bugfix
This commit is contained in:
Dolan
2017-04-14 17:30:54 +01:00
committed by GitHub
7 changed files with 49 additions and 13 deletions

View File

@ -64,7 +64,11 @@ doc.createParagraph()
doc.createParagraph('An aside, in light gray italics and indented').style('aside'); doc.createParagraph('An aside, in light gray italics and indented').style('aside');
doc.createParagraph('This is normal, but well-spaced text').style('wellSpaced'); doc.createParagraph('This is normal, but well-spaced text').style('wellSpaced');
doc.createParagraph('This is normal'); const para = doc.createParagraph();
para.createTextRun('This is a bold run,').bold();
para.createTextRun(' switching to normal ');
para.createTextRun('and then underlined ').underline();
para.createTextRun('and back to normal.');
const exporter = new docx.LocalPacker(doc, styles, undefined, numbering); const exporter = new docx.LocalPacker(doc, styles, undefined, numbering);
exporter.pack('test.docx'); exporter.pack('test.docx');

View File

@ -1,9 +1,13 @@
import { XmlComponent } from "../../xml-components"; import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
class TextAttributes extends XmlAttributeComponent<{space: "default" | "preserve"}> {
protected xmlKeys = {space: "xml:space"};
}
export class Text extends XmlComponent { export class Text extends XmlComponent {
constructor(text: string) { constructor(text: string) {
super("w:t"); super("w:t");
this.root.push(new TextAttributes({space: "preserve"}));
if (text) { if (text) {
this.root.push(text); this.root.push(text);
} }

View File

@ -41,7 +41,7 @@ describe("Document", () => {
expect(body[0]).to.have.property("w:p").which.includes({ expect(body[0]).to.have.property("w:p").which.includes({
"w:r": [ "w:r": [
{"w:rPr": []}, {"w:rPr": []},
{"w:t": ["sample paragraph text"]}, {"w:t": [{_attr: {"xml:space": "preserve"}}, "sample paragraph text"]},
], ],
}); });
}); });

View File

@ -39,7 +39,7 @@ describe("Paragraph", () => {
expect(tree).to.be.an("array").which.includes({ expect(tree).to.be.an("array").which.includes({
"w:r": [ "w:r": [
{"w:rPr": []}, {"w:rPr": []},
{"w:t": ["this is a test run"]}, {"w:t": [{_attr: {"xml:space": "preserve"}}, "this is a test run"]},
], ],
}); });
}); });

View File

@ -0,0 +1,22 @@
import { expect } from "chai";
import { Text } from "../../../../docx/run/run-components/text";
import { Formatter } from "../../../../export/formatter";
describe("Text", () => {
describe("#constructor", () => {
it("creates an empty text run if no text is given", () => {
const t = new Text("");
const f = new Formatter().format(t);
expect(f).to.deep.equal({"w:t": [{_attr: {"xml:space": "preserve"}}]});
});
it("adds the passed in text to the component", () => {
const t = new Text(" this is\n text");
const f = new Formatter().format(t);
expect(f).to.deep.equal({"w:t": [
{_attr: {"xml:space": "preserve"}},
" this is\n text",
]});
});
});
});

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { TextRun } from "../../../docx/run/text-run"; import { TextRun } from "../../../docx/run/text-run";
import { Utility } from "../../utility"; import { Formatter } from "../../../export/formatter";
describe("TextRun", () => { describe("TextRun", () => {
let run: TextRun; let run: TextRun;
@ -9,8 +9,11 @@ describe("TextRun", () => {
it("should add text into run", () => { it("should add text into run", () => {
run = new TextRun("test"); run = new TextRun("test");
const newJson = Utility.jsonify(run); const f = new Formatter().format(run);
assert.equal(newJson.root[1].root, "test"); expect(f).to.deep.equal({"w:r": [
{"w:rPr": []},
{"w:t": [{_attr: {"xml:space": "preserve"}}, "test"]},
]});
}); });
}); });
}); });

View File

@ -36,7 +36,7 @@ describe("Table", () => {
{"w:tcPr": []}, {"w:tcPr": []},
{"w:p": [ {"w:p": [
{"w:pPr": []}, {"w:pPr": []},
{"w:r": [{"w:rPr": []}, {"w:t": [c]}]}, {"w:r": [{"w:rPr": []}, {"w:t": [{_attr: {"xml:space": "preserve"}}, c]}]},
]}, ]},
]}); ]});
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
@ -65,7 +65,7 @@ describe("Table", () => {
{"w:tcPr": []}, {"w:tcPr": []},
{"w:p": [ {"w:p": [
{"w:pPr": []}, {"w:pPr": []},
{"w:r": [{"w:rPr": []}, {"w:t": [c]}]}, {"w:r": [{"w:rPr": []}, {"w:t": [{_attr: {"xml:space": "preserve"}}, c]}]},
]}, ]},
]}); ]});
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
@ -153,7 +153,7 @@ describe("Table", () => {
{"w:tcPr": []}, {"w:tcPr": []},
{"w:p": [ {"w:p": [
{"w:pPr": []}, {"w:pPr": []},
{"w:r": [{"w:rPr": []}, {"w:t": ["Hello"]}]}, {"w:r": [{"w:rPr": []}, {"w:t": [{_attr: {"xml:space": "preserve"}}, "Hello"]}]},
]}, ]},
], ],
}); });
@ -175,7 +175,10 @@ describe("Table", () => {
{"w:tcPr": []}, {"w:tcPr": []},
{"w:p": [ {"w:p": [
{"w:pPr": []}, {"w:pPr": []},
{"w:r": [{"w:rPr": []}, {"w:t": ["Test paragraph"]}]}, {"w:r": [
{"w:rPr": []},
{"w:t": [{_attr: {"xml:space": "preserve"}}, "Test paragraph"]},
]},
]}, ]},
], ],
}); });