[pLog-svn] r7149 - plog/branches/lifetype-1.2/class/data
jondaley at devel.lifetype.net
jondaley at devel.lifetype.net
Wed Oct 26 19:31:03 EDT 2011
Author: jondaley
Date: 2011-10-26 19:31:03 -0400 (Wed, 26 Oct 2011)
New Revision: 7149
Modified:
plog/branches/lifetype-1.2/class/data/textfilter.class.php
Log:
mostly whitespace change. though I removed the HTML comment thing - that was from WP and not needed in our installation. this version of balanceTags comes from WP (via raciloni: http://bugs.lifetype.net/view.php?id=1660)
Modified: plog/branches/lifetype-1.2/class/data/textfilter.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/data/textfilter.class.php 2011-10-26 22:09:02 UTC (rev 7148)
+++ plog/branches/lifetype-1.2/class/data/textfilter.class.php 2011-10-26 23:31:03 UTC (rev 7149)
@@ -66,8 +66,6 @@
$htmlAllowedTags = $config->getValue( "html_allowed_tags_in_comments" );
}
- // WP bug fix for comments - in case you REALLY meant to type '< !--'
- $string = str_replace('< !--', '< !--', $string);
// WP bug fix for LOVE <3 (and other situations with '<' before a number)
$string = preg_replace('#<([0-9]+)#', '<$1', $string);
// some nice people might like to use "< >" without meaning to do HTML?
@@ -75,8 +73,9 @@
$string = strip_tags( $string, $htmlAllowedTags );
$string = TextFilter::balanceTags($string);
- $string = TextFilter::filterJavaScript( $string );
+ $string = TextFilter::filterJavaScript( $string );
+
return $string;
}
@@ -299,41 +298,48 @@
* Added Cleaning Hooks
* 1.0 First Version
*/
- function balanceTags($text)
- {
+ function balanceTags($text){
$tagstack = array();
$stacksize = 0;
$tagqueue = '';
$newtext = '';
+
+ // Known single-entity/self-closing tags
+ $single_tags = array('br', 'hr', 'img', 'input');
+ // Tags that can be immediately nested within themselves
+ $nestable_tags = array('blockquote', 'div', 'span');
- while (preg_match("#<(/?\w*)\s*([^>]*)>#",$text,$regex)) {
+ // WP bug fix for LOVE <3 (and other situations with '<' before a number)
+ $text = preg_replace('#<([0-9]{1})#', '<$1', $text);
+
+ while ( preg_match("/<(\/?[\w:]*)\s*([^>]*)>/", $text, $regex) ) {
$newtext .= $tagqueue;
-
- $i = strpos($text,$regex[0]);
+
+ $i = strpos($text, $regex[0]);
$l = strlen($regex[0]);
-
- // clear the shifter
+
+ // clear the shifter
$tagqueue = '';
- // Pop or Push
- if ($regex[1][0] == "/") { // End Tag
+ // Pop or Push
+ if ( isset($regex[1][0]) && '/' == $regex[1][0] ) { // End Tag
$tag = strtolower(substr($regex[1],1));
- // if too many closing tags
- if($stacksize <= 0) {
+ // if too many closing tags
+ if( $stacksize <= 0 ) {
$tag = '';
- //or close to be safe $tag = '/' . $tag;
+ // or close to be safe $tag = '/' . $tag;
}
- // if stacktop value = tag close value then pop
- else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag
+ // if stacktop value = tag close value then pop
+ else if ( $tagstack[$stacksize - 1] == $tag ) { // found closing tag
$tag = '</' . $tag . '>'; // Close Tag
- // Pop
- array_pop ($tagstack);
+ // Pop
+ array_pop( $tagstack );
$stacksize--;
} else { // closing tag not at top, search for it
- for ($j=$stacksize-1;$j>=0;$j--) {
- if ($tagstack[$j] == $tag) {
- // add tag to tagqueue
- for ($k=$stacksize-1;$k>=$j;$k--){
- $tagqueue .= '</' . array_pop ($tagstack) . '>';
+ for ( $j = $stacksize-1; $j >= 0; $j-- ) {
+ if ( $tagstack[$j] == $tag ) {
+ // add tag to tagqueue
+ for ( $k = $stacksize-1; $k >= $j; $k--) {
+ $tagqueue .= '</' . array_pop( $tagstack ) . '>';
$stacksize--;
}
break;
@@ -343,55 +349,51 @@
}
} else { // Begin Tag
$tag = strtolower($regex[1]);
-
- // Tag Cleaning
-
- // If self-closing or '', don't do anything.
- if((substr($regex[2],-1) == '/') || ($tag == '')) {
+
+ // Tag Cleaning
+
+ // If self-closing or '', don't do anything.
+ if ( substr($regex[2],-1) == '/' || $tag == '' ) {
+ // do nothing
}
- // 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') {
+ // ElseIf it's a known single-entity tag but it doesn't close itself, do so
+ elseif ( in_array($tag, $single_tags) ) {
$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)) {
+ // If the top of the stack is the same as the tag we want to push, close previous tag
+ if ( $stacksize > 0 && !in_array($tag, $nestable_tags) && $tagstack[$stacksize - 1] == $tag ) {
$tagqueue = '</' . array_pop ($tagstack) . '>';
$stacksize--;
}
$stacksize = array_push ($tagstack, $tag);
}
-
- // Attributes
+
+ // Attributes
$attributes = $regex[2];
- if($attributes) {
+ if( !empty($attributes) )
$attributes = ' '.$attributes;
- }
- $tag = '<'.$tag.$attributes.'>';
- //If already queuing a close tag, then put this tag on, too
- if ($tagqueue) {
+
+ $tag = '<' . $tag . $attributes . '>';
+ //If already queuing a close tag, then put this tag on, too
+ if ( !empty($tagqueue) ) {
$tagqueue .= $tag;
$tag = '';
}
}
- $newtext .= substr($text,0,$i) . $tag;
- $text = substr($text,$i+$l);
- }
-
- // Clear Tag Queue
+ $newtext .= substr($text, 0, $i) . $tag;
+ $text = substr($text, $i + $l);
+ }
+
+ // Clear Tag Queue
$newtext .= $tagqueue;
-
- // Add Remaining text
+
+ // Add Remaining text
$newtext .= $text;
-
- // Empty Stack
- while($x = array_pop($tagstack)) {
+
+ // Empty Stack
+ while( $x = array_pop($tagstack) )
$newtext .= '</' . $x . '>'; // Add remaining tags to close
- }
-
- // WP fix for the bug with HTML comments
- $newtext = str_replace("< !--","<!--",$newtext);
- $newtext = str_replace("< !--","< !--",$newtext);
-
+
return $newtext;
}
More information about the pLog-svn
mailing list