diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-07-03 19:43:00 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-07-03 19:43:00 +0100 |
| commit | ba731d3c8a9408b9d33deb63e0492b54298169c5 (patch) | |
| tree | aa7ab6403bdfd8d923c6b9e86eeccf006dcb09ca | |
| parent | d98f3862193c6bb9b88735c131531ac6b1f7b5c0 (diff) | |
Read author name and initials for each comment
| -rw-r--r-- | lib/documents.js | 4 | ||||
| -rw-r--r-- | lib/docx/comments-reader.js | 12 | ||||
| -rw-r--r-- | test/docx/comments-reader.tests.js | 45 |
3 files changed, 58 insertions, 3 deletions
diff --git a/lib/documents.js b/lib/documents.js index a8312e9..1869dab 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -117,7 +117,9 @@ function comment(options) { return { type: types.comment, commentId: options.commentId, - body: options.body + body: options.body, + authorName: options.authorName, + authorInitials: options.authorInitials }; } diff --git a/lib/docx/comments-reader.js b/lib/docx/comments-reader.js index be98bac..41bf8f6 100644 --- a/lib/docx/comments-reader.js +++ b/lib/docx/comments-reader.js @@ -9,9 +9,19 @@ function createCommentsReader(bodyReader) { function readCommentElement(element) { var id = element.attributes["w:id"]; + + function readOptionalAttribute(name) { + return (element.attributes[name] || "").trim() || null; + } + return bodyReader.readXmlElements(element.children) .map(function(body) { - return documents.comment({commentId: id, body: body}); + return documents.comment({ + commentId: id, + body: body, + authorName: readOptionalAttribute("w:author"), + authorInitials: readOptionalAttribute("w:initials") + }); }); } diff --git a/test/docx/comments-reader.tests.js b/test/docx/comments-reader.tests.js index 55cffeb..2902f29 100644 --- a/test/docx/comments-reader.tests.js +++ b/test/docx/comments-reader.tests.js @@ -7,8 +7,9 @@ var xml = require("../../lib/xml"); var test = require("../test")(module); +var bodyReader = new BodyReader({}); + test('ID and body of comment are read', function() { - var bodyReader = new BodyReader({}); var body = [xml.element("w:p")]; var comments = createCommentsReader(bodyReader)({ root: xml.element("w:comments", {}, [ @@ -19,3 +20,45 @@ test('ID and body of comment are read', function() { assert.deepEqual(comments.value[0].body, [new documents.Paragraph([])]); assert.deepEqual(comments.value[0].commentId, "1"); }); + + +test('when optional attributes of comment are missing then they are read as null', function() { + var body = [xml.element("w:p")]; + var comments = createCommentsReader(bodyReader)({ + root: xml.element("w:comments", {}, [ + xml.element("w:comment", {"w:id": "1"}, body) + ]) + }); + assert.equal(comments.value.length, 1); + var comment = comments.value[0]; + assert.strictEqual(comment.authorName, null); + assert.strictEqual(comment.authorInitials, null); +}); + + +test('when optional attributes of comment are blank then they are read as null', function() { + var body = [xml.element("w:p")]; + var comments = createCommentsReader(bodyReader)({ + root: xml.element("w:comments", {}, [ + xml.element("w:comment", {"w:id": "1", "w:author": " ", "w:initials": " "}, body) + ]) + }); + assert.equal(comments.value.length, 1); + var comment = comments.value[0]; + assert.strictEqual(comment.authorName, null); + assert.strictEqual(comment.authorInitials, null); +}); + + +test('when optional attributes of comment are not blank then they are read', function() { + var body = [xml.element("w:p")]; + var comments = createCommentsReader(bodyReader)({ + root: xml.element("w:comments", {}, [ + xml.element("w:comment", {"w:id": "1", "w:author": "The Piemaker", "w:initials": "TP"}, body) + ]) + }); + assert.equal(comments.value.length, 1); + var comment = comments.value[0]; + assert.strictEqual(comment.authorName, "The Piemaker"); + assert.strictEqual(comment.authorInitials, "TP"); +}); |
