Skip to:
Content

bbPress.org

Changeset 8145


Ignore:
Timestamp:
10/26/2026 08:06:55 PM (2 weeks ago)
Author:
johnjamesjacoby
Message:

Formatting: preserve source when wrapping list items.

Use public WordPress HTML APIs to validate straightforward orphan list items, then insert only the missing unordered-list wrappers. Preserve original markup and character references, and leave ambiguous or complicated fragments unchanged.

Avoid source matching for valid lists and skip list-like tag names before creating a parser.

In branches/2.6, for 2.6.21.

See #2357.

Location:
branches/2.6
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2.6/CHANGELOG.md

    r8143 r8145  
    3939- Kept the reply editor's Forum and Reply To dropdowns synchronized with its
    4040  Topic, and made Forum read-only.
    41 - Wrapped standalone list items in topic and reply content on WordPress 6.9+
    42   to prevent missing list containers from affecting the surrounding layout,
    43   including content with character references, while preserving encoded text.
     41- Wrapped standalone list items in straightforward topic and reply HTML on
     42  WordPress 6.9+, while preserving original markup and encoded text.
    4443- Kept generated bbPress content available to excerpt-based templates without
    4544  causing themes that display both excerpts and content to duplicate it.
  • branches/2.6/src/includes/common/formatting.php

    r8141 r8145  
    175175 *
    176176 * Runs after force_balance_tags(), which closes items but does not supply
    177  * missing parent lists. Requires the public HTML serialization API available
    178  * in WordPress 6.9. Repaired fragments are normalized by the HTML Processor.
     177 * missing parent lists. Requires the HTML API available in WordPress 6.9.
     178 * Public HTML APIs validate explicit tag structure before wrappers are added.
     179 * All original source bytes are retained. Implicit closures, foreign content,
     180 * and ambiguous literal list-item tags are left unchanged.
    179181 * Code examples and block content are excluded from repair. Fragments without
    180182 * orphan list items and sites using other character encodings are unchanged.
    … …  
    189191 */
    190192function bbp_wrap_orphan_list_items( $content = '', $post_id = 0 ) {
    191         if ( ! class_exists( 'WP_HTML_Processor' ) || ( false === stripos( $content, '<li' ) ) || has_blocks( $content ) ) {
     193        if ( ! class_exists( 'WP_HTML_Processor' ) || false === stripos( $content, '<li' ) || ! preg_match( '/<li(?=[\t\n\f\r \/>])/i', $content ) || has_blocks( $content ) ) {
    192194                return $content;
    193195        }
    194 
    195196        if ( ! empty( $post_id ) && has_blocks( get_post_field( 'post_content', $post_id, 'raw' ) ) ) {
    196197                return $content;
    197198        }
    198 
    199199        $processor = WP_HTML_Processor::create_fragment( $content );
    200200        if ( ! is_callable( array( $processor, 'serialize_token' ) ) || ! is_utf8_charset() ) {
    201201                return $content;
    202202        }
    203 
    204         // Protect reference spelling without decoding punctuation for later filters.
    205         $original_processor = null;
    206         $placeholder        = '';
    207         if ( false !== strpos( $content, '&' ) ) {
    208                 // Try at most 16 private-use characters, each only three UTF-8 bytes.
    209                 $placeholder_source = str_replace( "\x00", '', $content );
    210                 for ( $attempt = 0; $attempt < 16; ++$attempt ) {
    211                         $candidate = "\xEE\x80" . chr( 0x80 + $attempt );
    212                         if ( false === strpos( $placeholder_source, $candidate ) ) {
    213                                 $placeholder = $candidate;
    214                                 break;
    215                         }
    216                 }
    217                 if ( '' === $placeholder ) {
     203        $excluded = array( 'UL', 'OL', 'MENU', 'DIR', 'PRE', 'CODE', 'LISTING' );
     204        $orphan = false;
     205        // Valid lists need only this initial semantic scan.
     206        while ( $processor->next_tag( 'LI' ) ) {
     207                if ( ! array_intersect( $excluded, $processor->get_breadcrumbs() ) ) {
     208                        $orphan = true;
     209                        break;
     210                }
     211        }
     212        if ( ! $orphan ) {
     213                return $content;
     214        }
     215
     216        // Match source boundaries only when lexical and semantic tags agree exactly.
     217        $item_pattern = '/<\/?li(?=[\t\n\f\r \/>])/i';
     218        $processor = WP_HTML_Processor::create_fragment( $content );
     219        $tags = new WP_HTML_Tag_Processor( $content );
     220        $query = array( 'tag_closers' => 'visit' );
     221        $insertions = array();
     222        $search = 0;
     223        $depth = 0;
     224        $end = 0;
     225        while ( $tags->next_tag( $query ) ) {
     226                if ( ! $processor->next_tag( $query ) || $tags->get_tag() !== $processor->get_tag() || $tags->is_tag_closer() !== $processor->is_tag_closer() || 'html' !== $processor->get_namespace() ) {
    218227                        return $content;
    219228                }
    220                 $original_processor = $processor;
    221                 $processor          = WP_HTML_Processor::create_fragment( str_replace( '&', $placeholder, $content ) );
    222         }
    223 
    224         $output          = '';
    225         $depth           = 0;
    226         $in_item         = false;
    227         $changed         = false;
    228         $pending_newline = false;
    229         $original_text   = '';
    230 
    231         while ( $processor->next_token() ) {
    232                 // References may split text tokens, but must not change the element tree.
    233                 if ( $original_processor && ( '#text' !== $processor->get_token_type() ) ) {
    234                         do {
    235                                 $matched = $original_processor->next_token();
    236                                 if ( $matched ) {
    237                                         $original_text .= $original_processor->get_modifiable_text();
    238                                 }
    239                         } while ( $matched && ( '#text' === $original_processor->get_token_type() ) );
    240                         if ( ! $matched
    241                                 || ( $original_processor->get_token_type() !== $processor->get_token_type() )
    242                                 || ( $original_processor->get_tag() !== $processor->get_tag() )
    243                                 || ( $original_processor->is_tag_closer() !== $processor->is_tag_closer() )
    244                                 || ( $original_processor->get_namespace() !== $processor->get_namespace() )
    245                                 || ( $original_processor->get_breadcrumbs() !== $processor->get_breadcrumbs() )
    246                         ) {
     229                if ( 'LI' !== $tags->get_tag() ) {
     230                        continue;
     231                }
     232                if ( ! preg_match( $item_pattern, $content, $item, PREG_OFFSET_CAPTURE, $search ) || $tags->is_tag_closer() !== ( '/' === $item[0][0][1] ) ) {
     233                        return $content;
     234                }
     235                $offset = $item[0][1];
     236                $search = $offset + strlen( $item[0][0] );
     237                if ( $tags->is_tag_closer() ) {
     238                        if ( ! preg_match( '/\G<\/li[\t\n\f\r ]*>/i', $content, $closer, 0, $offset ) ) {
    247239                                return $content;
    248240                        }
    249                 }
    250 
    251                 $tag         = $processor->get_tag();
    252                 $type        = $processor->get_token_type();
    253                 $closing     = $processor->is_tag_closer();
    254                 $breadcrumbs = $processor->get_breadcrumbs();
    255                 $is_item     = ( 'LI' === $tag ) && ! $closing && ! array_intersect(
    256                         array( 'UL', 'OL', 'MENU', 'DIR', 'PRE', 'CODE', 'LISTING' ),
    257                         $breadcrumbs
    258                 );
    259                 $text        = $processor->get_modifiable_text();
    260                 if ( $original_processor && ( '#text' === $type ) ) {
    261                         $text = WP_HTML_Decoder::decode_text_node( str_replace( $placeholder, '&', $text ) );
    262                 }
    263                 $is_spacing  = ( '#comment' === $type ) || ( ( '#text' === $type ) && ( '' === trim( $text, " \t\n\r\f" ) ) );
    264 
    265                 // A run ends at text or another element, not at whitespace or a comment.
    266                 if ( $depth && ! $in_item && ! $is_item && ! $is_spacing ) {
    267                         $output .= '</ul>';
    268                         $depth   = 0;
    269                 }
    270 
    271                 if ( $is_item ) {
    272                         if ( ! $depth ) {
    273                                 $output .= '<ul>';
    274                                 $depth   = count( $breadcrumbs ) - 1;
    275                                 $changed = true;
     241                        if ( $depth && $processor->get_current_depth() === $depth - 1 ) {
     242                                $end = $offset + strlen( $closer[0] );
     243                                $insertions[ $end ] = '</ul>';
     244                                $depth = 0;
    276245                        }
    277                         $in_item = true;
    278                 } elseif ( $closing && ( 'LI' === $tag ) && ( count( $breadcrumbs ) === $depth ) ) {
    279                         $in_item = false;
    280                 }
    281 
    282                 $serialized = $processor->serialize_token();
    283                 if ( $pending_newline ) {
    284                         $text = $processor->get_modifiable_text();
    285                         if ( ( '#text' === $type ) && ( ( "\n" === substr( $text, 0, 1 ) ) || ( $original_processor && '' === $text ) ) ) {
    286                                 $output .= "\n";
     246                } elseif ( ! array_intersect( $excluded, $processor->get_breadcrumbs() ) ) {
     247                        if ( $depth ) {
     248                                return $content;
    287249                        }
    288                         $pending_newline = false;
    289                 }
    290 
    291                 // Preserve the newline consumed by HTML parsing, including on WordPress 6.9.
    292                 if ( ! $closing && ( 'html' === $processor->get_namespace() ) ) {
    293                         if ( in_array( $tag, array( 'PRE', 'LISTING' ), true ) ) {
    294                                 if ( "\n" === substr( $serialized, -1 ) ) {
    295                                         $serialized = substr( $serialized, 0, -1 );
    296                                 }
    297                                 $pending_newline = true;
    298                         } elseif ( 'TEXTAREA' === $tag ) {
    299                                 $expected = $processor->get_modifiable_text();
    300                                 $textarea = new WP_HTML_Tag_Processor( $serialized );
    301                                 if ( ! $textarea->next_tag() ) {
    302                                         return $content;
    303                                 }
    304                                 // Serialized attribute values have escaped angle brackets.
    305                                 $text_start = strpos( $serialized, '>' ) + 1;
    306                                 if ( $textarea->get_modifiable_text() !== $expected ) {
    307                                         if ( "\n" . $textarea->get_modifiable_text() !== $expected ) {
    308                                                 return $content;
    309                                         }
    310                                         $serialized = substr_replace( $serialized, "\n", $text_start, 0 );
    311                                 } elseif ( ( "\n" !== substr( $expected, 0, 1 ) ) && ( "\n" === substr( $serialized, $text_start, 1 ) ) ) {
    312                                         $serialized = substr_replace( $serialized, '', $text_start, 1 );
    313                                 }
    314                                 if ( $original_processor ) {
    315                                         // A consumed literal newline may precede another encoded newline.
    316                                         $textarea = new WP_HTML_Tag_Processor( str_replace( $placeholder, '&', $serialized ) );
    317                                         if ( ! $textarea->next_tag() ) {
    318                                                 return $content;
    319                                         }
    320                                         $expected = $original_processor->get_modifiable_text();
    321                                         if ( $textarea->get_modifiable_text() !== $expected ) {
    322                                                 if ( "\n" . $textarea->get_modifiable_text() !== $expected ) {
    323                                                         return $content;
    324                                                 }
    325                                                 $serialized = substr_replace( $serialized, "\n", $text_start, 0 );
     250                        $gap = $end ? substr( $content, $end, $offset - $end ) : '';
     251                        $adjacent = '' === trim( $gap, " \t\n\r\f" );
     252                        // Only whitespace and comments join consecutive items.
     253                        if ( ! $adjacent && ( false !== strpos( $gap, '<!--' ) || false !== strpos( $gap, '&' ) ) ) {
     254                                $spacing = new WP_HTML_Tag_Processor( $gap );
     255                                $adjacent = true;
     256                                while ( $spacing->next_token() ) {
     257                                        if ( '#comment' !== $spacing->get_token_type() && ( '#text' !== $spacing->get_token_type() || '' !== trim( $spacing->get_modifiable_text(), " \t\n\r\f" ) ) ) {
     258                                                $adjacent = false;
     259                                                break;
    326260                                        }
    327261                                }
    328262                        }
    329                         // Older serializers cannot preserve every opaque element's payload.
    330                         if ( in_array( $tag, array( 'IFRAME', 'NOEMBED', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEXTAREA', 'TITLE', 'XMP' ), true ) ) {
    331                                 $token = new WP_HTML_Tag_Processor( $serialized );
    332                                 if ( ! $token->next_tag() || ( $token->get_modifiable_text() !== $processor->get_modifiable_text() ) ) {
    333                                         return $content;
    334                                 }
     263                        if ( $end && $adjacent ) {
     264                                unset( $insertions[ $end ] );
     265                        } else {
     266                                $insertions[ $offset ] = '<ul>';
    335267                        }
    336                 }
    337 
    338                 $output .= $serialized;
    339         }
    340 
    341         if ( $original_processor ) {
    342                 do {
    343                         $matched = $original_processor->next_token();
    344                         if ( $matched ) {
    345                                 $original_text .= $original_processor->get_modifiable_text();
    346                         }
    347                 } while ( $matched && ( '#text' === $original_processor->get_token_type() ) );
    348                 if ( $matched || null !== $original_processor->get_last_error() || $original_processor->paused_at_incomplete_token() ) {
    349                         return $content;
    350                 }
    351         }
    352 
    353         // Never publish a partially parsed or incomplete fragment.
    354         if ( null !== $processor->get_last_error() || $processor->paused_at_incomplete_token() || ! $changed ) {
     268                        $depth = $processor->get_current_depth();
     269                }
     270        }
     271        if ( $depth || preg_match( $item_pattern, $content, $item, 0, $search ) || $processor->next_tag( $query ) || null !== $processor->get_last_error() || $processor->paused_at_incomplete_token() || $tags->paused_at_incomplete_token() ) {
    355272                return $content;
    356273        }
    357 
    358         if ( $depth ) {
    359                 $output .= '</ul>';
    360         }
    361 
    362         if ( $original_processor ) {
    363                 $output     = str_replace( $placeholder, '&', $output );
    364                 $round_trip = WP_HTML_Processor::create_fragment( $output );
    365                 $text       = '';
    366                 while ( $round_trip->next_token() ) {
    367                         $text .= $round_trip->get_modifiable_text();
    368                 }
    369                 // Discarded markup must not join reference fragments or change text.
    370                 if ( null !== $round_trip->get_last_error() || $round_trip->paused_at_incomplete_token() || $text !== $original_text ) {
    371                         return $content;
    372                 }
    373         }
    374 
    375         return $output;
     274        // Apply insertions only after the whole fragment passes validation.
     275        $output = '';
     276        $copied = 0;
     277        foreach ( $insertions as $offset => $wrapper ) {
     278                $output .= substr( $content, $copied, $offset - $copied );
     279                $output .= $wrapper;
     280                $copied = $offset;
     281        }
     282        return $output . substr( $content, $copied );
    376283}
    377284
  • branches/2.6/tests/phpunit/testcases/common/orphan-list-items.php

    r8141 r8145  
    3232                        'named reference' => array( '<li>&copy; Example</li>', '<ul><li>&copy; Example</li></ul>' ),
    3333                        'references in attributes' => array( '<li title="&quot;&copy;">One</li>', '<ul><li title="&quot;&copy;">One</li></ul>' ),
    34                         'unquoted legacy reference' => array( '<li title=&copy>One</li>', '<ul><li title="&copy">One</li></ul>' ),
     34                        'unquoted legacy reference' => array( '<li title=&copy>One</li>', '<ul><li title=&copy>One</li></ul>' ),
    3535                        'ambiguous attribute ampersand' => array( '<li title="a&amp=b">One</li>', '<ul><li title="a&amp=b">One</li></ul>' ),
    3636                        'ampersand in attribute name' => array( '<li>&copy;</li><span data-&copy;="x">Two</span>', '<ul><li>&copy;</li></ul><span data-&copy;="x">Two</span>' ),
    37                         'placeholder collision' => array( "<li>\u{E000}\u{E001} &copy;</li>", "<ul><li>\u{E000}\u{E001} &copy;</li></ul>" ),
    38                         'null removal in text' => array( "<li>bbp_amper\x00sand_ &copy;</li>", '<ul><li>bbp_ampersand_ &copy;</li></ul>' ),
     37                        'private-use characters' => array( "<li>\u{E000}\u{E001} &copy;</li>", "<ul><li>\u{E000}\u{E001} &copy;</li></ul>" ),
     38                        'null byte in text' => array( "<li>bbp_amper\x00sand_ &copy;</li>", "<ul><li>bbp_amper\x00sand_ &copy;</li></ul>" ),
    3939                        'encoded spacing between items' => array( '<li>One</li>&#32;&NewLine;<li>Two</li>', '<ul><li>One</li>&#32;&NewLine;<li>Two</li></ul>' ),
    4040                        'encoded text between items' => array( '<li>One</li>&copy;<li>Two</li>', '<ul><li>One</li></ul>&copy;<ul><li>Two</li></ul>' ),
    … …  
    4444                        'mixed listing newlines' => array( "<listing>\n&NewLine;x</listing><li>One</li>", "<listing>\n&NewLine;x</listing><ul><li>One</li></ul>" ),
    4545                        'mixed textarea newlines' => array( "<textarea>\n&#10;x</textarea><li>One</li>", "<textarea>\n&#10;x</textarea><ul><li>One</li></ul>" ),
    46                         'attributes and case' => array( "<LI class='item'>One</LI>", '<ul><li class="item">One</li></ul>' ),
     46                        'attributes and case' => array( "<LI class='item'>One</LI>", "<ul><LI class='item'>One</LI></ul>" ),
    4747                        'valid unordered' => array( '<ul><li>One</li></ul>', '<ul><li>One</li></ul>' ),
    4848                        'valid ordered' => array( '<ol start="2"><li>Two</li></ol>', '<ol start="2"><li>Two</li></ol>' ),
    … …  
    5656                        'nested list in orphan' => array( '<li>One<ol><li>Two</li></ol></li>', '<ul><li>One<ol><li>Two</li></ol></li></ul>' ),
    5757                        'container boundary' => array( '<div><li>One</li></div><p>Text</p>', '<div><ul><li>One</li></ul></div><p>Text</p>' ),
     58                        'implicit closing boundaries are unchanged' => array( '<div><li>One</div><li>Two', '<div><li>One</div><li>Two' ),
     59                        'unclosed adjacent items are unchanged' => array( '<li>One<li>Two', '<li>One<li>Two' ),
    5860                        'text boundary' => array( '<li>One</li>Text<li>Two</li>', '<ul><li>One</li></ul>Text<ul><li>Two</li></ul>' ),
    59                         'comments' => array( '<!-- <li>Example</li> --><li>One</li>', '<!-- <li>Example</li> --><ul><li>One</li></ul>' ),
     61                        'ambiguous list tags in comments are unchanged' => array( '<!-- <li>Example</li> --><li>One</li>', '<!-- <li>Example</li> --><li>One</li>' ),
    6062                        'comment between items' => array( '<li>One</li><!-- next --><li>Two</li>', '<ul><li>One</li><!-- next --><li>Two</li></ul>' ),
    61                         'cdata' => array( '<![CDATA[<li>Example</li>]]><li>One</li>', '<!--[CDATA[<li-->Example]]&gt;<ul><li>One</li></ul>' ),
     63                        'ignored list end' => array( '<!-- <ul> --><li>One</li></ul><li>Two</li>', '<!-- <ul> --><li>One</li></ul><li>Two</li>' ),
     64                        'ignored list end inside item' => array( '<li>One</ul>Two</li>', '<li>One</ul>Two</li>' ),
     65                        'ignored item end in nested list is unchanged' => array( '<li>One<ul><li>Two</li></li></ul><li>Three</li>', '<li>One<ul><li>Two</li></li></ul><li>Three</li>' ),
     66                        'nested unordered list in orphan' => array( '<li>One<ul><li>Two</li></ul></li>', '<ul><li>One<ul><li>Two</li></ul></li></ul>' ),
     67                        'list end in comment' => array( '<li>One<!-- </ul> --></li>', '<ul><li>One<!-- </ul> --></li></ul>' ),
     68                        'list end in attribute' => array( '<li title="</ul>">One</li>', '<ul><li title="</ul>">One</li></ul>' ),
     69                        'list end in script' => array( '<li>One<script>"</ul>"</script></li>', '<ul><li>One<script>"</ul>"</script></li></ul>' ),
     70                        'list end in SVG style' => array( '<li><svg><style></ul></style></svg></li><li>Two</li>', '<li><svg><style></ul></style></svg></li><li>Two</li>' ),
     71                        'list end in MathML style' => array( '<li><math><style></ul></style></math></li><li>Two</li>', '<li><math><style></ul></style></math></li><li>Two</li>' ),
     72                        'foreign content is unchanged' => array( '<li><svg><circle /></svg></li>', '<li><svg><circle /></svg></li>' ),
     73                        'ambiguous list tags in cdata are unchanged' => array( '<![CDATA[<li>Example</li>]]><li>One</li>', '<![CDATA[<li>Example</li>]]><li>One</li>' ),
     74                        'processing instruction' => array( "<?example?><LI data-x='&copy;'>One</LI>", "<?example?><ul><LI data-x='&copy;'>One</LI></ul>" ),
    6275                        'unchanged case and quotes' => array( "<UL><LI class='item'>One</LI></UL>", "<UL><LI class='item'>One</LI></UL>" ),
    6376                        'incomplete input' => array( '<li>One</li><!--', '<li>One</li><!--' ),
    6477                        'unsupported parsing' => array( '<li>One</li><a><div><a></div></a>', '<li>One</li><a><div><a></div></a>' ),
    65                         'ampersand opening syntax' => array( '<li>One</li><&x>', '<ul><li>One</li></ul>&lt;&x&gt;' ),
    66                         'ampersand closing syntax' => array( '<li>One</li></&x>', '<ul><li>One</li></ul><!--&x-->' ),
    67                         'ampersand in tag name' => array( '<li>One</li><p&x>Two</p&x>', '<li>One</li><p&x>Two</p&x>' ),
     78                        'ampersand opening syntax' => array( '<li>One</li><&x>', '<ul><li>One</li></ul><&x>' ),
     79                        'ampersand closing syntax' => array( '<li>One</li></&x>', '<ul><li>One</li></ul></&x>' ),
     80                        'ampersand in tag name' => array( '<li>One</li><p&x>Two</p&x>', '<ul><li>One</li></ul><p&x>Two</p&x>' ),
    6881                        'quoted attribute' => array( '<span title="><li>Example</li>">Text</span>', '<span title="><li>Example</li>">Text</span>' ),
    6982                        'single quoted attribute' => array( "<span title='><li>Example</li>'>Text</span>", "<span title='><li>Example</li>'>Text</span>" ),
    … …  
    7689                        'listing text before orphan' => array( '<listing>x</listing><li>One</li>', '<listing>x</listing><ul><li>One</li></ul>' ),
    7790                        'textarea newline before orphan' => array( "<textarea>\n\nx</textarea><li>One</li>", "<textarea>\n\nx</textarea><ul><li>One</li></ul>" ),
    78                         'textarea quoted angle bracket' => array( "<textarea title='>'>\n\nx</textarea><li>One</li>", "<textarea title=\"&gt;\">\n\nx</textarea><ul><li>One</li></ul>" ),
    79                         'invalid textarea text' => array( "<textarea>\n\ncaf\xE9</textarea><li>One</li>", "<textarea>\n\ncaf\xE9</textarea><li>One</li>" ),
    80                         'nested orphan limitation' => array( '<li>One<blockquote><li>Two</li></blockquote></li>', '<ul><li>One<blockquote><li>Two</li></blockquote></li></ul>' ),
     91                        'textarea quoted angle bracket' => array( "<textarea title='>'>\n\nx</textarea><li>One</li>", "<textarea title='>'>\n\nx</textarea><ul><li>One</li></ul>" ),
     92                        'invalid textarea text' => array( "<textarea>\n\ncaf\xE9</textarea><li>One</li>", "<textarea>\n\ncaf\xE9</textarea><ul><li>One</li></ul>" ),
     93                        'nested orphans are unchanged' => array( '<li>One<blockquote><li>Two</li></blockquote></li>', '<li>One<blockquote><li>Two</li></blockquote></li>' ),
     94                        'quoted greater-than attribute' => array( '<LI title=">">One</LI>', '<ul><LI title=">">One</LI></ul>' ),
     95                        'ambiguous list tags in attributes are unchanged' => array( '<li title="<li>">One</li>', '<li title="<li>">One</li>' ),
     96                        'ambiguous list tags in scripts are unchanged' => array( '<script>"<li>Example</li>"</script><li>One</li>', '<script>"<li>Example</li>"</script><li>One</li>' ),
     97                        'nonstandard closing item is unchanged' => array( '<li>One</li title=x>', '<li>One</li title=x>' ),
     98                        'ignored non-list tag is unchanged' => array( '<li>One</li></span>', '<li>One</li></span>' ),
     99                        'later malformed run discards earlier repair' => array( '<li>One</li><p>Text</p><li>Two</li></ul>', '<li>One</li><p>Text</p><li>Two</li></ul>' ),
     100                        'whitespace following the last item stays outside the list' => array( "<li>One</li>\n<p>Text</p>", "<ul><li>One</li></ul>\n<p>Text</p>" ),
    81101                        'textarea' => array( '<textarea><li>Example</li></textarea>', '<textarea><li>Example</li></textarea>' ),
    82102                        'script' => array( '<script>"<li>Example</li>"</script>', '<script>"<li>Example</li>"</script>' ),
    … …  
    104124        }
    105125
     126        public function test_ignored_list_end_surviving_tag_balancing_is_unchanged() {
     127                $content = '<!-- <ul> --><li>One</li></ul><li>Two</li>';
     128                // This fixture reaches the filter because the balancer counts commented tags.
     129                $this->assertSame( $content, force_balance_tags( $content ) );
     130                $this->assertSame( $content, bbp_wrap_orphan_list_items( force_balance_tags( $content ) ) );
     131        }
     132
    106133        public function test_rendered_block_content_is_not_repaired() {
    107134                $post_id = $this->factory->post->create( array( 'post_content' => '<!-- wp:html --><li>Example</li><!-- /wp:html -->' ) );
    … …  
    167194        }
    168195
    169         public function test_opaque_payloads_are_preserved_or_repair_is_bypassed() {
     196        public function test_opaque_payloads_are_preserved() {
    170197                $modern = class_exists( 'WP_HTML_Processor' ) && is_callable( array( WP_HTML_Processor::create_fragment( '' ), 'serialize_token' ) );
    171198                foreach ( array( 'iframe', 'noembed', 'noframes', 'script', 'style', 'textarea', 'title', 'xmp' ) as $tag ) {
    172199                        $content = '<' . $tag . '>Example &amp;lt; &lt;b&gt;</' . $tag . '><li>One</li>';
    173200                        $output  = bbp_wrap_orphan_list_items( $content );
    174                         if ( $modern && ! in_array( $tag, array( 'iframe', 'noembed', 'noframes' ), true ) ) {
    175                                 $this->assertStringContainsString( '<ul><li>One</li></ul>', $output );
    176                         }
    177                         if ( $output === $content ) {
    178                                 $this->assertSame( $content, $output );
    179                                 continue;
    180                         }
    181                         $before = new WP_HTML_Tag_Processor( $content );
    182                         $after  = new WP_HTML_Tag_Processor( $output );
    183                         $this->assertTrue( $before->next_tag() );
    184                         $this->assertTrue( $after->next_tag() );
    185                         $this->assertSame( $before->get_modifiable_text(), $after->get_modifiable_text(), $tag );
    186                         $this->assertStringContainsString( '<ul><li>One</li></ul>', $output );
     201                        $this->assertSame( $modern ? str_replace( '<li>One</li>', '<ul><li>One</li></ul>', $content ) : $content, $output, $tag );
    187202                }
    188203        }
    … …  
    265280        }
    266281
    267         public function test_reference_masking_does_not_change_the_element_tree() {
    268                 $modern = class_exists( 'WP_HTML_Processor' ) && is_callable( array( WP_HTML_Processor::create_fragment( '' ), 'serialize_token' ) );
    269                 $controls = array(
     282        public function test_ambiguous_table_and_foreign_markup_is_unchanged() {
     283                $fixtures = array(
    270284                        '<li>One</li><math><annotation-xml encoding="text/html"><p>Two</p></annotation-xml></math>',
    271285                        '<li>One</li><table> <tr><td>Two</td></tr></table>',
    272                 );
    273                 foreach ( $controls as $content ) {
    274                         if ( $modern ) {
    275                                 $this->assertStringContainsString( '<ul><li>One</li></ul>', bbp_wrap_orphan_list_items( $content ) );
    276                         } else {
    277                                 $this->assertSame( $content, bbp_wrap_orphan_list_items( $content ) );
    278                         }
    279                 }
    280                 $fixtures = array(
    281286                        '<li>One</li><math><annotation-xml encoding="text&#47;html"><p>Two</p></annotation-xml></math>',
    282                         // Masked table whitespace requires unsupported foster parenting.
    283287                        '<li>One</li><table>&#32;<tr><td>Two</td></tr></table>',
    284288                );
    … …  
    289293
    290294        public function test_discarded_markup_does_not_create_character_references() {
    291                 foreach ( array( '<li>&</div>lt;b&</div>gt;</li>', "<li>&\x00amp;</li>" ) as $content ) {
    292                         $this->assertSame( $content, bbp_wrap_orphan_list_items( $content ) );
    293                 }
    294         }
    295 
    296         public function test_placeholder_selection_is_bounded() {
    297                 $characters = '';
    298                 for ( $attempt = 0; $attempt < 16; ++$attempt ) {
    299                         $characters .= "\xEE\x80" . chr( 0x80 + $attempt );
    300                 }
    301                 $content = '<li>' . $characters . ' &copy;</li>';
    302                 $this->assertSame( $content, bbp_wrap_orphan_list_items( $content ) );
    303                 $modern = class_exists( 'WP_HTML_Processor' ) && is_callable( array( WP_HTML_Processor::create_fragment( '' ), 'serialize_token' ) );
    304                 $content = '<li>bbp_ampersand_' . str_repeat( '_', 128 ) . str_repeat( '&', 32 ) . '</li>';
     295                $modern = class_exists( 'WP_HTML_Processor' ) && is_callable( array( WP_HTML_Processor::create_fragment( '' ), 'serialize_token' ) );
     296                $malformed = '<li>&</div>lt;b&</div>gt;</li>';
     297                $this->assertSame( $malformed, bbp_wrap_orphan_list_items( $malformed ) );
     298                $content = "<li>&\x00amp;</li>";
    305299                $this->assertSame( $modern ? '<ul>' . $content . '</ul>' : $content, bbp_wrap_orphan_list_items( $content ) );
     300        }
     301
     302        public function test_lookalike_tags_do_not_load_stored_content() {
     303                global $wpdb;
     304                foreach ( array( '<link href="style.css">', '<listing>Example</listing>', 'No list items.', '&lt;li&gt;Example&lt;/li&gt;' ) as $content ) {
     305                        $queries = $wpdb->num_queries;
     306                        $this->assertSame( $content, bbp_wrap_orphan_list_items( $content, PHP_INT_MAX ) );
     307                        $this->assertSame( $queries, $wpdb->num_queries );
     308                }
    306309        }
    307310
Note: See TracChangeset for help on using the changeset viewer.