Index: lib/moodlelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/moodlelib.php,v retrieving revision 1.960.2.141 diff -u -r1.960.2.141 moodlelib.php --- lib/moodlelib.php 12 Oct 2009 13:05:04 -0000 1.960.2.141 +++ lib/moodlelib.php 12 Oct 2009 13:45:19 -0000 @@ -6840,13 +6840,19 @@ return $text; } - // splits all html-tags to scanable lines + // Splits on HTML tags. Each open/close/empty tag will be the first thing + // and only tag in its 'line' preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER); $total_length = strlen($ending); - $open_tags = array(); $truncate = ''; + // This array stores information about open and close tags and their position + // in the truncated string. Each item in the array is an object with fields + // ->open (true if open), ->tag (tag name in lower case), and ->pos + // (byte position in truncated text) + $tagdetails = array(); + foreach ($lines as $line_matchings) { // if there is any html-tag in this line, handle it and add it (uncounted) to the output if (!empty($line_matchings[1])) { @@ -6855,15 +6861,14 @@ // do nothing // if tag is a closing tag (f.e. ) } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { - // delete tag from $open_tags list - $pos = array_search($tag_matchings[1], array_reverse($open_tags, true)); // can have multiple exact same open tags, close the last one - if ($pos !== false) { - unset($open_tags[$pos]); - } + // record closing tag + $tagdetails[] = (object)array('open'=>false, + 'tag'=>strtolower($tag_matchings[1]), 'pos'=>strlen($truncate)); // if tag is an opening tag (f.e. ) } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) { - // add tag to the beginning of $open_tags list - array_unshift($open_tags, strtolower($tag_matchings[1])); + // record opening tag + $tagdetails[] = (object)array('open'=>true, + 'tag'=>strtolower($tag_matchings[1]), 'pos'=>strlen($truncate)); } // add html-tag to $truncate'd text $truncate .= $line_matchings[1]; @@ -6926,6 +6931,24 @@ // add the defined ending to the text $truncate .= $ending; + // Now calculate the list of open html tags based on the truncate position + $open_tags = array(); + foreach ($tagdetails as $taginfo) { + if(isset($breakpos) && $taginfo->pos >= $breakpos) { + // Don't include tags after we made the break! + break; + } + if($taginfo->open) { + // add tag to the beginning of $open_tags list + array_unshift($open_tags, $taginfo->tag); + } else { + $pos = array_search($taginfo->tag, array_reverse($open_tags, true)); // can have multiple exact same open tags, close the last one + if ($pos !== false) { + unset($open_tags[$pos]); + } + } + } + // close all unclosed html-tags foreach ($open_tags as $tag) { $truncate .= '' . $tag . '>'; Index: lib/simpletest/testmoodlelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/simpletest/testmoodlelib.php,v retrieving revision 1.9.2.5 diff -u -r1.9.2.5 testmoodlelib.php --- lib/simpletest/testmoodlelib.php 21 Jun 2009 23:35:10 -0000 1.9.2.5 +++ lib/simpletest/testmoodlelib.php 12 Oct 2009 13:45:19 -0000 @@ -219,6 +219,34 @@ $this->assertFalse(make_user_directory(true, true)); } + + function test_shorten_text() { + $text = "short text already no tags"; + $this->assertEqual($text, shorten_text($text)); + + $text = "
short text already
with tags
"; + $this->assertEqual($text, shorten_text($text)); + + $text = "long text without any tags blah de blah blah blah what"; + $this->assertEqual('long text without any tags ...', shorten_text($text)); + + $text = "Long text with tags that will ". + "be chopped off but should be added back again
Long text with " . + "tags that ...