Changeset 8145
- Timestamp:
- 10/26/2026 08:06:55 PM (2 weeks ago)
- Location:
- branches/2.6
- Files:
-
- 3 edited
-
CHANGELOG.md (modified) (1 diff)
-
src/includes/common/formatting.php (modified) (2 diffs)
-
tests/phpunit/testcases/common/orphan-list-items.php (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/2.6/CHANGELOG.md
r8143 r8145 39 39 - Kept the reply editor's Forum and Reply To dropdowns synchronized with its 40 40 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. 44 43 - Kept generated bbPress content available to excerpt-based templates without 45 44 causing themes that display both excerpts and content to duplicate it. -
branches/2.6/src/includes/common/formatting.php
r8141 r8145 175 175 * 176 176 * 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. 179 181 * Code examples and block content are excluded from repair. Fragments without 180 182 * orphan list items and sites using other character encodings are unchanged. … … 189 191 */ 190 192 function 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 ) ) { 192 194 return $content; 193 195 } 194 195 196 if ( ! empty( $post_id ) && has_blocks( get_post_field( 'post_content', $post_id, 'raw' ) ) ) { 196 197 return $content; 197 198 } 198 199 199 $processor = WP_HTML_Processor::create_fragment( $content ); 200 200 if ( ! is_callable( array( $processor, 'serialize_token' ) ) || ! is_utf8_charset() ) { 201 201 return $content; 202 202 } 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() ) { 218 227 return $content; 219 228 } 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 ) ) { 247 239 return $content; 248 240 } 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; 276 245 } 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; 287 249 } 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; 326 260 } 327 261 } 328 262 } 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>'; 335 267 } 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() ) { 355 272 return $content; 356 273 } 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 ); 376 283 } 377 284 -
branches/2.6/tests/phpunit/testcases/common/orphan-list-items.php
r8141 r8145 32 32 'named reference' => array( '<li>© Example</li>', '<ul><li>© Example</li></ul>' ), 33 33 'references in attributes' => array( '<li title=""©">One</li>', '<ul><li title=""©">One</li></ul>' ), 34 'unquoted legacy reference' => array( '<li title=©>One</li>', '<ul><li title= "©">One</li></ul>' ),34 'unquoted legacy reference' => array( '<li title=©>One</li>', '<ul><li title=©>One</li></ul>' ), 35 35 'ambiguous attribute ampersand' => array( '<li title="a&=b">One</li>', '<ul><li title="a&=b">One</li></ul>' ), 36 36 'ampersand in attribute name' => array( '<li>©</li><span data-©="x">Two</span>', '<ul><li>©</li></ul><span data-©="x">Two</span>' ), 37 'p laceholder collision' => array( "<li>\u{E000}\u{E001} ©</li>", "<ul><li>\u{E000}\u{E001} ©</li></ul>" ),38 'null removal in text' => array( "<li>bbp_amper\x00sand_ ©</li>", '<ul><li>bbp_ampersand_ ©</li></ul>'),37 'private-use characters' => array( "<li>\u{E000}\u{E001} ©</li>", "<ul><li>\u{E000}\u{E001} ©</li></ul>" ), 38 'null byte in text' => array( "<li>bbp_amper\x00sand_ ©</li>", "<ul><li>bbp_amper\x00sand_ ©</li></ul>" ), 39 39 'encoded spacing between items' => array( '<li>One</li> 
<li>Two</li>', '<ul><li>One</li> 
<li>Two</li></ul>' ), 40 40 'encoded text between items' => array( '<li>One</li>©<li>Two</li>', '<ul><li>One</li></ul>©<ul><li>Two</li></ul>' ), … … 44 44 'mixed listing newlines' => array( "<listing>\n
x</listing><li>One</li>", "<listing>\n
x</listing><ul><li>One</li></ul>" ), 45 45 'mixed textarea newlines' => array( "<textarea>\n x</textarea><li>One</li>", "<textarea>\n 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>" ), 47 47 'valid unordered' => array( '<ul><li>One</li></ul>', '<ul><li>One</li></ul>' ), 48 48 'valid ordered' => array( '<ol start="2"><li>Two</li></ol>', '<ol start="2"><li>Two</li></ol>' ), … … 56 56 'nested list in orphan' => array( '<li>One<ol><li>Two</li></ol></li>', '<ul><li>One<ol><li>Two</li></ol></li></ul>' ), 57 57 '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' ), 58 60 '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>' ), 60 62 '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]]><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='©'>One</LI>", "<?example?><ul><LI data-x='©'>One</LI></ul>" ), 62 75 'unchanged case and quotes' => array( "<UL><LI class='item'>One</LI></UL>", "<UL><LI class='item'>One</LI></UL>" ), 63 76 'incomplete input' => array( '<li>One</li><!--', '<li>One</li><!--' ), 64 77 '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> <&x>' ),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>' ), 68 81 'quoted attribute' => array( '<span title="><li>Example</li>">Text</span>', '<span title="><li>Example</li>">Text</span>' ), 69 82 'single quoted attribute' => array( "<span title='><li>Example</li>'>Text</span>", "<span title='><li>Example</li>'>Text</span>" ), … … 76 89 'listing text before orphan' => array( '<listing>x</listing><li>One</li>', '<listing>x</listing><ul><li>One</li></ul>' ), 77 90 '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=\">\">\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>" ), 81 101 'textarea' => array( '<textarea><li>Example</li></textarea>', '<textarea><li>Example</li></textarea>' ), 82 102 'script' => array( '<script>"<li>Example</li>"</script>', '<script>"<li>Example</li>"</script>' ), … … 104 124 } 105 125 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 106 133 public function test_rendered_block_content_is_not_repaired() { 107 134 $post_id = $this->factory->post->create( array( 'post_content' => '<!-- wp:html --><li>Example</li><!-- /wp:html -->' ) ); … … 167 194 } 168 195 169 public function test_opaque_payloads_are_preserved _or_repair_is_bypassed() {196 public function test_opaque_payloads_are_preserved() { 170 197 $modern = class_exists( 'WP_HTML_Processor' ) && is_callable( array( WP_HTML_Processor::create_fragment( '' ), 'serialize_token' ) ); 171 198 foreach ( array( 'iframe', 'noembed', 'noframes', 'script', 'style', 'textarea', 'title', 'xmp' ) as $tag ) { 172 199 $content = '<' . $tag . '>Example &lt; <b></' . $tag . '><li>One</li>'; 173 200 $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 ); 187 202 } 188 203 } … … 265 280 } 266 281 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( 270 284 '<li>One</li><math><annotation-xml encoding="text/html"><p>Two</p></annotation-xml></math>', 271 285 '<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(281 286 '<li>One</li><math><annotation-xml encoding="text/html"><p>Two</p></annotation-xml></math>', 282 // Masked table whitespace requires unsupported foster parenting.283 287 '<li>One</li><table> <tr><td>Two</td></tr></table>', 284 288 ); … … 289 293 290 294 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 . ' ©</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>"; 305 299 $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.', '<li>Example</li>' ) 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 } 306 309 } 307 310
Note:
See TracChangeset
for help on using the changeset viewer.