Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix spaceless deprecation
  • Loading branch information
garak committed Feb 7, 2025
commit 3f35e94f0a216588db611df64efd3b1886d3fc4a
2 changes: 1 addition & 1 deletion src/Knp/Menu/Resources/views/knp_menu.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{% endmacro %}

{% block compressed_root %}
{% apply spaceless %}
{% apply knp_menu_spaceless %}
{{ block('root') }}
{% endapply %}
{% endblock %}
Expand Down
6 changes: 6 additions & 0 deletions src/Knp/Menu/Twig/MenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function getFilters(): array

return [
new TwigFilter('knp_menu_as_string', $legacy ? [$this, 'pathAsString'] : [MenuRuntimeExtension::class, 'pathAsString']),
new TwigFilter('knp_menu_spaceless', [$this, 'spaceless'], ['is_safe' => ['html']]),
];
}

Expand Down Expand Up @@ -121,4 +122,9 @@ public function isAncestor(ItemInterface $item, ?int $depth = null): bool

return $this->runtimeExtension->isAncestor($item, $depth);
}

public function spaceless(string $content): string
{
return trim((string) preg_replace('/>\s+</', '><', $content));
}
}
3 changes: 3 additions & 0 deletions tests/Knp/Menu/Tests/Renderer/TwigRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Knp\Menu\MenuItem;
use Knp\Menu\Renderer\RendererInterface;
use Knp\Menu\Renderer\TwigRenderer;
use Knp\Menu\Twig\Helper;
use Knp\Menu\Twig\MenuExtension;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

Expand All @@ -16,6 +18,7 @@ public function createRenderer(MatcherInterface $matcher): RendererInterface
{
$loader = new FilesystemLoader(__DIR__.'/../../../../../src/Knp/Menu/Resources/views');
$environment = new Environment($loader);
$environment->addExtension(new MenuExtension($this->createMock(Helper::class)));

return new TwigRenderer($environment, 'knp_menu.html.twig', $matcher, ['compressed' => true]);
}
Expand Down
Loading