Skip to content
Permalink
Browse files

Add new ViewHelper to remove new lines

  • Loading branch information...
Tim Schreiner
Tim Schreiner committed Nov 20, 2018
1 parent 53a5489 commit 89b95d2b63b82553e04d503ae4f6df1eab985d50
@@ -86,10 +86,7 @@ public function render(FileInterface $file, $width, $height, array $options = []
'options' => $options,
]);
$content = $view->render('pictureTag');
$content = trim(str_replace("\n", '', $content));
return $content;
return $view->render('pictureTag');
}
/**
@@ -0,0 +1,30 @@
<?php
namespace Codemonkey1988\ResponsiveImages\ViewHelpers;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* Blank lines removal ViewHelper
*/
class RemoveNewLineViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var bool
*/
protected $escapeOutput = false;
/**
* @param array $arguments
* @param \Closure $childClosure
* @param RenderingContextInterface $renderingContext
* @return string
*/
public static function renderStatic(array $arguments, \Closure $childClosure, RenderingContextInterface $renderingContext)
{
return str_replace("\n", '', $childClosure());
}
}
@@ -1,14 +1,19 @@
<html data-namespace-typo3-fluid="true"
xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers">
<f:if condition="!{isAnimatedGif} && !{options.disablePictureTag}">
<f:then>
<picture>
<f:render partial="Sources" arguments="{_all}"/>
<f:render partial="DefaultImage" arguments="{_all}"/>
</picture>
</f:then>
<f:else>
<f:render partial="DefaultImage" arguments="{_all}"/>
</f:else>
</f:if>
xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
xmlns:r="http://typo3.org/ns/Codemonkey1988/ResponsiveImages/ViewHelpers">
<r:removeNewLine>
<f:spaceless>
<f:if condition="!{isAnimatedGif} && !{options.disablePictureTag}">
<f:then>
<picture>
<f:render partial="Sources" arguments="{_all}"/>
<f:render partial="DefaultImage" arguments="{_all}"/>
</picture>
</f:then>
<f:else>
<f:render partial="DefaultImage" arguments="{_all}"/>
</f:else>
</f:if>
</f:spaceless>
</r:removeNewLine>
</html>
@@ -100,7 +100,7 @@ public function generateImageTagForValidJpegImage()
$file = $fileRepository->findByUid(1);
$subject = GeneralUtility::makeInstance(ResponsiveImageRenderer::class);
$result = $subject->render($file, 0, 0);
$result = trim($subject->render($file, 0, 0), "\n ");
$this->assertRegExp('/^<picture>.*<\/picture>$/', $result);
$this->assertRegExp('/<source media=".*" srcset=".*" \/>/', $result);
@@ -115,7 +115,7 @@ public function generateImageTagForValidJpegImageButDisabledPictureTag()
$file = $fileRepository->findByUid(1);
$subject = GeneralUtility::makeInstance(ResponsiveImageRenderer::class);
$result = $subject->render($file, 0, 0, ['disablePictureTag' => true]);
$result = trim($subject->render($file, 0, 0, ['disablePictureTag' => true]), "\n ");
$this->assertRegExp('/^<img src=".*" width="1920" height="1056" alt="" \/>$/', $result);
}
@@ -131,7 +131,7 @@ public function generatePictureTagForValidJpegImageWithoutImageProcessingDisable
$file = $fileRepository->findByUid(1);
$subject = GeneralUtility::makeInstance(ResponsiveImageRenderer::class);
$result = $subject->render($file, 0, 0);
$result = trim($subject->render($file, 0, 0), "\n ");
putenv('RESPONSIVE_IMAGES_PROCESSING');
@@ -157,7 +157,7 @@ public function generatePictureTagForValidJpegImageWithoutImageProcessingDisable
$file = $fileRepository->findByUid(1);
$subject = GeneralUtility::makeInstance(ResponsiveImageRenderer::class);
$result = $subject->render($file, 0, 0);
$result = trim($subject->render($file, 0, 0), "\n ");
$imagePaths = [
'.Build/bin/typo3conf/ext/responsive_images/Tests/Functional/Fixtures/fileadmin/example.jpg 1x',
@@ -0,0 +1,36 @@
<?php
namespace Codemonkey1988\ResponsiveImages\Tests\Unit\ViewHelpers;
/*
* This file is part of the TYPO3 responsive images project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read
* LICENSE file that was distributed with this source code.
*
*/
use Codemonkey1988\ResponsiveImages\ViewHelpers\RemoveNewLineViewHelper;
use Nimut\TestingFramework\TestCase\ViewHelperBaseTestcase;
/**
* Test class for \Codemonkey1988\ResponsiveImages\ViewHelpers\RemoveNewLineViewHelper
*/
class RemoveNewLineViewHelperTest extends ViewHelperBaseTestcase
{
/**
* @test
*/
public function newLinesAreRemoved()
{
/** @var RemoveNewLineViewHelper|\PHPUnit_Framework_MockObject_MockObject $viewHelper */
$viewHelper = $this->getAccessibleMock(RemoveNewLineViewHelper::class, ['renderChildren']);
$viewHelper->expects($this->once())->method('renderChildren')->willReturn("\n<content>\n\n</content>\n");
$this->injectDependenciesIntoViewHelper($viewHelper);
$this->assertSame('<content></content>', $viewHelper->render());
}
}

0 comments on commit 89b95d2

Please sign in to comment.
You can’t perform that action at this time.