From b6699f3a2a5205f78c50d24c535636c14aa6c383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Sat, 21 Feb 2026 06:09:45 +0100 Subject: [PATCH] Add support for Twitch CDN URLs in picture tag, returning simple img tag --- twitch/templatetags/image_tags.py | 13 ++++++++ twitch/tests/test_image_tags.py | 53 +++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/twitch/templatetags/image_tags.py b/twitch/templatetags/image_tags.py index 774d867..ae3ccee 100644 --- a/twitch/templatetags/image_tags.py +++ b/twitch/templatetags/image_tags.py @@ -72,6 +72,19 @@ def picture( # noqa: PLR0913, PLR0917 if not src: return SafeString("") + # For Twitch CDN URLs, skip format conversion and use simple img tag + if "static-cdn.jtvnw.net" in src: + return format_html( + format_string='', + src=src, + width=format_html(' width="{}"', width) if width else "", + height=format_html(' height="{}"', height) if height else "", + loading=format_html(' loading="{}"', loading) if loading else "", + css_class=format_html(' class="{}"', css_class) if css_class else "", + style=format_html(' style="{}"', style) if style else "", + alt=format_html(' alt="{}"', alt) if alt is not None else "", + ) + # Generate URLs for modern formats avif_url: str = get_format_url(src, "avif") webp_url: str = get_format_url(src, "webp") diff --git a/twitch/tests/test_image_tags.py b/twitch/tests/test_image_tags.py index 8ad3042..dc29fe9 100644 --- a/twitch/tests/test_image_tags.py +++ b/twitch/tests/test_image_tags.py @@ -185,6 +185,59 @@ class TestPictureTag: assert "https://cdn.example.com/images/photo.webp" in result assert "https://cdn.example.com/images/photo.jpg" in result + def test_twitch_cdn_url_simple_img(self) -> None: + """Test that Twitch CDN URLs return simple img tag without picture element.""" + result: SafeString = picture("https://static-cdn.jtvnw.net/ttv-boxart/1292861145.jpg") + + # Should NOT have picture element + assert "" not in result + assert "" not in result + + # Should NOT have source tags for format conversion + assert " None: + """Test Twitch CDN URL with optional attributes.""" + result: SafeString = picture( + "https://static-cdn.jtvnw.net/ttv-boxart/1292861145.jpg", + alt="Game art", + width=300, + height=400, + loading="eager", + css_class="game-cover", + style="border-radius: 8px", + ) + + # Should still be simple img tag + assert "" not in result + assert "" not in result + assert " None: + """Test Twitch CDN URL with PNG format.""" + result: SafeString = picture("https://static-cdn.jtvnw.net/badges/v1/1234567.png") + + # Should NOT have picture element or source tags + assert "" not in result + assert "" not in result + assert "