Skip to content

Commit

Permalink
[FEATURE] Add substractChars argument to ICalendarDescriptionViewHelper
Browse files Browse the repository at this point in the history
In order to ensure the first line of foldable text is always
75 chars long, the argument `substractChars` has been added.
The given value will be substracted from 75 to ensure the first
line is not longer than 75 chars.

Refs derhansen#992
  • Loading branch information
derhansen committed Mar 2, 2022
1 parent bce94c3 commit ad3acbe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
13 changes: 8 additions & 5 deletions Classes/ViewHelpers/Format/ICalendarDescriptionViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

/**
* ICalendar Description viewhelper. Note, this ViewHelper does not escape output and should only be used
* to process the iCal description field.
* to process the iCal description field and any other foldable ICal text fields.
*/
class ICalendarDescriptionViewHelper extends AbstractViewHelper
{
Expand All @@ -31,6 +31,7 @@ public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('description', 'string', 'The description', false);
$this->registerArgument('substractChars', 'integer', 'Amount of chars to substract from 75 in first line', false, 12);
}

/**
Expand All @@ -41,6 +42,8 @@ public function initializeArguments()
public function render(): string
{
$description = $this->arguments['description'] ?? null;
$substractChars = $this->arguments['substractChars'] ?? 0;
$firstLineMaxChars = 75 - $substractChars;
if ($description === null) {
$description = $this->renderChildren();
}
Expand All @@ -51,10 +54,10 @@ public function render(): string
$tmpDescription = str_replace(chr(13), '\n\n', $tmpDescription);
// Strip new lines
$tmpDescription = str_replace(chr(10), '', $tmpDescription);
// Glue everything together, so every line is max 75 chars
if (mb_strlen($tmpDescription) > 63) {
$newDescription = mb_substr($tmpDescription, 0, 63) . chr(10);
$tmpDescription = mb_substr($tmpDescription, 63);
// Glue everything together, so every line is max 75 chars respecting max. length of first line
if (mb_strlen($tmpDescription) > $firstLineMaxChars) {
$newDescription = mb_substr($tmpDescription, 0, $firstLineMaxChars) . chr(10);
$tmpDescription = mb_substr($tmpDescription, $firstLineMaxChars);
$arrPieces = mb_str_split($tmpDescription, 75);
foreach ($arrPieces as &$value) {
$value = ' ' . $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,39 @@ public function iCalendarDescriptionDataProvider(): array
return [
'emptyValue' => [
'',
12,
'',
],
'shortDescriptionLess75Chars' => [
'This is just a short text with less than 75 chars',
12,
'This is just a short text with less than 75 chars',
],
'shortDescriptionLess75CharsWithHtml' => [
'This is just a short text <b>with</b> less&nbsp;than 75 chars',
12,
'This is just a short text with less than 75 chars',
],
'shortDescriptionLess75CharsWithHtmlAndLineBreak' => [
'This is just a short text <b>with</b> less&nbsp;than 75 chars' . chr(13) . ' and some more text',
12,
'This is just a short text with less than 75 chars\n\n and some ' . chr(10) . ' more text',
],
'longDescriptionWithoutLineBreaks' => [
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam',
12,
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed di' . chr(10) . ' am nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, s' . chr(10) . ' ed diam',
],
'longDescriptionWithLineBreaks' => [
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ' . chr(13) . 'nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam',
12,
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed di' . chr(10) . ' am \n\nnonumy eirmod tempor invidunt ut labore et dolore magna aliquyam era' . chr(10) . ' t, sed diam',
],
'longDescriptionWithDifferentSubstractCharsOption' => [
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam',
48,
'Lorem ipsum dolor sit amet,' . chr(10) . ' consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut lab' . chr(10) . ' ore et dolore magna aliquyam erat, sed diam',
],
];
}

Expand All @@ -57,12 +68,13 @@ public function iCalendarDescriptionDataProvider(): array
* @dataProvider iCalendarDescriptionDataProvider
*
* @param mixed $value
* @param mixed $substractChars
* @param mixed $expected
*/
public function viewHelperReturnsExpectedValues($value, $expected)
public function viewHelperReturnsExpectedValues($value, $substractChars, $expected)
{
$viewHelper = new ICalendarDescriptionViewHelper();
$viewHelper->setArguments(['description' => $value]);
$viewHelper->setArguments(['description' => $value, 'substractChars' => $substractChars]);
$actual = $viewHelper->render();
self::assertEquals($expected, $actual);
}
Expand Down

0 comments on commit ad3acbe

Please sign in to comment.