*** collapse_text.module	2008-12-15 10:47:33.000000000 -0500
--- collapse_text.module.patched	2010-01-04 15:49:27.000000000 -0500
***************
*** 61,78 ****
   */
  function collapse_text_process($text) {
    // Per #259535 and #233877, add ability to specify title
!   // in collapse text. Thanks rivena, Justyn
!   $text = preg_replace_callback('/
        \[                         # look for an opening bracket
           collapse                # followed by the word `collapse`
           (\ collapsed)?          # followed by (optionally) a space and the word `collapsed` (captured)
           (?:\ title=([^\]]*))?   # followed by (optionally) a space and a title, consisting of any
                                   # characters except a close bracket (captured)
        \]                         # followed by a closing bracket
        (.+?)                      # then capture as few characters as possible until
        \[\/collapse\]             # a closing "tag", which is a slash followed by `collapse` in brackets
      /smx',
      "_collapse_text_replace_callback", $text);
    return $text;
  }
  
--- 61,91 ----
   */
  function collapse_text_process($text) {
    // Per #259535 and #233877, add ability to specify title
!   // in collapse text. Thanks rivena, Justyn
!   //pull out old collapse statement
! 
!   $pattern = "/\[collapse(.|\n|\r)*\[\/collapse\]/";
!   preg_match($pattern, $text, $matches);
! 
!   $new_text = preg_replace_callback('/
!       (?:<p.*?>)?                # remove paragraph is right before and after
        \[                         # look for an opening bracket
           collapse                # followed by the word `collapse`
           (\ collapsed)?          # followed by (optionally) a space and the word `collapsed` (captured)
           (?:\ title=([^\]]*))?   # followed by (optionally) a space and a title, consisting of any
                                   # characters except a close bracket (captured)
        \]                         # followed by a closing bracket
+       (?:<\/p\s*>)?              # remove paragraph is right before and after
        (.+?)                      # then capture as few characters as possible until
+       (?:<p.*?>)?                # remove paragraph is right before and after
        \[\/collapse\]             # a closing "tag", which is a slash followed by `collapse` in brackets
+       (?:<\/p\s*>)?              # remove paragraph is right before and after
      /smx',
      "_collapse_text_replace_callback", $text);
+   //$text now contains our replacement text. We do not want to completely override the old text so we will search for the old
+   //replace it with the new and then return that text.
+ 
+   $text = str_replace($matches[0], $new_text, $text);
    return $text;
  }
  
