From 0940971069290b5bce4dc9422d8b4d0d20f7d3b5 Mon Sep 17 00:00:00 2001 From: Toufic Mouallem Date: Sat, 9 Mar 2019 01:47:17 +0200 Subject: [PATCH] fix: better handling for responsive images (#312) --- src/resource/utils/dom/constants.js | 4 +- .../utils/dom/convert-lazy-loaded-images.js | 11 +++++- .../dom/convert-lazy-loaded-images.test.js | 38 +++++++++++++++++++ src/utils/dom/constants.js | 2 + 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/resource/utils/dom/constants.js b/src/resource/utils/dom/constants.js index 25385bbd..13902a08 100644 --- a/src/resource/utils/dom/constants.js +++ b/src/resource/utils/dom/constants.js @@ -1,4 +1,6 @@ export const IS_LINK = new RegExp('https?://', 'i'); -export const IS_IMAGE = new RegExp('.(png|gif|jpe?g)', 'i'); +const IMAGE_RE = '.(png|gif|jpe?g)'; +export const IS_IMAGE = new RegExp(`${IMAGE_RE}`, 'i'); +export const IS_SRCSET = new RegExp(`${IMAGE_RE}(\\s*[\\d.]+[wx])`, 'i'); export const TAGS_TO_REMOVE = ['script', 'style', 'form'].join(','); diff --git a/src/resource/utils/dom/convert-lazy-loaded-images.js b/src/resource/utils/dom/convert-lazy-loaded-images.js index 506e51ff..03eb6340 100644 --- a/src/resource/utils/dom/convert-lazy-loaded-images.js +++ b/src/resource/utils/dom/convert-lazy-loaded-images.js @@ -1,6 +1,6 @@ import { getAttrs } from 'utils/dom'; -import { IS_LINK, IS_IMAGE } from './constants'; +import { IS_LINK, IS_IMAGE, IS_SRCSET } from './constants'; // Convert all instances of images with potentially // lazy loaded images into normal images. @@ -14,7 +14,14 @@ export default function convertLazyLoadedImages($) { Reflect.ownKeys(attrs).forEach(attr => { const value = attrs[attr]; - if (attr !== 'src' && IS_LINK.test(value) && IS_IMAGE.test(value)) { + if (attr !== 'srcset' && IS_LINK.test(value) && IS_SRCSET.test(value)) { + $(img).attr('srcset', value); + } else if ( + attr !== 'src' && + attr !== 'srcset' && + IS_LINK.test(value) && + IS_IMAGE.test(value) + ) { $(img).attr('src', value); } }); diff --git a/src/resource/utils/dom/convert-lazy-loaded-images.test.js b/src/resource/utils/dom/convert-lazy-loaded-images.test.js index 496fa113..33639e9c 100644 --- a/src/resource/utils/dom/convert-lazy-loaded-images.test.js +++ b/src/resource/utils/dom/convert-lazy-loaded-images.test.js @@ -16,6 +16,31 @@ describe('convertLazyLoadedImages($)', () => { ); }); + it('moves image source candidates to srcset if placed in another attribute', () => { + const html = ''; + const $ = cheerio.load(html); + + const result = convertLazyLoadedImages($).html(); + + assert.equal( + result, + '' + ); + }); + + it('properly handles src and srcset attributes', () => { + const html = + ''; + const $ = cheerio.load(html); + + const result = convertLazyLoadedImages($).html(); + + assert.equal( + result, + '' + ); + }); + it('does nothing when value is not a link', () => { // This is far from perfect, since a relative url could // be perfectly correct. @@ -44,4 +69,17 @@ describe('convertLazyLoadedImages($)', () => { assert.equal(result, ''); }); + + it('does not replace an img src with srcset value', () => { + const html = + ''; + const $ = cheerio.load(html); + + const result = convertLazyLoadedImages($).html(); + + assert.equal( + result, + '' + ); + }); }); diff --git a/src/utils/dom/constants.js b/src/utils/dom/constants.js index 064ee963..b664a857 100644 --- a/src/utils/dom/constants.js +++ b/src/utils/dom/constants.js @@ -36,6 +36,8 @@ export const REMOVE_ATTR_LIST = REMOVE_ATTRS.join(','); export const WHITELIST_ATTRS = [ 'src', 'srcset', + 'sizes', + 'type', 'href', 'class', 'id',