Whitespace and coding standards cleanup for balanceTags(). See #11968

git-svn-id: http://svn.automattic.com/wordpress/trunk@13914 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
dd32 2010-04-01 13:18:34 +00:00
parent 2660f3f8f3
commit 3793a4fee2
1 changed files with 28 additions and 25 deletions

View File

@ -1028,19 +1028,22 @@ function balanceTags( $text, $force = false ) {
* @return string Balanced text.
*/
function force_balance_tags( $text ) {
$tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
$single_tags = array('br', 'hr', 'img', 'input'); //Known single-entity/self-closing tags
$nestable_tags = array('blockquote', 'div', 'span'); //Tags that can be immediately nested within themselves
$tagstack = array();
$stacksize = 0;
$tagqueue = '';
$newtext = '';
$single_tags = array('br', 'hr', 'img', 'input'); // Known single-entity/self-closing tags
$nestable_tags = array('blockquote', 'div', 'span'); // Tags that can be immediately nested within themselves
# WP bug fix for comments - in case you REALLY meant to type '< !--'
// WP bug fix for comments - in case you REALLY meant to type '< !--'
$text = str_replace('< !--', '< !--', $text);
# WP bug fix for LOVE <3 (and other situations with '<' before a number)
// WP bug fix for LOVE <3 (and other situations with '<' before a number)
$text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
while (preg_match("/<(\/?[\w:]*)\s*([^>]*)>/",$text,$regex)) {
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
@ -1049,22 +1052,22 @@ function force_balance_tags( $text ) {
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( $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
else if ( $tagstack[$stacksize - 1] == $tag ) { // found closing tag
$tag = '</' . $tag . '>'; // Close Tag
// Pop
array_pop ($tagstack);
array_pop( $tagstack );
$stacksize--;
} else { // closing tag not at top, search for it
for ($j=$stacksize-1;$j>=0;$j--) {
if ($tagstack[$j] == $tag) {
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 ( $k = $stacksize-1; $k >= $j; $k--) {
$tagqueue .= '</' . array_pop( $tagstack ) . '>';
$stacksize--;
}
break;
@ -1078,14 +1081,15 @@ function force_balance_tags( $text ) {
// Tag Cleaning
// If self-closing or '', don't do anything.
if((substr($regex[2],-1) == '/') || ($tag == '')) {
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 ( 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) && !in_array($tag, $nestable_tags) && ($tagstack[$stacksize - 1] == $tag)) {
if ( $stacksize > 0 && !in_array($tag, $nestable_tags) && $tagstack[$stacksize - 1] == $tag ) {
$tagqueue = '</' . array_pop ($tagstack) . '>';
$stacksize--;
}
@ -1094,18 +1098,18 @@ function force_balance_tags( $text ) {
// Attributes
$attributes = $regex[2];
if($attributes) {
if( !empty($attributes) )
$attributes = ' '.$attributes;
}
$tag = '<'.$tag.$attributes.'>';
$tag = '<' . $tag . $attributes . '>';
//If already queuing a close tag, then put this tag on, too
if ($tagqueue) {
if ( !empty($tagqueue) ) {
$tagqueue .= $tag;
$tag = '';
}
}
$newtext .= substr($text,0,$i) . $tag;
$text = substr($text,$i+$l);
$newtext .= substr($text, 0, $i) . $tag;
$text = substr($text, $i + $l);
}
// Clear Tag Queue
@ -1115,9 +1119,8 @@ function force_balance_tags( $text ) {
$newtext .= $text;
// Empty Stack
while($x = array_pop($tagstack)) {
while( $x = array_pop($tagstack) )
$newtext .= '</' . $x . '>'; // Add remaining tags to close
}
// WP fix for the bug with HTML comments
$newtext = str_replace("< !--","<!--",$newtext);