Skip to content

Commit

Permalink
ContentExtractor: fix handling of data-srcset
Browse files Browse the repository at this point in the history
The extractor was incorrectly setting the value of data-srcset into the
src attribute, however srcset does not use the same format leading to
loading error with incorrect paths. Now data-srcset sets the value of
srcset which will let the browser to use paths of either src or srcset
attributes.

Signed-off-by: Kevin Decherf <kevin@kdecherf.com>
  • Loading branch information
Kdecherf committed Jan 19, 2021
1 parent 987aa6c commit 20d6250
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Extractor/ContentExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,16 +607,23 @@ function ($element, $currentEntity) {
continue;
}

$src = null;
$attributes = [];
foreach ($this->config['src_lazy_load_attributes'] as $attribute) {
if ($e->hasAttribute($attribute)) {
$src = $e->getAttribute($attribute);
$key = 'src';
if ('data-srcset' === $attribute) {
$key = 'srcset';
}
$attributes[$key] = $e->getAttribute($attribute);
$e->removeAttribute($attribute);
}
}

if (null !== $src) {
$e->setAttribute('src', $src);
foreach (['src', 'srcset'] as $attr) {
if (\array_key_exists($attr, $attributes)
&& null !== $attributes[$attr]) {
$e->setAttribute($attr, $attributes[$attr]);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Extractor/ContentExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,11 @@ public function dataForlazyLoad(): array
'<div>' . str_repeat('this is the best part of the show', 10) . '<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-toto-src="http://0.0.0.0/big_image.jpg"/></div>',
'<img src="http://0.0.0.0/big_image.jpg"',
],
// test with img attribute data-srcset
[
'<div>' . str_repeat('this is the best part of the show', 10) . '<img data-src="http://0.0.0.0/src.jpg" data-srcset="http://0.0.0.0/srcset1 680w, http://0.0.0.0/srcset2 1536w"/></div>',
'<img src="http://0.0.0.0/src.jpg" srcset="http://0.0.0.0/srcset1 680w, http://0.0.0.0/srcset2 1536w"/>',
],
];
}

Expand Down

0 comments on commit 20d6250

Please sign in to comment.