Changeset 8135
- Timestamp:
- 10/24/2026 08:31:30 PM (3 weeks ago)
- Location:
- branches/2.6
- Files:
-
- 1 added
- 3 edited
-
CHANGELOG.md (modified) (1 diff)
-
src/includes/common/formatting.php (modified) (1 diff)
-
src/includes/core/filters.php (modified) (2 diffs)
-
tests/phpunit/testcases/common/orphan-list-items.php (added)
Legend:
- Unmodified
- Added
- Removed
-
branches/2.6/CHANGELOG.md
r8133 r8135 36 36 ### Fixed 37 37 38 - Wrapped standalone list items in topic and reply content on WordPress 6.9+ 39 to prevent missing list containers from affecting the surrounding layout. 38 40 - Kept generated bbPress content available to excerpt-based templates without 39 41 causing themes that display both excerpts and content to duplicate it. -
branches/2.6/src/includes/common/formatting.php
r8112 r8135 170 170 171 171 /** Formatting ****************************************************************/ 172 173 /** 174 * Wrap runs of list items that have no list container in displayed content. 175 * 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. 179 * Code examples and block content are excluded from repair. Fragments without 180 * orphan list items and sites using other character encodings are unchanged. 181 * References that may affect later formatting are excluded from normalization. 182 * Nested malformed list containers are not reconstructed. 183 * 184 * @since 2.6.21 bbPress (r8135) 185 * 186 * @param string $content Balanced topic or reply content. 187 * @param int $post_id Optional. Topic or reply ID. 188 * @return string Content with missing unordered list containers supplied. 189 */ 190 function bbp_wrap_orphan_list_items( $content = '', $post_id = 0 ) { 191 if ( ! class_exists( 'WP_HTML_Processor' ) || ( false === stripos( $content, '<li' ) ) || has_blocks( $content ) ) { 192 return $content; 193 } 194 195 if ( ! empty( $post_id ) && has_blocks( get_post_field( 'post_content', $post_id, 'raw' ) ) ) { 196 return $content; 197 } 198 199 $processor = WP_HTML_Processor::create_fragment( $content ); 200 if ( ! is_callable( array( $processor, 'serialize_token' ) ) || ! is_utf8_charset() ) { 201 return $content; 202 } 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; 212 } 213 214 $output = ''; 215 $depth = 0; 216 $in_item = false; 217 $changed = false; 218 $pending_newline = false; 219 220 while ( $processor->next_token() ) { 221 $tag = $processor->get_tag(); 222 $type = $processor->get_token_type(); 223 $closing = $processor->is_tag_closer(); 224 $breadcrumbs = $processor->get_breadcrumbs(); 225 $is_item = ( 'LI' === $tag ) && ! $closing && ! array_intersect( 226 array( 'UL', 'OL', 'MENU', 'DIR', 'PRE', 'CODE', 'LISTING' ), 227 $breadcrumbs 228 ); 229 $is_spacing = ( '#comment' === $type ) || ( ( '#text' === $type ) && ( '' === trim( $processor->get_modifiable_text(), " \t\n\r\f" ) ) ); 230 231 // A run ends at text or another element, not at whitespace or a comment. 232 if ( $depth && ! $in_item && ! $is_item && ! $is_spacing ) { 233 $output .= '</ul>'; 234 $depth = 0; 235 } 236 237 if ( $is_item ) { 238 if ( ! $depth ) { 239 $output .= '<ul>'; 240 $depth = count( $breadcrumbs ) - 1; 241 $changed = true; 242 } 243 $in_item = true; 244 } elseif ( $closing && ( 'LI' === $tag ) && ( count( $breadcrumbs ) === $depth ) ) { 245 $in_item = false; 246 } 247 248 $serialized = $processor->serialize_token(); 249 if ( $pending_newline ) { 250 if ( ( '#text' === $type ) && ( "\n" === substr( $processor->get_modifiable_text(), 0, 1 ) ) ) { 251 $output .= "\n"; 252 } 253 $pending_newline = false; 254 } 255 256 // Preserve the newline consumed by HTML parsing, including on WordPress 6.9. 257 if ( ! $closing && ( 'html' === $processor->get_namespace() ) ) { 258 if ( in_array( $tag, array( 'PRE', 'LISTING' ), true ) ) { 259 if ( "\n" === substr( $serialized, -1 ) ) { 260 $serialized = substr( $serialized, 0, -1 ); 261 } 262 $pending_newline = true; 263 } elseif ( 'TEXTAREA' === $tag ) { 264 $expected = $processor->get_modifiable_text(); 265 $textarea = new WP_HTML_Tag_Processor( $serialized ); 266 if ( ! $textarea->next_tag() ) { 267 return $content; 268 } 269 // Serialized attribute values have escaped angle brackets. 270 $text_start = strpos( $serialized, '>' ) + 1; 271 if ( $textarea->get_modifiable_text() !== $expected ) { 272 if ( "\n" . $textarea->get_modifiable_text() !== $expected ) { 273 return $content; 274 } 275 $serialized = substr_replace( $serialized, "\n", $text_start, 0 ); 276 } elseif ( ( "\n" !== substr( $expected, 0, 1 ) ) && ( "\n" === substr( $serialized, $text_start, 1 ) ) ) { 277 $serialized = substr_replace( $serialized, '', $text_start, 1 ); 278 } 279 } 280 // Older serializers cannot preserve every opaque element's payload. 281 if ( in_array( $tag, array( 'IFRAME', 'NOEMBED', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEXTAREA', 'TITLE', 'XMP' ), true ) ) { 282 $token = new WP_HTML_Tag_Processor( $serialized ); 283 if ( ! $token->next_tag() || ( $token->get_modifiable_text() !== $processor->get_modifiable_text() ) ) { 284 return $content; 285 } 286 } 287 } 288 289 $output .= $serialized; 290 } 291 292 // Never publish a partially parsed or incomplete fragment. 293 if ( null !== $processor->get_last_error() || $processor->paused_at_incomplete_token() || ! $changed ) { 294 return $content; 295 } 296 297 if ( $depth ) { 298 $output .= '</ul>'; 299 } 300 301 return $output; 302 } 172 303 173 304 /** -
branches/2.6/src/includes/core/filters.php
r8133 r8135 205 205 add_filter( 'bbp_get_reply_content', 'convert_smilies', 20 ); 206 206 add_filter( 'bbp_get_reply_content', 'force_balance_tags', 30 ); 207 add_filter( 'bbp_get_reply_content', 'bbp_wrap_orphan_list_items', 35, 2 ); 207 208 add_filter( 'bbp_get_reply_content', 'bbp_make_clickable', 40 ); 208 209 add_filter( 'bbp_get_reply_content', 'wpautop', 50 ); … … 216 217 add_filter( 'bbp_get_topic_content', 'convert_smilies', 20 ); 217 218 add_filter( 'bbp_get_topic_content', 'force_balance_tags', 30 ); 219 add_filter( 'bbp_get_topic_content', 'bbp_wrap_orphan_list_items', 35, 2 ); 218 220 add_filter( 'bbp_get_topic_content', 'bbp_make_clickable', 40 ); 219 221 add_filter( 'bbp_get_topic_content', 'wpautop', 50 );
Note:
See TracChangeset
for help on using the changeset viewer.