diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-09-07 20:04:58 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-09-07 20:04:58 +0100 |
| commit | e5e37a6f2cfa04e146d534028fbf291aac2a9fe2 (patch) | |
| tree | 82f731589d4534b51b2dd1caf4b51848c0468eb1 /tests | |
| parent | 97144ee9f557ef4886456972d36583b6b62685d2 (diff) | |
Add support for external images
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/docx/body-reader.tests.js | 42 | ||||
| -rw-r--r-- | tests/mammoth.tests.js | 8 | ||||
| -rw-r--r-- | tests/test-data/external-picture.docx | bin | 0 -> 4120 bytes | |||
| -rw-r--r-- | tests/testing.js | 28 |
4 files changed, 66 insertions, 12 deletions
diff --git a/tests/docx/body-reader.tests.js b/tests/docx/body-reader.tests.js index ad2caa2..2e85d76 100644 --- a/tests/docx/body-reader.tests.js +++ b/tests/docx/body-reader.tests.js @@ -286,7 +286,7 @@ describe("readXmlElement: ", function() { test("can read inline pictures", function() { var drawing = createInlineImage({ - relationshipId: "rId5", + blip: createEmbeddedBlip("rId5"), description: "It's a hat" }); @@ -349,9 +349,37 @@ describe("readXmlElement: ", function() { }); }); + test("can read linked pictures", function() { + var drawing = createInlineImage({ + blip: createLinkedBlip("rId5"), + description: "It's a hat" + }); + + var imageBuffer = new Buffer("Not an image at all!"); + var reader = new BodyReader({ + relationships: { + "rId5": {target: "file:///media/hat.png"} + }, + contentTypes: fakeContentTypes, + files: testing.createFakeFiles({ + "file:///media/hat.png": imageBuffer + }) + }); + var result = reader.readXmlElement(drawing); + assert.deepEqual(result.messages, []); + var element = single(result.value); + assert.equal("image", element.type); + assert.equal(element.altText, "It's a hat"); + assert.equal(element.contentType, "image/png"); + return element.read() + .then(function(readValue) { + assert.equal(readValue, imageBuffer); + }); + }); + test("warning if unsupported image type", function() { var drawing = createInlineImage({ - relationshipId: "rId5", + blip: createEmbeddedBlip("rId5"), description: "It's a hat" }); @@ -514,7 +542,7 @@ function createInlineImage(options) { new XmlElement("a:graphicData", {}, [ new XmlElement("pic:pic", {}, [ new XmlElement("pic:blipFill", {}, [ - new XmlElement("a:blip", {"r:embed": options.relationshipId}) + options.blip ]) ]) ]) @@ -522,3 +550,11 @@ function createInlineImage(options) { ]) ]); } + +function createEmbeddedBlip(relationshipId) { + return new XmlElement("a:blip", {"r:embed": relationshipId}); +} + +function createLinkedBlip(relationshipId) { + return new XmlElement("a:blip", {"r:link": relationshipId}); +} diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js index b7f4766..bb3dca2 100644 --- a/tests/mammoth.tests.js +++ b/tests/mammoth.tests.js @@ -184,6 +184,14 @@ describe('mammoth', function() { }); }); + test('images stored outside of document are included in output', function() { + var docxPath = path.join(__dirname, "test-data/external-picture.docx"); + return mammoth.convertToHtml({path: docxPath}).then(function(result) { + assert.equal(result.value, '<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>'); + assert.deepEqual([], result.messages); + }); + }); + test('simple list is converted to list elements', function() { var docxPath = path.join(__dirname, "test-data/simple-list.docx"); return mammoth.convertToHtml({path: docxPath}).then(function(result) { diff --git a/tests/test-data/external-picture.docx b/tests/test-data/external-picture.docx Binary files differnew file mode 100644 index 0000000..3475626 --- /dev/null +++ b/tests/test-data/external-picture.docx diff --git a/tests/testing.js b/tests/testing.js index b8757c9..d70f86e 100644 --- a/tests/testing.js +++ b/tests/testing.js @@ -7,6 +7,7 @@ exports.test = test; exports.testPath = testPath; exports.testData = testData; exports.createFakeDocxFile = createFakeDocxFile; +exports.createFakeFiles = createFakeFiles; function test(name, func) { @@ -28,6 +29,23 @@ function testData(testDataPath) { } function createFakeDocxFile(files) { + function exists(path) { + return !!files[path]; + } + + return { + read: createRead(files), + exists: exists + }; +} + +function createFakeFiles(files) { + return { + read: createRead(files) + }; +} + +function createRead(files) { function read(path, encoding) { return promises.when(files[path], function(buffer) { if (_.isString(buffer)) { @@ -43,13 +61,5 @@ function createFakeDocxFile(files) { } }); } - - function exists(path) { - return !!files[path]; - } - - return { - read: read, - exists: exists - }; + return read; } |
