This commit is contained in:
Michael Myers
2018-07-24 16:56:27 -04:00
parent cc904ee211
commit 985ea30d36
4 changed files with 62 additions and 3 deletions

View File

@ -160,6 +160,8 @@ export class File {
public createInternalHyperLink(anchor: string, text?: string): Hyperlink {
text = text === undefined ? anchor : text;
const hyperlink = new Hyperlink(text, this.docRelationships.RelationshipCount, anchor);
// NOTE: unlike File#createHyperlink(), since the link is to an internal bookmark
// we don't need to create a new relationship.
return hyperlink;
}

View File

@ -0,0 +1,42 @@
import { assert } from "chai";
import { Utility } from "../../../tests/utility";
import { Bookmark } from "./";
describe("Bookmark", () => {
let bookmark: Bookmark;
beforeEach(() => {
bookmark = new Bookmark("anchor", "Internal Link", 0);
});
it("should create a bookmark with three root elements", () => {
const newJson = Utility.jsonify(bookmark);
assert.equal(newJson.rootKey, undefined);
assert.equal(newJson.start.rootKey, "w:bookmarkStart");
assert.equal(newJson.text.rootKey, "w:r");
assert.equal(newJson.end.rootKey, "w:bookmarkEnd");
});
it("should create a bookmark with the correct attributes on the bookmark start element", () => {
const newJson = Utility.jsonify(bookmark);
const attributes = {
name: "anchor",
id: "1",
};
assert.equal(JSON.stringify(newJson.start.root[0].root), JSON.stringify(attributes));
});
it("should create a bookmark with the correct attributes on the text element", () => {
const newJson = Utility.jsonify(bookmark);
assert.equal(JSON.stringify(newJson.text.root[1].root[1]), JSON.stringify("Internal Link"));
});
it("should create a bookmark with the correct attributes on the bookmark end element", () => {
const newJson = Utility.jsonify(bookmark);
const attributes = {
id: "1",
};
assert.equal(JSON.stringify(newJson.end.root[0].root), JSON.stringify(attributes));
});
});

View File

@ -16,7 +16,7 @@ export class Bookmark {
constructor(name: string, text: string, relationshipsCount: number) {
this.linkId = relationshipsCount + 1;
this.start = new BookmarkStart(name, text, this.linkId);
this.start = new BookmarkStart(name, this.linkId);
this.text = new TextRun(text);
this.end = new BookmarkEnd(this.linkId);
}
@ -25,7 +25,7 @@ export class Bookmark {
export class BookmarkStart extends XmlComponent {
public linkId: number;
constructor(name: string, text: string, relationshipsCount: number) {
constructor(name: string, relationshipsCount: number) {
super("w:bookmarkStart");
this.linkId = relationshipsCount;

View File

@ -20,8 +20,8 @@ describe("Hyperlink", () => {
it("should create a hyperlink with right attributes", () => {
const newJson = Utility.jsonify(hyperlink);
const attributes = {
id: "rId1",
history: 1,
id: "rId1",
};
assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes));
});
@ -36,5 +36,20 @@ describe("Hyperlink", () => {
};
expect(tree["w:hyperlink"][1]).to.deep.equal(runJson);
});
describe("with optional anchor parameter", () => {
beforeEach(() => {
hyperlink = new Hyperlink("Anchor Text", 0, "anchor");
});
it("should create an internal link with anchor tag", () => {
const newJson = Utility.jsonify(hyperlink);
const attributes = {
history: 1,
anchor: "anchor",
};
assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes));
});
});
});
});