When no delimiter has been specified it tries to split at paragraph boundaries (when it finds a <p> or <br /> or a linebreak etc.). The problem is that it does't check well enough how much actual content will remain. When you have a really long first paragraph this can cause great trouble. When using tinyMCE I get Code like this:

<p>
Lorem ipsum dolor....

In other words there's a linebrek just after the <p> and if the text in the paragraph is longer than the specified trim-lenght node_teaser will cut my teaser text at that linebreak. Leaving me with no text and a broken <p> tag...

For the time beeing I have altered my node.module and added $half_size = $size / 2; and && $length < $half_size


  // 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);

  // Introducing $half_size
  $half_size = $size / 2;

  foreach ($breakpoints as $point => $offset) {
    $length = strpos($reversed, strrev($point));
    // Checking that the length of what we cut off is not longer than half the teaser text length asked for
    // Otherwise we let the function go on and split at the end of the last full sentence
    if ($length !== FALSE && $length < $half_size) {
      $position = - $length - $offset;
      return ($position == 0) ? $teaser : substr($teaser, 0, $position);
    }
  }

This works OK for me now, get alot of broken <p> tags that doesn't validate though, an additional function to validate the HTML and close open tags would be nice.

Comments

gpk’s picture

Thanks for posting this issue snobojohan, I was literally just about to do same but you beat me to it!

Previously I reported same problem in a forum topic http://drupal.org/node/160241.

jscheel’s picture

Category: feature » bug

I can confirm this. Fix seems to work fine.

Bevan’s picture

Status: Active » Closed (duplicate)

Please check if this is the same as the issue described at http://drupal.org/node/180425

gpk’s picture

Version: 5.2 » 6.x-dev

It's not a duplicate of that issue but of http://drupal.org/node/155337

manerhabe’s picture

Thanks so much! This fixed the really strange teasers I was getting.

Bevan’s picture

Version: 6.x-dev » 7.x-dev
Status: Closed (duplicate) » Fixed

This has been improved on in http://drupal.org/node/155337 and http://drupal.org/node/180425.

To the point where the issue described can probably be considered fixed.

Feel free to reset status to active if you plan on contributing this as a patch. I think any patch introducing the approach here would be a feature and need to go into drupal 7.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.