Changeset 8141
- Timestamp:
- 10/25/2026 05:12:18 AM (3 weeks ago)
- Location:
- branches/2.6
- Files:
-
- 3 edited
-
CHANGELOG.md (modified) (1 diff)
-
src/includes/common/formatting.php (modified) (8 diffs)
-
tests/phpunit/testcases/common/orphan-list-items.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/2.6/CHANGELOG.md
r8137 r8141 39 39 Topic, and made Forum read-only. 40 40 - Wrapped standalone list items in topic and reply content on WordPress 6.9+ 41 to prevent missing list containers from affecting the surrounding layout. 41 to prevent missing list containers from affecting the surrounding layout, 42 including content with character references, while preserving encoded text. 42 43 - Kept generated bbPress content available to excerpt-based templates without 43 44 causing themes that display both excerpts and content to duplicate it. -
branches/2.6/src/includes/common/formatting.php
r8135 r8141 179 179 * Code examples and block content are excluded from repair. Fragments without 180 180 * orphan list items and sites using other character encodings are unchanged. 181 * References that may affect later formatting are excluded from normalization.181 * Character references retain their spelling for later formatting filters. 182 182 * Nested malformed list containers are not reconstructed. 183 183 * … … 202 202 } 203 203 204 // Do not expose encoded ASCII or named punctuation to later formatting filters. 205 $pattern = '/&(?: 206 \#0*(?:[0-9]|[1-9][0-9]|1[01][0-9]|12[0-7])(?![0-9]) # Decimal ASCII: 0 through 127. 207 | \#[xX]0*[0-7]?[0-9a-fA-F](?![0-9a-fA-F]) # Hexadecimal ASCII: 00 through 7F. 208 | (?!(?:amp|lt|gt|quot|apos|nbsp);)[a-zA-Z][a-zA-Z0-9]*; # Other named references. 209 )/x'; 210 if ( preg_match( $pattern, $content ) ) { 211 return $content; 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 ) { 218 return $content; 219 } 220 $original_processor = $processor; 221 $processor = WP_HTML_Processor::create_fragment( str_replace( '&', $placeholder, $content ) ); 212 222 } 213 223 … … 217 227 $changed = false; 218 228 $pending_newline = false; 229 $original_text = ''; 219 230 220 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 ) { 247 return $content; 248 } 249 } 250 221 251 $tag = $processor->get_tag(); 222 252 $type = $processor->get_token_type(); … … 227 257 $breadcrumbs 228 258 ); 229 $is_spacing = ( '#comment' === $type ) || ( ( '#text' === $type ) && ( '' === trim( $processor->get_modifiable_text(), " \t\n\r\f" ) ) ); 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" ) ) ); 230 264 231 265 // A run ends at text or another element, not at whitespace or a comment. … … 248 282 $serialized = $processor->serialize_token(); 249 283 if ( $pending_newline ) { 250 if ( ( '#text' === $type ) && ( "\n" === substr( $processor->get_modifiable_text(), 0, 1 ) ) ) { 284 $text = $processor->get_modifiable_text(); 285 if ( ( '#text' === $type ) && ( ( "\n" === substr( $text, 0, 1 ) ) || ( $original_processor && '' === $text ) ) ) { 251 286 $output .= "\n"; 252 287 } … … 277 312 $serialized = substr_replace( $serialized, '', $text_start, 1 ); 278 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 ); 326 } 327 } 279 328 } 280 329 // Older serializers cannot preserve every opaque element's payload. … … 290 339 } 291 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 292 353 // Never publish a partially parsed or incomplete fragment. 293 354 if ( null !== $processor->get_last_error() || $processor->paused_at_incomplete_token() || ! $changed ) { … … 297 358 if ( $depth ) { 298 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 } 299 373 } 300 374 -
branches/2.6/tests/phpunit/testcases/common/orphan-list-items.php
r8135 r8141 25 25 'one item' => array( '<li>One</li>', '<ul><li>One</li></ul>' ), 26 26 'adjacent items' => array( "<li>One</li>\n<li>Two</li>", "<ul><li>One</li>\n<li>Two</li></ul>" ), 27 'non-ASCII decimal reference' => array( '<li>é</li>', "<ul><li>\u{00E9}</li></ul>" ), 28 'decimal reference boundary' => array( '<li>Ϩ</li>', "<ul><li>\u{03E8}</li></ul>" ), 29 'non-linefeed hexadecimal reference' => array( '<li>ª</li>', "<ul><li>\u{00AA}</li></ul>" ), 27 'non-ASCII decimal reference' => array( '<li>é</li>', '<ul><li>é</li></ul>' ), 28 'decimal reference boundary' => array( '<li>Ϩ</li>', '<ul><li>Ϩ</li></ul>' ), 29 'non-linefeed hexadecimal reference' => array( '<li>ª</li>', '<ul><li>ª</li></ul>' ), 30 'bare ampersand' => array( '<li>Fish & chips</li>', '<ul><li>Fish & chips</li></ul>' ), 31 'escaped ampersand' => array( '<li>Fish & chips</li>', '<ul><li>Fish & chips</li></ul>' ), 32 'named reference' => array( '<li>© Example</li>', '<ul><li>© Example</li></ul>' ), 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>' ), 35 'ambiguous attribute ampersand' => array( '<li title="a&=b">One</li>', '<ul><li title="a&=b">One</li></ul>' ), 36 'ampersand in attribute name' => array( '<li>©</li><span data-©="x">Two</span>', '<ul><li>©</li></ul><span data-©="x">Two</span>' ), 37 'placeholder 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>' ), 39 'encoded spacing between items' => array( '<li>One</li> 
<li>Two</li>', '<ul><li>One</li> 
<li>Two</li></ul>' ), 40 'encoded text between items' => array( '<li>One</li>©<li>Two</li>', '<ul><li>One</li></ul>©<ul><li>Two</li></ul>' ), 41 'encoded leading pre newline' => array( '<pre> x</pre><li>One</li>', '<pre> x</pre><ul><li>One</li></ul>' ), 42 'encoded leading textarea newline' => array( '<textarea> x</textarea><li>One</li>', '<textarea> x</textarea><ul><li>One</li></ul>' ), 43 'mixed pre newlines' => array( "<pre>\n x</pre><li>One</li>", "<pre>\n x</pre><ul><li>One</li></ul>" ), 44 'mixed listing newlines' => array( "<listing>\n
x</listing><li>One</li>", "<listing>\n
x</listing><ul><li>One</li></ul>" ), 45 'mixed textarea newlines' => array( "<textarea>\n x</textarea><li>One</li>", "<textarea>\n x</textarea><ul><li>One</li></ul>" ), 30 46 'attributes and case' => array( "<LI class='item'>One</LI>", '<ul><li class="item">One</li></ul>' ), 31 47 'valid unordered' => array( '<ul><li>One</li></ul>', '<ul><li>One</li></ul>' ), … … 47 63 'incomplete input' => array( '<li>One</li><!--', '<li>One</li><!--' ), 48 64 '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>' ), 49 68 'quoted attribute' => array( '<span title="><li>Example</li>">Text</span>', '<span title="><li>Example</li>">Text</span>' ), 50 69 'single quoted attribute' => array( "<span title='><li>Example</li>'>Text</span>", "<span title='><li>Example</li>'>Text</span>" ), … … 120 139 121 140 foreach ( array( 'pre', 'listing', 'textarea' ) as $tag ) { 122 foreach ( array( '', 'x', "\nx", "\n\nx", "\n\n\nx", "\r\n\r\nx", "\n\n&lt;&#10;</textarea>" ) as $text ) {141 foreach ( array( '', 'x', "\nx", "\n\nx", "\n\n\nx", "\r\n\r\nx", "\n\n&lt;&#10;</textarea>", "\n x", "\r\n
x", "\r
x", "\n
x" ) as $text ) { 123 142 $content = '<' . $tag . '>' . $text . '</' . $tag . '><li>One</li>'; 124 143 $repaired = bbp_wrap_orphan_list_items( $content ); … … 149 168 150 169 public function test_opaque_payloads_are_preserved_or_repair_is_bypassed() { 170 $modern = class_exists( 'WP_HTML_Processor' ) && is_callable( array( WP_HTML_Processor::create_fragment( '' ), 'serialize_token' ) ); 151 171 foreach ( array( 'iframe', 'noembed', 'noframes', 'script', 'style', 'textarea', 'title', 'xmp' ) as $tag ) { 152 172 $content = '<' . $tag . '>Example &lt; <b></' . $tag . '><li>One</li>'; 153 173 $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 } 154 177 if ( $output === $content ) { 155 178 $this->assertSame( $content, $output ); … … 166 189 167 190 public function test_ascii_references_do_not_change_later_content_formatting() { 191 $modern = class_exists( 'WP_HTML_Processor' ) && is_callable( array( WP_HTML_Processor::create_fragment( '' ), 'serialize_token' ) ); 168 192 foreach ( array( ' ', '
', '
', '
', '
', '
', '
', '
', '
', ' ', '	', ':', '@' ) as $reference ) { 169 193 $fixtures = array( 170 '<li>One' . $reference . 'Two</li>',171 '<p>a' . $reference . $reference . 'b</p><li>x</li>',172 '<listing>' . $reference . 'x</listing><li>One</li>',173 '<textarea>a' . $reference . 'z</textarea><li>One</li>',194 array( '<li>One' . $reference . 'Two</li>', '<ul><li>One' . $reference . 'Two</li></ul>' ), 195 array( '<p>a' . $reference . $reference . 'b</p><li>x</li>', '<p>a' . $reference . $reference . 'b</p><ul><li>x</li></ul>' ), 196 array( '<listing>' . $reference . 'x</listing><li>One</li>', '<listing>' . $reference . 'x</listing><ul><li>One</li></ul>' ), 197 array( '<textarea>a' . $reference . 'z</textarea><li>One</li>', '<textarea>a' . $reference . 'z</textarea><ul><li>One</li></ul>' ), 174 198 ); 175 foreach ( $fixtures as $content ) { 176 $this->assertSame( $content, bbp_wrap_orphan_list_items( $content ) ); 199 foreach ( $fixtures as $fixture ) { 200 list( $content, $wrapped ) = $fixture; 201 $this->assertSame( $modern ? $wrapped : $content, bbp_wrap_orphan_list_items( $content ) ); 177 202 // convert_chars() escapes incomplete references before this filter. 178 203 if ( ';' !== substr( $reference, -1 ) ) { … … 182 207 remove_filter( $filter, 'bbp_wrap_orphan_list_items', 35 ); 183 208 try { 184 $before = apply_filters( $filter, $ content );209 $before = apply_filters( $filter, $modern ? $wrapped : $content ); 185 210 } finally { 186 211 add_filter( $filter, 'bbp_wrap_orphan_list_items', 35, 2 ); … … 193 218 194 219 public function test_encoded_punctuation_does_not_create_links() { 220 $modern = class_exists( 'WP_HTML_Processor' ) && is_callable( array( WP_HTML_Processor::create_fragment( '' ), 'serialize_token' ) ); 195 221 foreach ( array( 'http://example.com', 'name@example.com', 'www.example.com', 'http://example.com', 'name@example.com', 'www.example.com' ) as $text ) { 196 222 $content = '<li>' . $text . '</li>'; 197 $this->assertSame( $ content, bbp_wrap_orphan_list_items( $content ) );223 $this->assertSame( $modern ? '<ul>' . $content . '</ul>' : $content, bbp_wrap_orphan_list_items( $content ) ); 198 224 foreach ( array( 'bbp_get_topic_content', 'bbp_get_reply_content' ) as $filter ) { 199 225 $this->assertStringNotContainsString( '<a ', apply_filters( $filter, $content ) ); 200 226 } 201 227 } 228 } 229 230 public function test_getters_repair_reference_content_without_changing_storage() { 231 $modern = class_exists( 'WP_HTML_Processor' ) && is_callable( array( WP_HTML_Processor::create_fragment( '' ), 'serialize_token' ) ); 232 $forum_id = $this->factory->forum->create(); 233 $topic_id = $this->factory->topic->create( array( 'post_parent' => $forum_id ) ); 234 $reply_id = $this->factory->reply->create( array( 'post_parent' => $topic_id ) ); 235 foreach ( array( 'topic' => $topic_id, 'reply' => $reply_id ) as $type => $post_id ) { 236 $getter = 'bbp_get_' . $type . '_content'; 237 foreach ( array( 'Fish & chips', 'Fish & chips', '© Example', 'http://example.com', 'name@example.com', 'One Two' ) as $text ) { 238 $content = '<li>' . $text . '</li>'; 239 wp_update_post( array( 'ID' => $post_id, 'post_content' => $content ) ); 240 $stored = get_post_field( 'post_content', $post_id, 'raw' ); 241 remove_filter( $getter, 'bbp_wrap_orphan_list_items', 35 ); 242 try { 243 $before = $getter( $post_id ); 244 } finally { 245 add_filter( $getter, 'bbp_wrap_orphan_list_items', 35, 2 ); 246 } 247 $output = $getter( $post_id ); 248 if ( $modern ) { 249 $this->assertStringContainsString( '<ul>', $output ); 250 $this->assertStringContainsString( '</ul>', $output ); 251 } else { 252 $this->assertStringNotContainsString( '<ul>', $output ); 253 } 254 // WordPress may decode punctuation on save; retain its existing links. 255 preg_match_all( '#<a\b[^>]*>.*?</a>#s', $before, $links_before ); 256 preg_match_all( '#<a\b[^>]*>.*?</a>#s', $output, $links_after ); 257 $this->assertSame( $links_before, $links_after ); 258 preg_match_all( '/&(?:\#(?:[0-9]+|x[0-9a-f]+)|[a-z][a-z0-9]*);/i', $before, $references ); 259 foreach ( $references[0] as $reference ) { 260 $this->assertStringContainsString( $reference, $output ); 261 } 262 $this->assertSame( $stored, get_post_field( 'post_content', $post_id, 'raw' ) ); 263 } 264 } 265 } 266 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( 270 '<li>One</li><math><annotation-xml encoding="text/html"><p>Two</p></annotation-xml></math>', 271 '<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 '<li>One</li><math><annotation-xml encoding="text/html"><p>Two</p></annotation-xml></math>', 282 // Masked table whitespace requires unsupported foster parenting. 283 '<li>One</li><table> <tr><td>Two</td></tr></table>', 284 ); 285 foreach ( $fixtures as $content ) { 286 $this->assertSame( $content, bbp_wrap_orphan_list_items( $content ) ); 287 } 288 } 289 290 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>'; 305 $this->assertSame( $modern ? '<ul>' . $content . '</ul>' : $content, bbp_wrap_orphan_list_items( $content ) ); 202 306 } 203 307
Note:
See TracChangeset
for help on using the changeset viewer.