Skip to content

Commit

Permalink
Rename to StringUtil methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Jun 2, 2021
1 parent 4dc2d38 commit 5a5578f
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,16 @@ protected function compile()
}
elseif ($objEvent->title)
{
$responseContext->setTitle(StringUtil::getRawDecodedValue($objEvent->title));
$responseContext->setTitle(StringUtil::inputEncodedToPlainText($objEvent->title));
}

if ($objEvent->description)
{
$responseContext->setMetaDescription(StringUtil::getRawDecodedValue($objEvent->description));
$responseContext->setMetaDescription(StringUtil::inputEncodedToPlainText($objEvent->description));
}
elseif ($objEvent->teaser)
{
$responseContext->setMetaDescription(StringUtil::getRawDecodedValueFromHtml($objEvent->teaser));
$responseContext->setMetaDescription(StringUtil::htmlToPlainText($objEvent->teaser));
}

if ($objEvent->robots)
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Resources/contao/classes/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ public static function getMetaData($strData, $strLanguage)
}

/**
* @deprecated Use StringUtil::getRawDecodedValueFromHtml() instead.
* @deprecated Use StringUtil::htmlToPlainText() instead.
*
* Prepare a text to be used in the meta description tag
*
Expand All @@ -578,7 +578,7 @@ public static function getMetaData($strData, $strLanguage)
*/
protected function prepareMetaDescription($strText)
{
trigger_deprecation('contao/core-bundle', '4.12', 'Using "Contao\Frontend::prepareMetaDescription()" has been deprecated and will no longer work Contao 5.0. Use the StringUtil::getRawDecodedValueFromHtml() instead.');
trigger_deprecation('contao/core-bundle', '4.12', 'Using "Contao\Frontend::prepareMetaDescription()" has been deprecated and will no longer work Contao 5.0. Use the StringUtil::htmlToPlainText() instead.');

$strText = $this->replaceInsertTags($strText, false);
$strText = strip_tags($strText);
Expand Down
49 changes: 31 additions & 18 deletions core-bundle/src/Resources/contao/library/Contao/StringUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -975,22 +975,10 @@ public static function ampersand($strString, $blnEncode=true): string
/**
* Converts an input-encoded string back to its raw UTF-8 value it originated from.
*
* It handles all Contao input encoding specifics like insert tags, basic entities and encoded entities.
*
* @param bool $blnRemoveInsertTags True to remove insert tags instead of replacing them
* It handles all Contao input encoding specifics like basic entities and encoded entities.
*/
public static function getRawDecodedValue(string $strValue, bool $blnRemoveInsertTags = false): string
public static function revertInputEncoding(string $strValue): string
{
if ($blnRemoveInsertTags)
{
$strValue = static::stripInsertTags($strValue);
}
else
{
$strValue = Controller::replaceInsertTags($strValue, false);
}

$strValue = strip_tags($strValue);
$strValue = static::restoreBasicEntities($strValue);
$strValue = static::decodeEntities($strValue);

Expand All @@ -1005,23 +993,48 @@ public static function getRawDecodedValue(string $strValue, bool $blnRemoveInser
mb_substitute_character($substituteCharacter);
}

return $strValue;
}

/**
* Converts an input-encoded string to plain text UTF-8.
*
* Strips or replaces insert tags, strips HTML tags, decodes entities, escapes insert tag braces.
*
* @see StringUtil::revertInputEncoding()
*
* @param bool $blnRemoveInsertTags True to remove insert tags instead of replacing them
*/
public static function inputEncodedToPlainText(string $strValue, bool $blnRemoveInsertTags = false): string
{
if ($blnRemoveInsertTags)
{
$strValue = static::stripInsertTags($strValue);
}
else
{
$strValue = Controller::replaceInsertTags($strValue, false);
}

$strValue = strip_tags($strValue);
$strValue = static::revertInputEncoding($strValue);
$strValue = str_replace(array('{{', '}}'), array('[{]', '[}]'), $strValue);

return $strValue;
}

/**
* Gets the raw text value of an HTML string with normalized white space.
* Converts an HTML string to plain text with normalized white space.
*
* It handles all Contao input encoding specifics like insert tags, basic
* entities and encoded entities and is meant to be used with content from
* fields that have the allowHtml flag enabled.
*
* @see StringUtil::getRawDecodedValue()
* @see StringUtil::inputEncodedToPlainText()
*
* @param bool $blnRemoveInsertTags True to remove insert tags instead of replacing them
*/
public static function getRawDecodedValueFromHtml(string $strValue, bool $blnRemoveInsertTags = false): string
public static function htmlToPlainText(string $strValue, bool $blnRemoveInsertTags = false): string
{
if (!$blnRemoveInsertTags)
{
Expand All @@ -1035,7 +1048,7 @@ public static function getRawDecodedValueFromHtml(string $strValue, bool $blnRem
$strValue
);

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

// Remove duplicate line breaks and spaces
$strValue = trim(preg_replace(array('/[^\S\n]+/', '/\s*\n\s*/'), array(' ', "\n"), $strValue));
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Resources/contao/models/PageModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,11 @@ public function __set($strKey, $varValue)
switch ($strKey)
{
case 'pageTitle':
$responseContext->setTitle(StringUtil::getRawDecodedValue($varValue ?? ''));
$responseContext->setTitle(StringUtil::inputEncodedToPlainText($varValue ?? ''));
break;

case 'description':
$responseContext->setMetaDescription(StringUtil::getRawDecodedValue($varValue ?? ''));
$responseContext->setMetaDescription(StringUtil::inputEncodedToPlainText($varValue ?? ''));
break;

case 'robots':
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Resources/contao/modules/ModuleArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ protected function compile()

if ($responseContext instanceof WebpageResponseContext)
{
$responseContext->setTitle(StringUtil::getRawDecodedValue($this->title ?? ''));
$responseContext->setTitle(StringUtil::inputEncodedToPlainText($this->title ?? ''));

if ($this->teaser)
{
$responseContext->setMetaDescription(StringUtil::getRawDecodedValueFromHtml($this->teaser));
$responseContext->setMetaDescription(StringUtil::htmlToPlainText($this->teaser));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class ContaoWebpageResponseContext extends WebpageResponseContext
{
public function __construct(PageModel $pageModel)
{
$title = $pageModel->pageTitle ?: StringUtil::getRawDecodedValue($pageModel->title ?: '');
$title = $pageModel->pageTitle ?: StringUtil::inputEncodedToPlainText($pageModel->title ?: '');

$this
->setTitle($title ?: '')
->setMetaDescription(StringUtil::getRawDecodedValue($pageModel->description ?: ''))
->setMetaDescription(StringUtil::inputEncodedToPlainText($pageModel->description ?: ''))
;

if ($pageModel->robots) {
Expand Down
54 changes: 40 additions & 14 deletions core-bundle/tests/Contao/StringUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,48 @@ public function trimsplitProvider(): \Generator
}

/**
* @dataProvider getRawDecodedValueProvider
* @dataProvider getRevertInputEncoding
*/
public function testGetsRawDecodedValues(string $source, string $expected, bool $removeInsertTags = false): void
public function testRevertInputEncoding(string $source, string $expected = null): void
{
$this->assertSame($expected, StringUtil::getRawDecodedValue($source, $removeInsertTags));
Input::setGet('value', $source);
$inputEncoded = Input::get('value');
Input::setGet('value', null);

// Test input encoding round trip
$this->assertSame($expected ?? $source, StringUtil::revertInputEncoding($inputEncoded));
}

public function getRevertInputEncoding(): \Generator
{
yield ['foobar'];
yield ['foo{{email::test@example.com}}bar'];
yield ['{{date::...}}'];
yield ["<>&\u{A0}<>&\u{A0}"];
yield ['I <3 Contao'];
yield ['Remove unexpected <span>HTML tags'];
yield ['Keep non-HTML <tags> intact'];
yield ['Basic [&] entities [nbsp]', "Basic & entities \u{A0}"];
yield ["Cont\xE4o invalid UTF-8", "Cont\u{FFFD}o invalid UTF-8"];
}

/**
* @dataProvider getInputEncodedToPlainText
*/
public function testInputEncodedToPlainText(string $source, string $expected, bool $removeInsertTags = false): void
{
$this->assertSame($expected, StringUtil::inputEncodedToPlainText($source, $removeInsertTags));

Input::setGet('value', $expected);
$inputEncoded = Input::get('value');
Input::setGet('value', null);

// Test input encoding round trip
$this->assertSame($expected, StringUtil::getRawDecodedValue($inputEncoded, true));
$this->assertSame($expected, StringUtil::getRawDecodedValue($inputEncoded, false));
$this->assertSame($expected, StringUtil::inputEncodedToPlainText($inputEncoded, true));
$this->assertSame($expected, StringUtil::inputEncodedToPlainText($inputEncoded, false));
}

public function getRawDecodedValueProvider(): \Generator
public function getInputEncodedToPlainText(): \Generator
{
yield ['foobar', 'foobar'];
yield ['foo{{email::test@example.com}}bar', 'footest@example.combar'];
Expand All @@ -182,29 +208,29 @@ public function getRawDecodedValueProvider(): \Generator
}

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

Input::setPost('value', str_replace(['&#123;&#123;', '&#125;&#125;'], ['[{]', '[}]'], $source));
$inputXssStripped = str_replace(['&#123;&#123;', '&#125;&#125;'], ['{{', '}}'], Input::postHtml('value', true));
Input::setPost('value', null);

$this->assertSame($expected, StringUtil::getRawDecodedValueFromHtml($inputXssStripped, $removeInsertTags));
$this->assertSame($expected, StringUtil::htmlToPlainText($inputXssStripped, $removeInsertTags));
}

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

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",
'<h1>Headline</h1>Text<ul><li>List 1</li><li>List 2</li></ul><p>Inline<span>text</span> and <a>link</a></p><div><div><div>single newline',
"Headline\nText\nList 1\nList 2\nInlinetext and link\nsingle newline",
];
}
}
6 changes: 3 additions & 3 deletions faq-bundle/src/Resources/contao/modules/ModuleFaqReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ protected function compile()
}
elseif ($objFaq->question)
{
$responseContext->setTitle(StringUtil::getRawDecodedValue($objFaq->question));
$responseContext->setTitle(StringUtil::inputEncodedToPlainText($objFaq->question));
}

if ($objFaq->description)
{
$responseContext->setMetaDescription(StringUtil::getRawDecodedValue($objFaq->description));
$responseContext->setMetaDescription(StringUtil::inputEncodedToPlainText($objFaq->description));
}
elseif ($objFaq->question)
{
$responseContext->setMetaDescription(StringUtil::getRawDecodedValue($objFaq->question));
$responseContext->setMetaDescription(StringUtil::inputEncodedToPlainText($objFaq->question));
}

if ($objFaq->robots)
Expand Down
6 changes: 3 additions & 3 deletions news-bundle/src/Resources/contao/modules/ModuleNewsReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ protected function compile()
}
elseif ($objArticle->headline)
{
$responseContext->setTitle(StringUtil::getRawDecodedValue($objArticle->headline));
$responseContext->setTitle(StringUtil::inputEncodedToPlainText($objArticle->headline));
}

if ($objArticle->description)
{
$responseContext->setMetaDescription(StringUtil::getRawDecodedValue($objArticle->description));
$responseContext->setMetaDescription(StringUtil::inputEncodedToPlainText($objArticle->description));
}
elseif ($objArticle->teaser)
{
$responseContext->setMetaDescription(StringUtil::getRawDecodedValueFromHtml($objArticle->teaser));
$responseContext->setMetaDescription(StringUtil::htmlToPlainText($objArticle->teaser));
}

if ($objArticle->robots)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function compile()

if ($responseContext instanceof WebpageResponseContext)
{
$responseContext->setTitle(StringUtil::getRawDecodedValue($objNewsletter->subject));
$responseContext->setTitle(StringUtil::inputEncodedToPlainText($objNewsletter->subject));
}
}

Expand Down

0 comments on commit 5a5578f

Please sign in to comment.