Skip to content

Commit 57d5d9f

Browse files
authored
fix: correctly pass img_attrs (#224)
1 parent f3370e6 commit 57d5d9f

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

phpstan.baseline.neon

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,13 @@ parameters:
357357
-
358358
message: '#^Cannot access offset ''alt'' on mixed\.$#'
359359
identifier: offsetAccess.nonOffsetAccessible
360-
count: 1
360+
count: 2
361361
path: src/Bridge/Twig/Extension/ImageExtension.php
362362

363363
-
364364
message: '#^Cannot access offset ''class'' on mixed\.$#'
365365
identifier: offsetAccess.nonOffsetAccessible
366-
count: 1
366+
count: 2
367367
path: src/Bridge/Twig/Extension/ImageExtension.php
368368

369369
-
@@ -375,13 +375,13 @@ parameters:
375375
-
376376
message: '#^Cannot access offset ''loading'' on mixed\.$#'
377377
identifier: offsetAccess.nonOffsetAccessible
378-
count: 1
378+
count: 3
379379
path: src/Bridge/Twig/Extension/ImageExtension.php
380380

381381
-
382382
message: '#^Cannot access offset ''style'' on mixed\.$#'
383383
identifier: offsetAccess.nonOffsetAccessible
384-
count: 1
384+
count: 2
385385
path: src/Bridge/Twig/Extension/ImageExtension.php
386386

387387
-
@@ -402,6 +402,12 @@ parameters:
402402
count: 1
403403
path: src/Bridge/Twig/Extension/ImageExtension.php
404404

405+
-
406+
message: '#^Only booleans are allowed in a ternary operator condition, array\<string, mixed\> given\.$#'
407+
identifier: ternary.condNotBoolean
408+
count: 1
409+
path: src/Bridge/Twig/Extension/ImageExtension.php
410+
405411
-
406412
message: '#^Only booleans are allowed in a ternary operator condition, string\|null given\.$#'
407413
identifier: ternary.condNotBoolean

psalm.baseline.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,10 @@
462462
<code><![CDATA[\is_string($value)]]></code>
463463
</DocblockTypeContradiction>
464464
<InvalidArrayAccess>
465-
<code><![CDATA[$options['attributes']['class']]]></code>
465+
<code><![CDATA[$options['attrs']['class']]]></code>
466466
</InvalidArrayAccess>
467467
<InvalidArrayOffset>
468-
<code><![CDATA[$options['attributes']['class']]]></code>
468+
<code><![CDATA[$options['attrs']['class']]]></code>
469469
</InvalidArrayOffset>
470470
<InvalidCast>
471471
<code><![CDATA[$options['height']]]></code>

src/Bridge/Twig/Extension/ImageExtension.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,23 @@ function (array $context, string $path, array $options = []): string {
8484

8585
// Build <picture> element
8686
$attributes = array_filter([
87-
'class' => $options['attributes']['class'] ?? null,
88-
'style' => $options['attributes']['style'] ?? null,
87+
'class' => $options['attrs']['class'] ?? null,
88+
'style' => $options['attrs']['style'] ?? null,
8989
]);
9090

9191
$imgAttributes = array_filter([
9292
'src' => $srcFallback1x,
9393
'srcset' => $srcsetFallback,
9494
'width' => $width !== null ? (string) $width : null,
9595
'height' => $height !== null ? (string) $height : null,
96-
'alt' => $options['attributes']['alt'] ?? '',
97-
'loading' => $options['attributes']['loading'] ?? 'lazy',
98-
'decoding' => $options['attributes']['decoding'] ?? 'async',
96+
'class' => $options['img_attrs']['class'] ?? null,
97+
'style' => $options['img_attrs']['style'] ?? null,
98+
'alt' => $options['img_attrs']['alt'] ?? $options['attrs']['alt'] ?? null,
99+
'loading' => $options['img_attrs']['loading'] ?? $options['attrs']['loading'] ?? 'lazy',
100+
'decoding' => $options['img_attrs']['loading'] ?? $options['attrs']['decoding'] ?? 'async',
99101
]);
100102

101-
$html = '<picture '.implode(' ', array_map(
103+
$html = '<picture'.($attributes ? ' ' : '').implode(' ', array_map(
102104
static fn ($key, $value) => htmlspecialchars($key, \ENT_QUOTES).'="'.htmlspecialchars($value, \ENT_QUOTES).'"',
103105
array_keys($attributes),
104106
$attributes
@@ -204,7 +206,7 @@ private function buildOriginAbsolutePath(string $path, array $context, array $op
204206
private function buildImgproxyFilter(array $options): string
205207
{
206208
$filter = '';
207-
unset($options['self'], $options['attributes']);
209+
unset($options['self'], $options['attrs'], $options['img_attrs']);
208210

209211
if ($options !== []) {
210212
$filters = [];

tests/functional/site/content/articles/images/images.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ This is an asset lookup: {{ yassg_thumbnail(item.image) }}
1919

2020
![Logo]({{ yassg_thumbnail(item.image, {width: 400, height: 200, gravity: "no"}) }})
2121

22-
{{ yassg_picture(item.image, {width: 400, height: 200, gravity: "no", attributes: {alt: "Hello", class: "prose", style: "border: 1px solid red"}}) }}
22+
{{ yassg_picture(item.image, {width: 400, height: 200, gravity: "no", attrs: {alt: "Hello", class: "prose", style: "border: 1px solid red"}, img_attrs: {class: "mb-rounded"}}) }}

tests/functional/site/fixtures/en/article/images/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ <h1>Images!</h1>
4646
<p>This is an asset lookup: /sub/dir/another/content/articles/images/image.6ec7e8a4.webp</p>
4747
<p><img src="/sub/dir/another/content/articles/images/image.4de2c22b.webp" alt="Logo" /></p>
4848
<p><img src="/sub/dir/another/content/articles/images/image.5c87cfff.webp" alt="Logo" /></p>
49-
<p><picture class="prose" style="border: 1px solid red"><source type="image/avif" srcset="/sub/dir/another/content/articles/images/image.f72ab4b6.avif 1x, /sub/dir/another/content/articles/images/image.db2a185c.avif 2x"/><source type="image/webp" srcset="/sub/dir/another/content/articles/images/image.5c87cfff.webp 1x, /sub/dir/another/content/articles/images/image.3fc9b4bd.webp 2x"/><source type="image/png" srcset="/sub/dir/another/content/articles/images/image.86315aa7.png 1x, /sub/dir/another/content/articles/images/image.6d24603a.png 2x"/><img src="/sub/dir/another/content/articles/images/image.86315aa7.png" srcset="/sub/dir/another/content/articles/images/image.86315aa7.png 1x, /sub/dir/another/content/articles/images/image.6d24603a.png 2x" width="400" height="200" alt="Hello" loading="lazy" decoding="async" /></picture></p>
49+
<p><picture class="prose" style="border: 1px solid red"><source type="image/avif" srcset="/sub/dir/another/content/articles/images/image.f72ab4b6.avif 1x, /sub/dir/another/content/articles/images/image.db2a185c.avif 2x"/><source type="image/webp" srcset="/sub/dir/another/content/articles/images/image.5c87cfff.webp 1x, /sub/dir/another/content/articles/images/image.3fc9b4bd.webp 2x"/><source type="image/png" srcset="/sub/dir/another/content/articles/images/image.86315aa7.png 1x, /sub/dir/another/content/articles/images/image.6d24603a.png 2x"/><img src="/sub/dir/another/content/articles/images/image.86315aa7.png" srcset="/sub/dir/another/content/articles/images/image.86315aa7.png 1x, /sub/dir/another/content/articles/images/image.6d24603a.png 2x" width="400" height="200" class="mb-rounded" alt="Hello" loading="lazy" decoding="async" /></picture></p>
5050

5151
</div>
5252
</div>

0 commit comments

Comments
 (0)