Skip to content

Commit

Permalink
fix: don't rewrite srcset attribute when rewriting image urls
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Nov 29, 2022
1 parent 0875f9d commit fd3cd2f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ public function rewriteContentImageUrls(string $content): string
return $image['height'] || $image['width'];
})->mapWithKeys(function (array $image) {
$src = $this->getOriginalImageUrl($image['image_src']);
$tag = str_replace($image['image_src'], esc_url($this->generateImageUrl($src, $image['height'], $image['width'], $image['cropped'])), $image['image_tag']);

$tag = preg_replace('#(src=["|\'])[^\s]+?(["|\'])#', sprintf('$1%s$2', esc_url($this->generateImageUrl($src, $image['height'], $image['width'], $image['cropped']))), $image['image_tag']);
$tag = preg_replace('#(?<=\s)(height|width)=["|\']?[\d%]+["|\']?\s?#i', '', $tag);

return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,22 @@ public function testReEnableImageDownsizeFilterIfFlagWasTrue()
$this->assertTrue($imageDownsizeFilterEnabledProperty->getValue($subscriber));
}

public function testRewriteContentImageUrlsDoesntRewriteSrcsetUrls()
{
$content = '<img width="205" height="112" src="https://assets.com/uploads/image.jpg" srcset="https://assets.com/uploads/image.jpg?width=2048 2048w, https://assets.com/uploads/image.jpg?height=164&amp;width=300 300w, https://assets.com/uploads/image.jpg?height=560&amp;width=1024 1024w, https://assets.com/uploads/image.jpg?height=420&amp;width=768 768w" />';
$expectedContent = '<img src="https://assets.com/uploads/image.jpg?height=112&width=205&cropped" srcset="https://assets.com/uploads/image.jpg?width=2048 2048w, https://assets.com/uploads/image.jpg?height=164&amp;width=300 300w, https://assets.com/uploads/image.jpg?height=560&amp;width=1024 1024w, https://assets.com/uploads/image.jpg?height=420&amp;width=768 768w" />';

$_prime_post_caches = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), '_prime_post_caches');
$_prime_post_caches->expects($this->once())
->with($this->identicalTo([]), $this->identicalTo(false));

$esc_url = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'esc_url');
$esc_url->expects($this->any())
->willReturn($this->returnArgument(0));

$this->assertSame($expectedContent, (new ContentDeliveryNetworkImageProcessingSubscriber($this->getImageSizes(), true, 'https://assets.com/uploads'))->rewriteContentImageUrls($content));
}

public function testRewriteContentImageUrlsDoesntRewriteUrlIfUploadsUrlDoesntMatch()
{
$content = '<img class="alignnone size-thumbnail" src="https://domain.com/uploads/image-150x150.jpg" alt="" />';
Expand Down

0 comments on commit fd3cd2f

Please sign in to comment.