Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Bridges/AssetsLatte/Nodes/NAssetNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final class NAssetNode extends StatementNode
public ArrayNode $attributes;
public AreaNode $content;
public bool $optional;
private string $tmpVar;


/** @return \Generator<int, ?list<string>, array{AreaNode, ?Tag}, static> */
Expand All @@ -56,12 +57,15 @@ public static function create(Tag $tag): \Generator

public function print(PrintContext $context): string
{
$this->tmpVar = '$ʟ_asset_' . $context->generateId();

return $context->format(
<<<'XX'
if ($ʟ_tmp = $this->global->assets->resolve(%node, %node, %dump)) %line {
if (%raw = $this->global->assets->resolve(%node, %node, %dump)) %line {
%node
}
XX,
$this->tmpVar,
$this->name,
$this->attributes,
$this->optional,
Expand All @@ -77,8 +81,9 @@ private function init(Tag $tag): void
assert($el !== null);
$tag->replaceNAttribute(new AuxiliaryNode(fn(PrintContext $context) => $context->format(
<<<'XX'
echo $this->global->assets->renderAttributes($ʟ_tmp, %dump, %dump);
echo $this->global->assets->renderAttributes(%raw, %dump, %dump);
XX,
$this->tmpVar,
strtolower($el->name),
self::findUsedAttributes($el),
)));
Expand Down
82 changes: 82 additions & 0 deletions tests/Bridges.Latte/n-asset-n-class.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php declare(strict_types=1);

use Nette\Assets\ImageAsset;
use Nette\Assets\Registry;
use Nette\Bridges\AssetsLatte\LatteExtension;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


test('n:class before n:asset on the same element does not clobber the resolved asset', function () {
$asset = new ImageAsset(
url: 'https://example.com/image.jpg',
width: 800,
height: 600,
);

$mockRegistry = Mockery::mock(Registry::class);
$mockRegistry->expects()->getAsset('foo', [])->andReturn($asset);

$latte = new Latte\Engine;
$latte->addExtension(new LatteExtension($mockRegistry));
$latte->setLoader(new Latte\Loaders\StringLoader);

$result = $latte->renderToString('<img n:class="\'foo\', \'bar\'" n:asset=foo>');
Assert::same('<img class="foo bar" src="https://example.com/image.jpg" width="800" height="600">', $result);
});


test('n:asset before n:class on the same element does not clobber the resolved asset', function () {
$asset = new ImageAsset(
url: 'https://example.com/image.jpg',
width: 800,
height: 600,
);

$mockRegistry = Mockery::mock(Registry::class);
$mockRegistry->expects()->getAsset('foo', [])->andReturn($asset);

$latte = new Latte\Engine;
$latte->addExtension(new LatteExtension($mockRegistry));
$latte->setLoader(new Latte\Loaders\StringLoader);

$result = $latte->renderToString('<img n:asset=foo n:class="\'foo\', \'bar\'">');
Assert::same('<img src="https://example.com/image.jpg" width="800" height="600" class="foo bar">', $result);
});


test('multiple n:asset + n:class elements in one template each keep their own asset', function () {
$first = new ImageAsset(url: 'https://example.com/first.jpg', width: 100, height: 100);
$second = new ImageAsset(url: 'https://example.com/second.jpg', width: 200, height: 200);

$mockRegistry = Mockery::mock(Registry::class);
$mockRegistry->expects()->getAsset('first', [])->andReturn($first);
$mockRegistry->expects()->getAsset('second', [])->andReturn($second);

$latte = new Latte\Engine;
$latte->addExtension(new LatteExtension($mockRegistry));
$latte->setLoader(new Latte\Loaders\StringLoader);

$result = $latte->renderToString(
'<img n:class="\'a\'" n:asset=first><img n:class="\'b\'" n:asset=second>',
);
Assert::same(
'<img class="a" src="https://example.com/first.jpg" width="100" height="100">'
. '<img class="b" src="https://example.com/second.jpg" width="200" height="200">',
$result,
);
});


test('n:asset? combined with n:class still renders when the asset is missing', function () {
$mockRegistry = Mockery::mock(Registry::class);
$mockRegistry->expects()->tryGetAsset('foo', [])->andReturn(null);

$latte = new Latte\Engine;
$latte->addExtension(new LatteExtension($mockRegistry));
$latte->setLoader(new Latte\Loaders\StringLoader);

$result = $latte->renderToString('<img n:class="\'foo\'" n:asset?=foo>');
Assert::same('', $result);
});