Skip to content

Commit

Permalink
Only use the alt text of image elements as a fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Aug 31, 2022
1 parent d67ddaf commit 62ccce2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 3 additions & 1 deletion lib/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
});
};
Expand Down
25 changes: 25 additions & 0 deletions test/images.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>"})
})
})
));
});
}
});

0 comments on commit 62ccce2

Please sign in to comment.