[FEATURE] Spaceless ViewHelper 47/43747/6
authorClaus Due <claus@namelesscoder.net>
Fri, 2 Oct 2015 16:57:00 +0000 (18:57 +0200)
committerBenni Mack <benni@typo3.org>
Wed, 7 Oct 2015 09:22:38 +0000 (11:22 +0200)
Removes redundant spaces between HTML tags while
preserving the whitespace that may be inside HTML
tags. Trims the final result before output.

Heavily inspired by Twig's corresponding node type.

<code title="Usage of f:spaceless">
<f:spaceless>
<div>
    <div>
        <div>text

text</div>
    </div>
</div>
</f:spaceless>
</code>
<output>
<div><div><div>text

text</div></div></div>
</output>

Change-Id: I3aa88877619dfa8e681b53c2d4f5df91bb51d478
Resolves: #70170
Releases: master
Reviewed-on: http://review.typo3.org/43747
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Frans Saris <franssaris@gmail.com>
Tested-by: Frans Saris <franssaris@gmail.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/core/Documentation/Changelog/master/Feature-70170-ViewHelperToStripWhitespaceBetweenHTMLTags.rst [new file with mode: 0644]
typo3/sysext/fluid/Classes/ViewHelpers/SpacelessViewHelper.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/Unit/ViewHelpers/SpacelessViewHelperTest.php [new file with mode: 0644]

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-70170-ViewHelperToStripWhitespaceBetweenHTMLTags.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-70170-ViewHelperToStripWhitespaceBetweenHTMLTags.rst
new file mode 100644 (file)
index 0000000..b2e60ac
--- /dev/null
@@ -0,0 +1,29 @@
+==================================================================
+Feature: #70170 - ViewHelper to strip whitespace between HTML tags
+==================================================================
+
+Description
+===========
+
+Removes redundant spaces between HTML tags while preserving the whitespace that may be inside HTML tags. Trims the final result before output.
+
+Heavily inspired by Twig's corresponding node type.
+
+.. code-block:: html
+
+       <code title="Usage of f:spaceless">
+       <f:spaceless>
+       <div>
+           <div>
+               <div>text
+
+       text</div>
+       </div>
+       </div>
+       </f:spaceless>
+       </code>
+       <output>
+       <div><div><div>text
+
+       text</div></div></div>
+       </output>
diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/SpacelessViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/SpacelessViewHelper.php
new file mode 100644 (file)
index 0000000..a8cf804
--- /dev/null
@@ -0,0 +1,61 @@
+<?php
+namespace TYPO3\CMS\Fluid\ViewHelpers;
+
+/*                                                                        *
+ * This script is backported from the TYPO3 Flow package "TYPO3.Fluid".   *
+ *                                                                        *
+ * It is free software; you can redistribute it and/or modify it under    *
+ * the terms of the GNU Lesser General Public License, either version 3   *
+ *  of the License, or (at your option) any later version.                *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+
+/**
+ * Space Removal ViewHelper
+ *
+ * Removes redundant spaces between HTML tags while
+ * preserving the whitespace that may be inside HTML
+ * tags. Trims the final result before output.
+ *
+ * Heavily inspired by Twig's corresponding node type.
+ *
+ * <code title="Usage of f:spaceless">
+ * <f:spaceless>
+ * <div>
+ *     <div>
+ *         <div>text
+ *
+ * text</div>
+ *     </div>
+ * </div>
+ * </code>
+ * <output>
+ * <div><div><div>text
+ *
+ * text</div></div></div>
+ * </output>
+ */
+class SpacelessViewHelper extends AbstractViewHelper implements CompilableInterface {
+
+       /**
+        * @return string
+        */
+       public function render() {
+               return static::renderStatic($this->arguments, $this->renderChildrenClosure, $this->renderingContext);
+       }
+
+       /**
+        * @param array $arguments
+        * @param \Closure $childClosure
+        * @param RenderingContextInterface $renderingContext
+        */
+       public static function renderStatic(array $arguments, \Closure $childClosure, RenderingContextInterface $renderingContext) {
+               return trim(preg_replace('/\\>\\s+\\</', '><', $childClosure()));
+       }
+
+}
diff --git a/typo3/sysext/fluid/Tests/Unit/ViewHelpers/SpacelessViewHelperTest.php b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/SpacelessViewHelperTest.php
new file mode 100644 (file)
index 0000000..48e23be
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
+
+/*                                                                        *
+ * This script is backported from the TYPO3 Flow package "TYPO3.Fluid".   *
+ *                                                                        *
+ * It is free software; you can redistribute it and/or modify it under    *
+ * the terms of the GNU Lesser General Public License, either version 3   *
+ *  of the License, or (at your option) any later version.                *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+use TYPO3\CMS\Fluid\ViewHelpers\SpacelessViewHelper;
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+
+/**
+ * Testcase for SpacelessViewHelper
+ */
+class SpacelessViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase {
+
+       /**
+        * @param string $input
+        * @param string $expected
+        * @dataProvider getRenderStaticData
+        * @test
+        */
+       public function testRender($input, $expected) {
+               $instance = new SpacelessViewHelper();
+               $instance->setRenderChildrenClosure(function() use ($input) { return $input; });
+               $instance->setRenderingContext($this->getMock(RenderingContextInterface::class));
+               $instance->setArguments(array());
+               $this->assertEquals($expected, $instance->render());
+       }
+
+       /**
+        * @param string $input
+        * @param string $expected
+        * @dataProvider getRenderStaticData
+        * @test
+        */
+       public function testRenderStatic($input, $expected) {
+               $context = $this->getMock(RenderingContextInterface::class);
+               $this->assertEquals($expected, SpacelessViewHelper::renderStatic(array(), function() use ($input) { return $input; }, $context));
+       }
+
+       /**
+        * @return array
+        */
+       public function getRenderStaticData() {
+               return array(
+                       'extra whitespace between tags' => array('<div>foo</div>  <div>bar</div>', '<div>foo</div><div>bar</div>'),
+                       'whitespace preserved in text node' => array(PHP_EOL . '<div>' . PHP_EOL . 'foo</div>', '<div>' . PHP_EOL . 'foo</div>'),
+                       'whitespace removed from non-text node' => array(PHP_EOL . '<div>' . PHP_EOL . '<div>foo</div></div>', '<div><div>foo</div></div>')
+               );
+       }
+
+}