Skip to content
Snippets Groups Projects
Commit c3107ec8 authored by Claus Due's avatar Claus Due Committed by Benni Mack
Browse files

[FEATURE] Spaceless 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>
</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: default avatarWouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: default avatarFrans Saris <franssaris@gmail.com>
Tested-by: default avatarFrans Saris <franssaris@gmail.com>
Reviewed-by: default avatarBenni Mack <benni@typo3.org>
Tested-by: default avatarBenni Mack <benni@typo3.org>
parent e925e8fe
Branches
Tags
No related merge requests found
==================================================================
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>
<?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()));
}
}
<?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>')
);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment