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;
$tagqueue = '';
$newtext = '';
$single_tags = array( 'br', 'hr', 'img', 'input' ); // Known single-entity/self-closing tags
$nestable_tags = array( 'blockquote', 'div', 'object', 'q', 'span' ); // Tags that can be immediately nested within themselves
// Known single-entity/self-closing tags
$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 '< !--'
$text = str_replace('< !--', '< !--', $text);
@ -1203,14 +1205,23 @@ function force_balance_tags( $text ) {
// Tag Cleaning
// If self-closing or '', don't do anything.
if ( substr($regex[2],-1) == '/' || $tag == '' ) {
// If it's an empty tag "< >", do nothing
if ( '' == $tag ) {
// 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 ( in_array($tag, $single_tags) ) {
$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 ( $stacksize > 0 && !in_array($tag, $nestable_tags) && $tagstack[$stacksize - 1] == $tag ) {
$tagqueue = '</' . array_pop( $tagstack ) . '>';
@ -1221,7 +1232,7 @@ function force_balance_tags( $text ) {
// Attributes
$attributes = $regex[2];
if( !empty($attributes) )
if( ! empty( $attributes ) && $attributes[0] != '>' )
$attributes = ' ' . $attributes;
$tag = '<' . $tag . $attributes . '>';