Skip to:
Content

bbPress.org

Changeset 8135


Ignore:
Timestamp:
10/24/2026 08:31:30 PM (3 weeks ago)
Author:
johnjamesjacoby
Message:

Formatting: Wrap orphaned list items in content.

Use the WordPress HTML Processor to supply missing unordered list containers in displayed topic and reply content on WordPress 6.9 and newer. Preserve stored content, existing list containers, and block content, and leave older installations unchanged.

Preserve preformatted text across supported parser versions. Skip non-UTF-8 sites, references that could change later formatting, and fragments that cannot be safely serialized. Add regression coverage for the helper and the topic and reply content filters.

In branches/2.6, for 2.6.21.

Props Pebbleontheshore.
Fixes #2357.

Location:
branches/2.6
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2.6/CHANGELOG.md

    r8133 r8135  
    3636### Fixed
    3737
     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.
    3840- Kept generated bbPress content available to excerpt-based templates without
    3941  causing themes that display both excerpts and content to duplicate it.
  • branches/2.6/src/includes/common/formatting.php

    r8112 r8135  
    170170
    171171/** 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 */
     190function 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}
    172303
    173304/**
  • branches/2.6/src/includes/core/filters.php

    r8133 r8135  
    205205add_filter( 'bbp_get_reply_content', 'convert_smilies',    20 );
    206206add_filter( 'bbp_get_reply_content', 'force_balance_tags', 30 );
     207add_filter( 'bbp_get_reply_content', 'bbp_wrap_orphan_list_items', 35, 2 );
    207208add_filter( 'bbp_get_reply_content', 'bbp_make_clickable', 40 );
    208209add_filter( 'bbp_get_reply_content', 'wpautop',            50 );
    … …  
    216217add_filter( 'bbp_get_topic_content', 'convert_smilies',    20 );
    217218add_filter( 'bbp_get_topic_content', 'force_balance_tags', 30 );
     219add_filter( 'bbp_get_topic_content', 'bbp_wrap_orphan_list_items', 35, 2 );
    218220add_filter( 'bbp_get_topic_content', 'bbp_make_clickable', 40 );
    219221add_filter( 'bbp_get_topic_content', 'wpautop',            50 );
Note: See TracChangeset for help on using the changeset viewer.