Add support for Twitch CDN URLs in picture tag, returning simple img tag

This commit is contained in:
Joakim Hellsén 2026-02-21 06:09:45 +01:00
commit b6699f3a2a
Signed by: Joakim Hellsén
SSH key fingerprint: SHA256:/9h/CsExpFp+PRhsfA0xznFx2CGfTT5R/kpuFfUgEQk
2 changed files with 66 additions and 0 deletions

View file

@ -72,6 +72,19 @@ def picture( # noqa: PLR0913, PLR0917
if not src: if not src:
return SafeString("") 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='<img src="{src}"{width}{height}{loading}{css_class}{style}{alt} />',
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 # Generate URLs for modern formats
avif_url: str = get_format_url(src, "avif") avif_url: str = get_format_url(src, "avif")
webp_url: str = get_format_url(src, "webp") webp_url: str = get_format_url(src, "webp")

View file

@ -185,6 +185,59 @@ class TestPictureTag:
assert "https://cdn.example.com/images/photo.webp" in result assert "https://cdn.example.com/images/photo.webp" in result
assert "https://cdn.example.com/images/photo.jpg" 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 "<picture>" not in result
assert "</picture>" not in result
# Should NOT have source tags for format conversion
assert "<source" not in result
# Should have simple img tag
assert 'src="https://static-cdn.jtvnw.net/ttv-boxart/1292861145.jpg"' in result
assert 'loading="lazy"' in result
def test_twitch_cdn_url_with_attributes(self) -> 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 "<picture>" not in result
assert "</picture>" not in result
assert "<source" not in result
# Should have all attributes
assert 'src="https://static-cdn.jtvnw.net/ttv-boxart/1292861145.jpg"' in result
assert 'alt="Game art"' in result
assert 'width="300"' in result
assert 'height="400"' in result
assert 'loading="eager"' in result
assert 'class="game-cover"' in result
assert 'style="border-radius: 8px"' in result
def test_twitch_cdn_url_with_png(self) -> 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 "<picture>" not in result
assert "</picture>" not in result
assert "<source" not in result
# Should have simple img tag
assert 'src="https://static-cdn.jtvnw.net/badges/v1/1234567.png"' in result
class TestPictureTagTemplate: class TestPictureTagTemplate:
"""Tests for the picture tag used in templates.""" """Tests for the picture tag used in templates."""