Repository: KnpLabs/KnpMenu (PHP)

Issue:
Twig spaceless deprecation
User Deprecated: Since twig/twig 3.12: Twig Filter "spaceless" is deprecated in vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views/knp_menu.html.twig at line 12.


Discussion:

Patch:
diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml
index f55195dc..a1fff83f 100644
--- a/.github/workflows/build.yaml
+++ b/.github/workflows/build.yaml
@@ -42,16 +42,16 @@ jobs:
                 include:
                     - description: 'No Symfony specified'
                       php: '8.1'
-                      max_deprecations: 1
+                      max_deprecations: 0
                     - description: 'No Symfony specified'
                       php: '8.2'
-                      max_deprecations: 1
+                      max_deprecations: 0
                     - description: 'No Symfony specified'
                       php: '8.3'
-                      max_deprecations: 1
+                      max_deprecations: 0
                     - description: 'No Symfony specified'
                       php: '8.4'
-                      max_deprecations: 1
+                      max_deprecations: 0
                     - description: 'Lowest deps'
                       php: '8.1'
                       composer_option: '--prefer-lowest'
@@ -59,12 +59,12 @@ jobs:
                     - description: 'Symfony 6'
                       php: '8.1'
                       symfony: 6.4.*
-                      max_deprecations: 1
+                      max_deprecations: 0
                     - description: 'Symfony 7'
                       php: '8.4'
                       dev: true
                       symfony: 7.2.*
-                      max_deprecations: 1
+                      max_deprecations: 0
         name: PHP ${{ matrix.php }} tests (${{ matrix.description }})
         steps:
             - name: Checkout
diff --git a/src/Knp/Menu/Resources/views/knp_menu.html.twig b/src/Knp/Menu/Resources/views/knp_menu.html.twig
index 6bb65cc7..bb20becb 100644
--- a/src/Knp/Menu/Resources/views/knp_menu.html.twig
+++ b/src/Knp/Menu/Resources/views/knp_menu.html.twig
@@ -9,7 +9,7 @@
 {% endmacro %}
 
 {% block compressed_root %}
-{% apply spaceless %}
+{% apply knp_menu_spaceless %}
 {{ block('root') }}
 {% endapply %}
 {% endblock %}
diff --git a/src/Knp/Menu/Twig/MenuExtension.php b/src/Knp/Menu/Twig/MenuExtension.php
index 9ed6568b..f484aa4b 100644
--- a/src/Knp/Menu/Twig/MenuExtension.php
+++ b/src/Knp/Menu/Twig/MenuExtension.php
@@ -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', [self::class, 'spaceless'], ['is_safe' => ['html']]),
         ];
     }
 
@@ -121,4 +122,12 @@ public function isAncestor(ItemInterface $item, ?int $depth = null): bool
 
         return $this->runtimeExtension->isAncestor($item, $depth);
     }
+
+    /**
+     * @internal
+     */
+    public static function spaceless(string $content): string
+    {
+        return trim((string) preg_replace('/>\s+</', '><', $content));
+    }
 }


Test patch:
diff --git a/tests/Knp/Menu/Tests/Renderer/TwigRendererTest.php b/tests/Knp/Menu/Tests/Renderer/TwigRendererTest.php
index bf479002..0545c44e 100644
--- a/tests/Knp/Menu/Tests/Renderer/TwigRendererTest.php
+++ b/tests/Knp/Menu/Tests/Renderer/TwigRendererTest.php
@@ -7,6 +7,7 @@
 use Knp\Menu\MenuItem;
 use Knp\Menu\Renderer\RendererInterface;
 use Knp\Menu\Renderer\TwigRenderer;
+use Knp\Menu\Twig\MenuExtension;
 use Twig\Environment;
 use Twig\Loader\FilesystemLoader;
 
@@ -16,6 +17,7 @@ public function createRenderer(MatcherInterface $matcher): RendererInterface
     {
         $loader = new FilesystemLoader(__DIR__.'/../../../../../src/Knp/Menu/Resources/views');
         $environment = new Environment($loader);
+        $environment->addExtension(new MenuExtension());
 
         return new TwigRenderer($environment, 'knp_menu.html.twig', $matcher, ['compressed' => true]);
     }