When balancing tags, properly close tags that shouldn't be self-closed but are. Support all self-closing tags.

props coffee2code.
fixes #1597.



git-svn-id: http://core.svn.wordpress.org/trunk@21828 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2012-09-13 16:39:06 +00:00
parent 54e24f964a
commit f54edf701a
1 changed files with 20 additions and 9 deletions

View File

@ -1155,8 +1155,10 @@ function force_balance_tags( $text ) {
$stacksize = 0; $stacksize = 0;
$tagqueue = ''; $tagqueue = '';
$newtext = ''; $newtext = '';
$single_tags = array( 'br', 'hr', 'img', 'input' ); // Known single-entity/self-closing tags // Known single-entity/self-closing tags
$nestable_tags = array( 'blockquote', 'div', 'object', 'q', 'span' ); // Tags that can be immediately nested within themselves $single_tags = array( 'area', 'base', 'basefont', 'br', 'col', 'command', 'embed', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param', 'source' );
// Tags that can be immediately nested within themselves
$nestable_tags = array( 'blockquote', 'div', 'object', 'q', 'span' );
// 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);
@ -1203,26 +1205,35 @@ function force_balance_tags( $text ) {
// Tag Cleaning // Tag Cleaning
// If self-closing or '', don't do anything. // If it's an empty tag "< >", do nothing
if ( substr($regex[2],-1) == '/' || $tag == '' ) { if ( '' == $tag ) {
// do nothing // do nothing
} }
// ElseIf it presents itself as a self-closing tag...
elseif ( substr( $regex[2], -1 ) == '/' ) {
// ...but it isn't a known single-entity self-closing tag, then don't let it be treated as such and
// immediately close it with a closing tag (the tag will encapsulate no text as a result)
if ( ! in_array( $tag, $single_tags ) )
$regex[2] = trim( substr( $regex[2], 0, -1 ) ) . "></$tag";
}
// 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 it's not a single-entity tag
else {
// 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--;
} }
$stacksize = array_push ($tagstack, $tag); $stacksize = array_push( $tagstack, $tag );
} }
// Attributes // Attributes
$attributes = $regex[2]; $attributes = $regex[2];
if( !empty($attributes) ) if( ! empty( $attributes ) && $attributes[0] != '>' )
$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