diff --git a/src/file/file.ts b/src/file/file.ts index ddc55841b3..866ddd1480 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -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; } diff --git a/src/file/paragraph/links/bookmark.spec.ts b/src/file/paragraph/links/bookmark.spec.ts new file mode 100644 index 0000000000..08d0aec91c --- /dev/null +++ b/src/file/paragraph/links/bookmark.spec.ts @@ -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)); + }); +}); diff --git a/src/file/paragraph/links/bookmark.ts b/src/file/paragraph/links/bookmark.ts index 661ca4118a..de4fabdaa2 100644 --- a/src/file/paragraph/links/bookmark.ts +++ b/src/file/paragraph/links/bookmark.ts @@ -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; diff --git a/src/file/paragraph/links/hyperlink.spec.ts b/src/file/paragraph/links/hyperlink.spec.ts index e3f0b58c49..d6432b432a 100644 --- a/src/file/paragraph/links/hyperlink.spec.ts +++ b/src/file/paragraph/links/hyperlink.spec.ts @@ -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)); + }); + }); }); });