[BUGFIX] Also crop when maxCharacters < first word
authorPeter Beernink <p.beernink@drecomm.nl>
Fri, 25 Mar 2011 11:20:27 +0000 (12:20 +0100)
committerSteffen Gebert <steffen.gebert@typo3.org>
Sun, 8 May 2011 11:26:20 +0000 (13:26 +0200)
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: I96446430bd0681e9f64c699bca9f80866a712490
Resolves: #25368
Reviewed-on: http://review.typo3.org/1955
Reviewed-by: Steffen Gebert
Tested-by: Steffen Gebert
tests/typo3/sysext/cms/tslib/tslib_contentTest.php
typo3/sysext/cms/tslib/class.tslib_content.php

index 86a7e52..b27f7fa 100644 (file)
@@ -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
                                ),
index bf7df65..466d040 100644 (file)
@@ -3682,17 +3682,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;