Make WordPress Core


Ignore:
Timestamp:
09/13/2012 04:39:06 PM (12 years ago)
Author:
nacin
Message:

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

props coffee2code.
fixes #1597.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/formatting.php

    r21718 r21828  
    11561156    $tagqueue = '';
    11571157    $newtext = '';
    1158     $single_tags = array( 'br', 'hr', 'img', 'input' ); // Known single-entity/self-closing tags
    1159     $nestable_tags = array( 'blockquote', 'div', 'object', 'q', 'span' ); // Tags that can be immediately nested within themselves
     1158    // Known single-entity/self-closing tags
     1159    $single_tags = array( 'area', 'base', 'basefont', 'br', 'col', 'command', 'embed', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param', 'source' );
     1160    // Tags that can be immediately nested within themselves
     1161    $nestable_tags = array( 'blockquote', 'div', 'object', 'q', 'span' );
    11601162
    11611163    // WP bug fix for comments - in case you REALLY meant to type '< !--'
     
    12041206            // Tag Cleaning
    12051207
    1206             // If self-closing or '', don't do anything.
    1207             if ( substr($regex[2],-1) == '/' || $tag == '' ) {
     1208            // If it's an empty tag "< >", do nothing
     1209            if ( '' == $tag ) {
    12081210                // do nothing
     1211            }
     1212            // ElseIf it presents itself as a self-closing tag...
     1213            elseif ( substr( $regex[2], -1 ) == '/' ) {
     1214                // ...but it isn't a known single-entity self-closing tag, then don't let it be treated as such and
     1215                // immediately close it with a closing tag (the tag will encapsulate no text as a result)
     1216                if ( ! in_array( $tag, $single_tags ) )
     1217                    $regex[2] = trim( substr( $regex[2], 0, -1 ) ) . "></$tag";
    12091218            }
    12101219            // ElseIf it's a known single-entity tag but it doesn't close itself, do so
    12111220            elseif ( in_array($tag, $single_tags) ) {
    12121221                $regex[2] .= '/';
    1213             } else {    // Push the tag onto the stack
     1222            }
     1223            // Else it's not a single-entity tag
     1224            else {
    12141225                // If the top of the stack is the same as the tag we want to push, close previous tag
    12151226                if ( $stacksize > 0 && !in_array($tag, $nestable_tags) && $tagstack[$stacksize - 1] == $tag ) {
    1216                     $tagqueue = '</' . array_pop ($tagstack) . '>';
     1227                    $tagqueue = '</' . array_pop( $tagstack ) . '>';
    12171228                    $stacksize--;
    12181229                }
    1219                 $stacksize = array_push ($tagstack, $tag);
     1230                $stacksize = array_push( $tagstack, $tag );
    12201231            }
    12211232
    12221233            // Attributes
    12231234            $attributes = $regex[2];
    1224             if( !empty($attributes) )
    1225                 $attributes = ' '.$attributes;
     1235            if( ! empty( $attributes ) && $attributes[0] != '>' )
     1236                $attributes = ' ' . $attributes;
    12261237
    12271238            $tag = '<' . $tag . $attributes . '>';
Note: See TracChangeset for help on using the changeset viewer.