[BUGFIX] Do not remove trailing slash in slug sanitization 99/58099/2
authorBenni Mack <benni@typo3.org>
Fri, 31 Aug 2018 09:27:54 +0000 (11:27 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Fri, 31 Aug 2018 10:38:47 +0000 (12:38 +0200)
It is totally legitimate to allow /my-page/ as slug (when in FormEngine
just remove the "readonly" attribute of the slug field), if explicitly
wanted.

There is a difference between "generate" (automatically create the slug)
where TYPO3 recommends slugs without slashes, and "sanitize"
to clean up a slug, where "/" is completely valid.

Resolves: #86055
Releases: master
Change-Id: I8e73f27e18fc74242cb230d69a85ec2946cf1dae
Reviewed-on: https://review.typo3.org/58099
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Frans Saris <franssaris@gmail.com>
Tested-by: Frans Saris <franssaris@gmail.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Classes/DataHandling/SlugHelper.php
typo3/sysext/core/Tests/Unit/DataHandling/SlugHelperTest.php

index e5974f9..c081e8b 100644 (file)
@@ -107,8 +107,9 @@ class SlugHelper
         $slug = rawurlencode($slug);
         // @todo: add a test and see if we need this
         $slug = str_replace('%2F', '/', $slug);
-        // Remove trailing and beginning slashes
-        $slug = '/' . $this->extract($slug);
+        // Remove trailing and beginning slashes, except if the trailing slash was added, then we'll re-add it
+        $appendTrailingSlash = substr($slug, -1) === '/';
+        $slug = '/' . $this->extract($slug) . ($appendTrailingSlash ? '/' : '');
         return $slug;
     }
 
index 22ad184..1c9aaf4 100644 (file)
@@ -80,20 +80,20 @@ class SlugHelperTest extends UnitTestCase
                 '/1/2',
                 '/1/2',
             ],
-            'remove trailing slash' => [
+            'do not remove trailing slash' => [
                 [],
                 '1/2/',
-                '/1/2',
+                '/1/2/',
             ],
             'keep pending slash and remove fallback' => [
                 [],
                 '/-1/2',
                 '/1/2',
             ],
-            'remove trailing slash and fallback' => [
+            'do not remove trailing slash, but remove fallback' => [
                 [],
                 '1/2-/',
-                '/1/2',
+                '/1/2/',
             ],
             'reduce multiple fallback chars to one' => [
                 [],