diff options
| author | Michael Williamson <mike@zwobble.org> | 2022-08-31 21:20:01 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2022-08-31 21:20:01 +0100 |
| commit | 62ccce26f2b44ecd5df06670d0e935d7d57d7d4d (patch) | |
| tree | 280c8ab542e06a441374f147d84bcd93ff41bec1 | |
| parent | d67ddaff3c05da19012a1daff584a0f33ff5df83 (diff) | |
Only use the alt text of image elements as a fallback
| -rw-r--r-- | NEWS | 6 | ||||
| -rw-r--r-- | lib/images.js | 4 | ||||
| -rw-r--r-- | test/images.tests.js | 25 |
3 files changed, 34 insertions, 1 deletions
@@ -1,3 +1,9 @@ +# 1.5.0 + +* Only use the alt text of image elements as a fallback. If an alt attribute is + returned from the function passed to mammoth.images.imgElement, that value + will now be preferred to the alt text of the image element. + # 1.4.21 * Ignore w:u elements when w:val is missing. diff --git a/lib/images.js b/lib/images.js index c7d3ef5..fc45115 100644 --- a/lib/images.js +++ b/lib/images.js @@ -8,10 +8,12 @@ exports.imgElement = imgElement; function imgElement(func) { return function(element, messages) { return promises.when(func(element)).then(function(result) { - var attributes = _.clone(result); + var attributes = {}; if (element.altText) { attributes.alt = element.altText; } + _.extend(attributes, result); + return [Html.freshElement("img", attributes)]; }); }; diff --git a/test/images.tests.js b/test/images.tests.js index 9a9344d..cd8d51b 100644 --- a/test/images.tests.js +++ b/test/images.tests.js @@ -83,5 +83,30 @@ test('mammoth.images.imgElement()', { }) )); }); + }, + + 'image alt text can be overriden by alt attribute returned from function': function() { + var imageBuffer = new Buffer("abc"); + var image = new documents.Image({ + readImage: function(encoding) { + return promises.when(imageBuffer.toString(encoding)); + }, + contentType: "image/jpeg", + altText: "<alt>" + }); + + var result = mammoth.images.imgElement(function(image) { + return {alt: "<alt override>", src: "<src>"}; + })(image); + + return result.then(function(result) { + assertThat(result, contains( + hasProperties({ + tag: hasProperties({ + attributes: equalTo({alt: "<alt override>", src: "<src>"}) + }) + }) + )); + }); } }); |
