0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Updated gallery renderer to cope with invalid images

no issue
- skip rendering images that do not have all required fields
- do not render anything if there are no valid images
This commit is contained in:
Kevin Ansfield 2018-08-31 11:13:24 +01:00
parent d29e376367
commit bba3049106
2 changed files with 63 additions and 2 deletions

View file

@ -25,7 +25,20 @@ module.exports = {
// let version = opts.options.version;
let dom = opts.env.dom;
if (!payload.images || payload.images.length === 0) {
let isValidImage = (image) => {
return image.fileName
&& image.src
&& image.width
&& image.height;
};
let validImages = [];
if (payload.images && payload.images.length) {
validImages = payload.images.filter(isValidImage);
}
if (validImages.length === 0) {
return '';
}
@ -56,7 +69,7 @@ module.exports = {
return rows;
};
let rows = buildStructure(payload.images);
let rows = buildStructure(validImages);
rows.forEach((row) => {
let rowDiv = dom.createElement('div');

View file

@ -90,4 +90,52 @@ describe('Gallery card', function () {
serializer.serialize(card.render(opts)).should.eql('');
});
it('renders nothing with no valid images', function () {
let opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
images: [{src: 'undefined'}],
caption: 'Test caption'
}
};
serializer.serialize(card.render(opts)).should.eql('');
});
it('skips invalid images', function () {
let opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
images: [
{
row: 0,
fileName: 'NatGeo01.jpg',
src: '/content/images/2018/08/NatGeo01-9.jpg',
width: 3200,
height: 1600
},
{
row: 0,
fileName: 'NatGeo02.jpg',
src: '/content/images/2018/08/NatGeo02-10.jpg',
},
{
row: 0,
fileName: 'NatGeo03.jpg',
src: '/content/images/2018/08/NatGeo03-6.jpg',
width: 3200,
height: 1600
}
],
caption: 'Test caption'
}
};
serializer.serialize(card.render(opts)).should.eql('<figure class="kg-card kg-gallery-card kg-width-wide"><div class="kg-gallery-container"><div class="kg-gallery-row"><div class="kg-gallery-image"><img src="/content/images/2018/08/NatGeo01-9.jpg" width="3200" height="1600"></div><div class="kg-gallery-image"><img src="/content/images/2018/08/NatGeo03-6.jpg" width="3200" height="1600"></div></div></div><figcaption>Test caption</figcaption></figure>');
});
});