117 - Decode HTML entities in extracted thumbnail URLs

This commit is contained in:
myrmidex 2026-08-01 17:13:32 +02:00
parent 299ec3151a
commit 253700312e
6 changed files with 131 additions and 6 deletions

View file

@ -110,12 +110,12 @@ public static function extractThumbnail(string $html): ?string
{
// Try OpenGraph image first
if (preg_match('/<meta property="og:image" content="([^"]+)"/i', $html, $matches)) {
return $matches[1];
return html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8');
}
// Try first image in article content
if (preg_match('/<img[^>]+src="([^"]+)"/i', $html, $matches)) {
return $matches[1];
return html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8');
}
return null;

View file

@ -68,12 +68,12 @@ public static function extractThumbnail(string $html): ?string
{
// Try OpenGraph image first
if (preg_match('/<meta property="og:image" content="([^"]+)"/i', $html, $matches)) {
return $matches[1];
return html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8');
}
// Try first image in content
if (preg_match('/<img[^>]+src="([^"]+)"/i', $html, $matches)) {
return $matches[1];
return html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8');
}
return null;

View file

@ -67,11 +67,11 @@ public static function extractFullArticle(string $html): ?string
public static function extractThumbnail(string $html): ?string
{
if (preg_match('/<meta property="og:image" content="([^"]+)"/i', $html, $matches)) {
return $matches[1];
return html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8');
}
if (preg_match('/<img[^>]+src="([^"]+)"/i', $html, $matches)) {
return $matches[1];
return html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8');
}
return null;

View file

@ -242,6 +242,36 @@ public function test_extract_thumbnail_prefers_og_image(): void
$this->assertEquals('https://example.com/og-image.jpg', $thumbnail);
}
public function test_extract_thumbnail_decodes_html_entities_in_og_image(): void
{
// Real Belga CDN URLs carry query params, so the &amp; in the raw HTML
// must be decoded or the URL Lemmy receives is malformed. See #117.
$html = '<html><head><meta property="og:image" content="https://cdn.example.com/img:12345:full?v=6a6c828f&amp;m=cdodnjha"/></head></html>';
$thumbnail = BelgaArticlePageParser::extractThumbnail($html);
$this->assertEquals('https://cdn.example.com/img:12345:full?v=6a6c828f&m=cdodnjha', $thumbnail);
$this->assertStringNotContainsString('&amp;', (string) $thumbnail);
}
public function test_extract_thumbnail_decodes_html_entities_in_img_tag(): void
{
$html = '<html><body><img src="https://cdn.example.com/pic.png?a=1&amp;b=2" alt="test"/></body></html>';
$thumbnail = BelgaArticlePageParser::extractThumbnail($html);
$this->assertEquals('https://cdn.example.com/pic.png?a=1&b=2', $thumbnail);
}
public function test_extract_data_returns_decoded_thumbnail(): void
{
$html = '<html><head><meta property="og:image" content="https://cdn.example.com/x.jpg?v=1&amp;m=2"/></head></html>';
$data = BelgaArticlePageParser::extractData($html);
$this->assertEquals('https://cdn.example.com/x.jpg?v=1&m=2', $data['thumbnail']);
}
public function test_extract_thumbnail_returns_null_when_not_found(): void
{
$html = '<html><body><div>No images here</div></body></html>';

View file

@ -282,4 +282,24 @@ public function test_extract_full_article_with_realistic_guardian_html(): void
$this->assertStringContainsString("\n\n", $fullArticle);
$this->assertStringNotContainsString('<strong>', $fullArticle);
}
public function test_extract_thumbnail_decodes_html_entities(): void
{
// Entity-encoded ampersands make the URL unusable downstream. See #117.
$html = '<html><head><meta property="og:image" content="https://i.guim.co.uk/img/media/abc.jpg?width=1200&amp;quality=85"/></head></html>';
$thumbnail = GuardianArticlePageParser::extractThumbnail($html);
$this->assertEquals('https://i.guim.co.uk/img/media/abc.jpg?width=1200&quality=85', $thumbnail);
$this->assertStringNotContainsString('&amp;', (string) $thumbnail);
}
public function test_extract_thumbnail_decodes_html_entities_in_img_tag(): void
{
$html = '<html><body><img src="https://i.guim.co.uk/img/pic.png?a=1&amp;b=2" alt="test"/></body></html>';
$thumbnail = GuardianArticlePageParser::extractThumbnail($html);
$this->assertEquals('https://i.guim.co.uk/img/pic.png?a=1&b=2', $thumbnail);
}
}

View file

@ -0,0 +1,75 @@
<?php
namespace Tests\Unit\Services\Parsers;
use App\Services\Parsers\VrtArticlePageParser;
use Tests\TestCase;
class VrtArticlePageParserTest extends TestCase
{
public function test_extract_thumbnail_from_og_image(): void
{
$html = '<html><head><meta property="og:image" content="https://images.vrt.be/image.jpg"/></head></html>';
$this->assertEquals(
'https://images.vrt.be/image.jpg',
VrtArticlePageParser::extractThumbnail($html)
);
}
public function test_extract_thumbnail_falls_back_to_img_tag(): void
{
$html = '<html><body><img src="https://images.vrt.be/body.png" alt="test"/></body></html>';
$this->assertEquals(
'https://images.vrt.be/body.png',
VrtArticlePageParser::extractThumbnail($html)
);
}
public function test_extract_thumbnail_prefers_og_image(): void
{
$html = '<html><head><meta property="og:image" content="https://images.vrt.be/og.jpg"/></head>'
.'<body><img src="https://images.vrt.be/body.png" alt="test"/></body></html>';
$this->assertEquals(
'https://images.vrt.be/og.jpg',
VrtArticlePageParser::extractThumbnail($html)
);
}
public function test_extract_thumbnail_returns_null_when_not_found(): void
{
$this->assertNull(VrtArticlePageParser::extractThumbnail('<html><body>No images</body></html>'));
}
public function test_extract_thumbnail_decodes_html_entities(): void
{
// Entity-encoded ampersands make the URL unusable downstream. See #117.
$html = '<html><head><meta property="og:image" content="https://images.vrt.be/orig.jpg?crop=1&amp;w=1200"/></head></html>';
$thumbnail = VrtArticlePageParser::extractThumbnail($html);
$this->assertEquals('https://images.vrt.be/orig.jpg?crop=1&w=1200', $thumbnail);
$this->assertStringNotContainsString('&amp;', (string) $thumbnail);
}
public function test_extract_thumbnail_decodes_html_entities_in_img_tag(): void
{
$html = '<html><body><img src="https://images.vrt.be/pic.png?a=1&amp;b=2" alt="test"/></body></html>';
$this->assertEquals(
'https://images.vrt.be/pic.png?a=1&b=2',
VrtArticlePageParser::extractThumbnail($html)
);
}
public function test_extract_data_returns_decoded_thumbnail(): void
{
$html = '<html><head><meta property="og:image" content="https://images.vrt.be/x.jpg?v=1&amp;m=2"/></head></html>';
$data = VrtArticlePageParser::extractData($html);
$this->assertEquals('https://images.vrt.be/x.jpg?v=1&m=2', $data['thumbnail']);
}
}