[pLog-svn] r2529 - plog/branches/plog-1.0.2/class/data

oscar at devel.plogworld.net oscar at devel.plogworld.net
Sun Oct 16 20:58:37 GMT 2005


Author: oscar
Date: 2005-10-16 20:58:36 +0000 (Sun, 16 Oct 2005)
New Revision: 2529

Modified:
   plog/branches/plog-1.0.2/class/data/textfilter.class.php
Log:
Updated version of the balanceTags code from WP.

Modified: plog/branches/plog-1.0.2/class/data/textfilter.class.php
===================================================================
--- plog/branches/plog-1.0.2/class/data/textfilter.class.php	2005-10-15 15:03:59 UTC (rev 2528)
+++ plog/branches/plog-1.0.2/class/data/textfilter.class.php	2005-10-16 20:58:36 UTC (rev 2529)
@@ -279,12 +279,7 @@
          *            1.0  First Version
          */
         function balanceTags($text, $is_comment = 0) 
-        {
-            
-            if (get_settings('use_balanceTags') == 0) {
-                return $text;
-            }
-        
+        {        
             $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
         
             # WP bug fix for comments - in case you REALLY meant to type '< !--'
@@ -293,10 +288,10 @@
             $text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
         
             while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) {
-                $newtext = $newtext . $tagqueue;
+                $newtext .= $tagqueue;
         
                 $i = strpos($text,$regex[0]);
-                $l = strlen($tagqueue) + strlen($regex[0]);
+                $l = strlen($regex[0]);
         
                 // clear the shifter
                 $tagqueue = '';
@@ -332,32 +327,46 @@
         
                     // Tag Cleaning
         
-                    // Push if not img or br or hr
-                    if($tag != 'br' && $tag != 'img' && $tag != 'hr') {
+                    // If self-closing or '', don't do anything.
+                    if((substr($regex[2],-1) == '/') || ($tag == '')) {
+                    }
+                    // ElseIf it's a known single-entity tag but it doesn't close itself, do so
+                    elseif ($tag == 'br' || $tag == 'img' || $tag == 'hr' || $tag == 'input') {
+                        $regex[2] .= '/';
+                    } else {	// Push the tag onto the stack
+                        // If the top of the stack is the same as the tag we want to push, close previous tag
+                        if (($stacksize > 0) && ($tag != 'div') && ($tagstack[$stacksize - 1] == $tag)) {
+                            $tagqueue = '</' . array_pop ($tagstack) . '>';
+                            $stacksize--;
+                        }
                         $stacksize = array_push ($tagstack, $tag);
                     }
         
                     // Attributes
-                    // $attributes = $regex[2];
                     $attributes = $regex[2];
                     if($attributes) {
                         $attributes = ' '.$attributes;
                     }
                     $tag = '<'.$tag.$attributes.'>';
+                    //If already queuing a close tag, then put this tag on, too
+                    if ($tagqueue) {
+                        $tagqueue .= $tag;
+                        $tag = '';
+                    }
                 }
                 $newtext .= substr($text,0,$i) . $tag;
                 $text = substr($text,$i+$l);
             }  
         
             // Clear Tag Queue
-            $newtext = $newtext . $tagqueue;
+            $newtext .= $tagqueue;
         
             // Add Remaining text
             $newtext .= $text;
         
             // Empty Stack
             while($x = array_pop($tagstack)) {
-                $newtext = $newtext . '</' . $x . '>'; // Add remaining tags to close      
+                $newtext .= '</' . $x . '>'; // Add remaining tags to close
             }
         
             // WP fix for the bug with HTML comments




More information about the pLog-svn mailing list