From 03ff5d091d3eb5bb0573c4be90e325dd2ac943e4 Mon Sep 17 00:00:00 2001
From: Peter Beernink <p.beernink@drecomm.nl>
Date: Fri, 25 Mar 2011 12:20:27 +0100
Subject: [PATCH] [BUGFIX] Also crop when maxCharacters < first word

When maxCharacters is equal or less then the length of the first word,
the whole string was returned when 'crop to word' is enabled instead of
the only the cropped part.

Change-Id: I0a7ec720b12a7999245b183f36daff5cdd7a8ace
Resolves: #25368
Reviewed-on: http://review.typo3.org/1274
Reviewed-by: Steffen Gebert
Tested-by: Steffen Gebert
---
 .../cms/tslib/class.tslib_contentTest.php     | 12 +++++++++
 .../sysext/cms/tslib/class.tslib_content.php  | 26 ++++++++++++++-----
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/tests/typo3/sysext/cms/tslib/class.tslib_contentTest.php b/tests/typo3/sysext/cms/tslib/class.tslib_contentTest.php
index 7f9ee15cc845..a3ce27d2fab8 100644
--- a/tests/typo3/sysext/cms/tslib/class.tslib_contentTest.php
+++ b/tests/typo3/sysext/cms/tslib/class.tslib_contentTest.php
@@ -422,9 +422,15 @@ class tslib_contentTest extends tx_phpunit_testcase {
 				$charset . ' plain text; -58|...' => array(
 					'-58|...', $plainText, '...h' . chr(248) . 'j implemented the original version of the crop function.', $charset
 				),
+				$charset . ' plain text; 4|...|1' => array(
+					'4|...|1', $plainText, 'Kasp...', $charset
+				),
 				$charset . ' plain text; 20|...|1' => array(
 					'20|...|1', $plainText, 'Kasper Sk' . chr(229) . 'rh' . chr(248) . 'j...', $charset
 				),
+				$charset . ' plain text; -5|...|1' => array(
+					'-5|...|1', $plainText, '...tion.', $charset
+				),
 				$charset . ' plain text; -49|...|1' => array(
 					'-49|...|1', $plainText, '...the original version of the crop function.', $charset
 				),
@@ -446,6 +452,9 @@ class tslib_contentTest extends tx_phpunit_testcase {
 				$charset . ' text with markup; -58|...' => array(
 					'-58|...', $textWithMarkup, '<strong><a href="mailto:kasper@typo3.org">...h' . chr(248) . 'j</a> implemented</strong> the original version of the crop function.', $charset
 				),
+				$charset . ' text with markup 4|...|1' => array(
+					'4|...|1', $textWithMarkup, '<strong><a href="mailto:kasper@typo3.org">Kasp...</a></strong>', $charset
+				),
 				$charset . ' text with markup; 11|...|1' => array(
 					'11|...|1', $textWithMarkup, '<strong><a href="mailto:kasper@typo3.org">Kasper...</a></strong>', $charset
 				),
@@ -494,6 +503,9 @@ class tslib_contentTest extends tx_phpunit_testcase {
 				$charset . ' text with entities -59|...' => array(
 					'-59|...', $textWithEntities, '...h&oslash;j implemented the; original version of the crop function.', $charset
 				),
+				$charset . ' text with entities 4|...|1' => array(
+					'4|...|1', $textWithEntities, 'Kasp...', $charset
+				),
 				$charset . ' text with entities 9|...|1' => array(
 					'9|...|1', $textWithEntities, 'Kasper...', $charset
 				),
diff --git a/typo3/sysext/cms/tslib/class.tslib_content.php b/typo3/sysext/cms/tslib/class.tslib_content.php
index 23d1e018ebb6..ecf3a787904d 100644
--- a/typo3/sysext/cms/tslib/class.tslib_content.php
+++ b/typo3/sysext/cms/tslib/class.tslib_content.php
@@ -3681,17 +3681,29 @@ class tslib_cObj {
 					$cropPosition = $absChars - $strLen;
 						// The snippet "&[^&\s;]{2,8};" in the RegEx below represents entities.
 					$patternMatchEntityAsSingleChar = '(&[^&\s;]{2,8};|.)';
-					if ($crop2space) {
-						$cropRegEx = $chars < 0
-							? '#(?<=\s)' . $patternMatchEntityAsSingleChar . '{0,' . $cropPosition . '}$#ui'
-							: '#^' . $patternMatchEntityAsSingleChar . '{0,' . $cropPosition . '}(?=\s)#ui';
+
+					$cropRegEx = $chars < 0
+						? '#' . $patternMatchEntityAsSingleChar . '{0,' . ($cropPosition + 1) . '}$#ui'
+						: '#^' . $patternMatchEntityAsSingleChar . '{0,' . ($cropPosition + 1) . '}#ui';
+					if (preg_match($cropRegEx, $tempContent, $croppedMatch)) {
+						$tempContentPlusOneCharacter = $croppedMatch[0];
 					} else {
-						$cropRegEx = $chars < 0
-							? '#' . $patternMatchEntityAsSingleChar . '{0,' . $cropPosition . '}$#ui'
-							: '#^' . $patternMatchEntityAsSingleChar . '{0,' . $cropPosition . '}#ui';
+						$tempContentPlusOneCharacter = FALSE;
 					}
+
+					$cropRegEx = $chars < 0
+						? '#' . $patternMatchEntityAsSingleChar . '{0,' . $cropPosition . '}$#ui'
+						: '#^' . $patternMatchEntityAsSingleChar . '{0,' . $cropPosition . '}#ui';
 					if (preg_match($cropRegEx, $tempContent, $croppedMatch)) {
 						$tempContent = $croppedMatch[0];
+						if (($crop2space) && ($tempContentPlusOneCharacter !== FALSE)) {
+							$cropRegEx = $chars < 0
+								? '#(?<=\s)' . $patternMatchEntityAsSingleChar . '{0,' . $cropPosition . '}$#ui'
+								: '#^' . $patternMatchEntityAsSingleChar . '{0,' . $cropPosition . '}(?=\s)#ui';
+							if (preg_match($cropRegEx, $tempContentPlusOneCharacter, $croppedMatch)) {
+								$tempContent = $croppedMatch[0];
+							}
+						}
 					}
 					$splittedContent[$offset] = $GLOBALS['TSFE']->csConvObj->utf8_decode($tempContent, $GLOBALS['TSFE']->renderCharset);
 					break;
-- 
GitLab