[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:24:22 +0000 (13:24 +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: I0a7ec720b12a7999245b183f36daff5cdd7a8ace
Resolves: #25368
Reviewed-on: http://review.typo3.org/1274
Reviewed-by: Steffen Gebert
Tested-by: Steffen Gebert
tests/typo3/sysext/cms/tslib/class.tslib_contentTest.php
typo3/sysext/cms/tslib/class.tslib_content.php

index 7f9ee15..a3ce27d 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 23d1e01..ecf3a78 100644 (file)
@@ -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;