Skip to content

Commit

Permalink
Test and fix getRawDecodedValueFromHtml method
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed May 19, 2021
1 parent f7969ba commit 5821b91
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1024,14 +1024,19 @@ public static function getRawDecodedValue(string $strValue, bool $blnRemoveInser
*/
public static function getRawDecodedValueFromHtml(string $strValue, bool $blnRemoveInsertTags = false): string
{
if (!$blnRemoveInsertTags)
{
$strValue = Controller::replaceInsertTags($strValue, false);
}

// Add new lines before and after block level elements
$strValue = preg_replace(
array('/[\r\n]+/', '/</?(?:br|blockquote|div|dl|figcaption|figure|footer|h\d|header|hr|li|p|pre|tr)\b/i'),
array('/[\r\n]+/', '/<\/?(?:br|blockquote|div|dl|figcaption|figure|footer|h\d|header|hr|li|p|pre|tr)\b/i'),
array(' ', "\n$0"),
$strValue
);

$strValue = static::getRawDecodedValue($strValue, $blnRemoveInsertTags);
$strValue = static::getRawDecodedValue($strValue, true);

// Remove duplicate line breaks and spaces
$strValue = trim(preg_replace(array('/[^\S\n]+/', '/\s*\n\s*/'), array(' ', "\n"), $strValue));
Expand Down
50 changes: 50 additions & 0 deletions core-bundle/tests/Contao/StringUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@

namespace Contao\CoreBundle\Tests\Contao;

use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
use Contao\CoreBundle\Tests\TestCase;
use Contao\StringUtil;
use Contao\System;
use Psr\Log\NullLogger;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\RequestStack;

class StringUtilTest extends TestCase
{
Expand All @@ -26,6 +28,10 @@ protected function setUp(): void

$container = new ContainerBuilder();
$container->setParameter('kernel.project_dir', $this->getFixturesDir());
$container->setParameter('kernel.cache_dir', $this->getFixturesDir().'/cache');
$container->setParameter('kernel.debug', false);
$container->set('request_stack', new RequestStack());
$container->set('contao.security.token_checker', $this->createMock(TokenChecker::class));
$container->set('monolog.logger.contao', new NullLogger());

System::setContainer($container);
Expand Down Expand Up @@ -142,4 +148,48 @@ public function trimsplitProvider(): \Generator
['foo', 'bar'],
];
}

/**
* @dataProvider getRawDecodedValueProvider
*/
public function testGetsRawDecodedValues(string $source, string $expected, bool $removeInsertTags = false): void
{
$this->assertSame($expected, StringUtil::getRawDecodedValue($source, $removeInsertTags));
}

public function getRawDecodedValueProvider(): \Generator
{
yield ['foobar', 'foobar'];
yield ['foo{{email::test@example.com}}bar', 'footest@example.combar'];
yield ['foo{{email::test@example.com}}bar', 'foobar', true];
yield ['{{date::...}}', '...'];
yield ['{{date::...}}', '', true];
yield ["&lt;&#62;&\u{A0}[lt][gt][&][nbsp]", "<>&\u{A0}<>&\u{A0}", true];
yield ["I &lt;3 Contao", "I <3 Contao"];
yield ["Remove unexpected <span>HTML tags", "Remove unexpected HTML tags"];
yield ["Keep non-HTML &lt;tags&#62; intact", "Keep non-HTML <tags> intact"];
yield ["Cont\xE4o invalid UTF-8", "Cont\u{FFFD}o invalid UTF-8"];
yield ["&#123;&#123;date&#125;&#125;", "\u{200C}{\u{200C}{date}}"];
}

/**
* @dataProvider getRawDecodedValueFromHtmlProvider
*/
public function testGetsRawDecodedValuesFromHtml(string $source, string $expected, bool $removeInsertTags = false): void
{
$this->assertSame($expected, StringUtil::getRawDecodedValueFromHtml($source, $removeInsertTags));
}

public function getRawDecodedValueFromHtmlProvider(): \Generator
{
yield from $this->getRawDecodedValueProvider();

yield ['foo<br>bar{{br}}baz', "foo\nbar\nbaz"];
yield [" \t\r\nfoo \t\r\n \r\n\t bar \t\r\n", "foo bar"];
yield [" \t\r\n<br>foo \t<br>\r\n \r\n\t<br> bar <br>\t\r\n", "foo\nbar"];
yield [
"<h1>Headline</h1>Text<ul><li>List 1</li><li>List 2</li></ul><p>Inline<span>text</span> and <a>link</a></p>",
"Headline\nText\nList 1\nList 2\nInlinetext and link",
];
}
}

0 comments on commit 5821b91

Please sign in to comment.