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

git-svn-id: https://develop.svn.wordpress.org/trunk@13914 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dion Hulse 2010-04-01 13:18:34 +00:00
parent 6edd892710
commit 90dd44e0c2

View File

@ -1028,13 +1028,16 @@ function balanceTags( $text, $force = false ) {
* @return string Balanced text. * @return string Balanced text.
*/ */
function force_balance_tags( $text ) { function force_balance_tags( $text ) {
$tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = ''; $tagstack = array();
$stacksize = 0;
$tagqueue = '';
$newtext = '';
$single_tags = array('br', 'hr', 'img', 'input'); // Known single-entity/self-closing tags $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 $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); $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); $text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
while ( preg_match("/<(\/?[\w:]*)\s*([^>]*)>/", $text, $regex) ) { while ( preg_match("/<(\/?[\w:]*)\s*([^>]*)>/", $text, $regex) ) {
@ -1078,14 +1081,15 @@ function force_balance_tags( $text ) {
// Tag Cleaning // Tag Cleaning
// If self-closing or '', don't do anything. // 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 it's a known single-entity tag but it doesn't close itself, do so
elseif ( in_array($tag, $single_tags) ) { elseif ( in_array($tag, $single_tags) ) {
$regex[2] .= '/'; $regex[2] .= '/';
} else { // Push the tag onto the stack } 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 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) . '>'; $tagqueue = '</' . array_pop ($tagstack) . '>';
$stacksize--; $stacksize--;
} }
@ -1094,12 +1098,12 @@ function force_balance_tags( $text ) {
// Attributes // Attributes
$attributes = $regex[2]; $attributes = $regex[2];
if($attributes) { if( !empty($attributes) )
$attributes = ' '.$attributes; $attributes = ' '.$attributes;
}
$tag = '<' . $tag . $attributes . '>'; $tag = '<' . $tag . $attributes . '>';
//If already queuing a close tag, then put this tag on, too //If already queuing a close tag, then put this tag on, too
if ($tagqueue) { if ( !empty($tagqueue) ) {
$tagqueue .= $tag; $tagqueue .= $tag;
$tag = ''; $tag = '';
} }
@ -1115,9 +1119,8 @@ function force_balance_tags( $text ) {
$newtext .= $text; $newtext .= $text;
// Empty Stack // Empty Stack
while($x = array_pop($tagstack)) { while( $x = array_pop($tagstack) )
$newtext .= '</' . $x . '>'; // Add remaining tags to close $newtext .= '</' . $x . '>'; // Add remaining tags to close
}
// WP fix for the bug with HTML comments // WP fix for the bug with HTML comments
$newtext = str_replace("< !--","<!--",$newtext); $newtext = str_replace("< !--","<!--",$newtext);