Index: modules/node/node.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.module,v
retrieving revision 1.898
diff -u -p -r1.898 node.module
--- modules/node/node.module	5 Nov 2007 15:14:54 -0000	1.898
+++ modules/node/node.module	8 Nov 2007 02:23:52 -0000
@@ -283,36 +283,49 @@ function node_teaser($body, $format = NU
     return $body;
   }
 
+  // If the delimiter has not been specified, try to split at paragraph or
+  // sentence boundaries.
+
   // The teaser may not be longer than maximum length specified. Initial slice.
   $teaser = truncate_utf8($body, $size);
-  $position = 0;
-  // Cache the reverse of the teaser.
+
+  // Set $max_length to the actual length of the UTF8 string -- which might not
+  // be the same as $size.
+  $max_length = strlen($teaser);
+
+  // How much to cut off the end of body to make the teaser. Initialize it to
+  // the largest possible value.
+  $min_length = $max_length;
+
+  // Store the reverse of the teaser.  We use strpos on the reversed needle and
+  // haystack for speed and convenience.
   $reversed = strrev($teaser);
 
-  // In some cases, no delimiter has been specified. In this case, we try to
-  // split at paragraph boundaries.
-  $breakpoints = array('</p>' => 0, '<br />' => 6, '<br>' => 4, "\n" => 1);
-  // We use strpos on the reversed needle and haystack for speed.
-  foreach ($breakpoints as $point => $offset) {
+  // If there's a line or paragraph break before the end of sliced teaser,
+  // break there.
+  $line_breaks = array('</p>' => 0, '<br />' => 6, '<br>' => 4, "\n" => 1);
+  foreach ($line_breaks as $point => $offset) {
     $length = strpos($reversed, strrev($point));
     if ($length !== FALSE) {
-      $position = - $length - $offset;
-      return ($position == 0) ? $teaser : substr($teaser, 0, $position);
+      $min_length = min($length + $offset, $min_length);
     }
   }
 
-  // When even the first paragraph is too long, we try to split at the end of
-  // the last full sentence.
-  $breakpoints = array('. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '؟ ' => 1);
-  $min_length = strlen($reversed);
-  foreach ($breakpoints as $point => $offset) {
-    $length = strpos($reversed, strrev($point));
-    if ($length !== FALSE) {
-      $min_length = min($length, $min_length);
-      $position = 0 - $length - $offset;
+  // If the first paragraph is too long, try to split it at the end of the last
+  // full sentence.
+  if ($min_length === $max_length) {
+    $sentence_breaks = array('. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '؟ ' => 1);
+    foreach ($sentence_breaks as $point => $offset) {
+      $length = strpos($reversed, strrev($point));
+      if ($length !== FALSE) {
+        $min_length = min($length + $offset, $min_length);
+      }
     }
   }
-  return ($position == 0) ? $teaser : substr($teaser, 0, $position);
+
+  // If the first sentence is too long, don't split any more.  Return the initial
+  // slice.
+  return ($min_length === $max_length) ? $teaser : substr($teaser, 0, 0 - $min_length);
 }
 
 /**
