Skip to:
Content

bbPress.org

Changeset 8141


Ignore:
Timestamp:
10/25/2026 05:12:18 AM (3 weeks ago)
Author:
johnjamesjacoby
Message:

Formatting: Repair lists containing character references.

Preserve ampersands and character-reference spelling while supplying missing list containers. Avoid decoding punctuation or whitespace before later link and paragraph filters, so ordinary legacy content no longer skips repair.

Validate the parsed structure and decoded text against the original fragment, preserve preformatted newlines, and bound the temporary marker selection. Retain existing fallbacks when safe serialization is unavailable and add regression coverage through both topic and reply getters and their formatting filters.

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

    r8137 r8141  
    3939  Topic, and made Forum read-only.
    4040- 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.
    4243- Kept generated bbPress content available to excerpt-based templates without
    4344  causing themes that display both excerpts and content to duplicate it.
  • branches/2.6/src/includes/common/formatting.php

    r8135 r8141  
    179179 * Code examples and block content are excluded from repair. Fragments without
    180180 * 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.
    182182 * Nested malformed list containers are not reconstructed.
    183183 *
    … …  
    202202        }
    203203
    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 ) );
    212222        }
    213223
    … …  
    217227        $changed         = false;
    218228        $pending_newline = false;
     229        $original_text   = '';
    219230
    220231        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
    221251                $tag         = $processor->get_tag();
    222252                $type        = $processor->get_token_type();
    … …  
    227257                        $breadcrumbs
    228258                );
    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" ) ) );
    230264
    231265                // A run ends at text or another element, not at whitespace or a comment.
    … …  
    248282                $serialized = $processor->serialize_token();
    249283                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 ) ) ) {
    251286                                $output .= "\n";
    252287                        }
    … …  
    277312                                        $serialized = substr_replace( $serialized, '', $text_start, 1 );
    278313                                }
     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                                }
    279328                        }
    280329                        // Older serializers cannot preserve every opaque element's payload.
    … …  
    290339        }
    291340
     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
    292353        // Never publish a partially parsed or incomplete fragment.
    293354        if ( null !== $processor->get_last_error() || $processor->paused_at_incomplete_token() || ! $changed ) {
    … …  
    297358        if ( $depth ) {
    298359                $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                }
    299373        }
    300374
  • branches/2.6/tests/phpunit/testcases/common/orphan-list-items.php

    r8135 r8141  
    2525                        'one item' => array( '<li>One</li>', '<ul><li>One</li></ul>' ),
    2626                        '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>&#233;</li>', "<ul><li>\u{00E9}</li></ul>" ),
    28                         'decimal reference boundary' => array( '<li>&#1000;</li>', "<ul><li>\u{03E8}</li></ul>" ),
    29                         'non-linefeed hexadecimal reference' => array( '<li>&#xAA;</li>', "<ul><li>\u{00AA}</li></ul>" ),
     27                        'non-ASCII decimal reference' => array( '<li>&#233;</li>', '<ul><li>&#233;</li></ul>' ),
     28                        'decimal reference boundary' => array( '<li>&#1000;</li>', '<ul><li>&#1000;</li></ul>' ),
     29                        'non-linefeed hexadecimal reference' => array( '<li>&#xAA;</li>', '<ul><li>&#xAA;</li></ul>' ),
     30                        'bare ampersand' => array( '<li>Fish & chips</li>', '<ul><li>Fish & chips</li></ul>' ),
     31                        'escaped ampersand' => array( '<li>Fish &#038; chips</li>', '<ul><li>Fish &#038; chips</li></ul>' ),
     32                        'named reference' => array( '<li>&copy; Example</li>', '<ul><li>&copy; Example</li></ul>' ),
     33                        '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>' ),
     35                        'ambiguous attribute ampersand' => array( '<li title="a&amp=b">One</li>', '<ul><li title="a&amp=b">One</li></ul>' ),
     36                        '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>' ),
     39                        'encoded spacing between items' => array( '<li>One</li>&#32;&NewLine;<li>Two</li>', '<ul><li>One</li>&#32;&NewLine;<li>Two</li></ul>' ),
     40                        'encoded text between items' => array( '<li>One</li>&copy;<li>Two</li>', '<ul><li>One</li></ul>&copy;<ul><li>Two</li></ul>' ),
     41                        'encoded leading pre newline' => array( '<pre>&#10;x</pre><li>One</li>', '<pre>&#10;x</pre><ul><li>One</li></ul>' ),
     42                        'encoded leading textarea newline' => array( '<textarea>&#10;x</textarea><li>One</li>', '<textarea>&#10;x</textarea><ul><li>One</li></ul>' ),
     43                        'mixed pre newlines' => array( "<pre>\n&#10;x</pre><li>One</li>", "<pre>\n&#10;x</pre><ul><li>One</li></ul>" ),
     44                        'mixed listing newlines' => array( "<listing>\n&NewLine;x</listing><li>One</li>", "<listing>\n&NewLine;x</listing><ul><li>One</li></ul>" ),
     45                        'mixed textarea newlines' => array( "<textarea>\n&#10;x</textarea><li>One</li>", "<textarea>\n&#10;x</textarea><ul><li>One</li></ul>" ),
    3046                        'attributes and case' => array( "<LI class='item'>One</LI>", '<ul><li class="item">One</li></ul>' ),
    3147                        'valid unordered' => array( '<ul><li>One</li></ul>', '<ul><li>One</li></ul>' ),
    … …  
    4763                        'incomplete input' => array( '<li>One</li><!--', '<li>One</li><!--' ),
    4864                        '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>' ),
    4968                        'quoted attribute' => array( '<span title="><li>Example</li>">Text</span>', '<span title="><li>Example</li>">Text</span>' ),
    5069                        'single quoted attribute' => array( "<span title='><li>Example</li>'>Text</span>", "<span title='><li>Example</li>'>Text</span>" ),
    … …  
    120139
    121140                foreach ( array( 'pre', 'listing', 'textarea' ) as $tag ) {
    122                         foreach ( array( '', 'x', "\nx", "\n\nx", "\n\n\nx", "\r\n\r\nx", "\n\n&amp;lt;&amp;#10;&lt;/textarea&gt;" ) as $text ) {
     141                        foreach ( array( '', 'x', "\nx", "\n\nx", "\n\n\nx", "\r\n\r\nx", "\n\n&amp;lt;&amp;#10;&lt;/textarea&gt;", "\n&#10;x", "\r\n&#x0A;x", "\r&NewLine;x", "\n&#10x" ) as $text ) {
    123142                                $content  = '<' . $tag . '>' . $text . '</' . $tag . '><li>One</li>';
    124143                                $repaired = bbp_wrap_orphan_list_items( $content );
    … …  
    149168
    150169        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' ) );
    151171                foreach ( array( 'iframe', 'noembed', 'noframes', 'script', 'style', 'textarea', 'title', 'xmp' ) as $tag ) {
    152172                        $content = '<' . $tag . '>Example &amp;lt; &lt;b&gt;</' . $tag . '><li>One</li>';
    153173                        $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                        }
    154177                        if ( $output === $content ) {
    155178                                $this->assertSame( $content, $output );
    … …  
    166189
    167190        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' ) );
    168192                foreach ( array( '&#10;', '&#010;', '&#00010;', '&#10', '&#xA;', '&#Xa;', '&#x000a;', '&#xA', '&NewLine;', '&#13;', '&#9;', '&#58;', '&#64;' ) as $reference ) {
    169193                        $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>' ),
    174198                        );
    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 ) );
    177202                                // convert_chars() escapes incomplete references before this filter.
    178203                                if ( ';' !== substr( $reference, -1 ) ) {
    … …  
    182207                                        remove_filter( $filter, 'bbp_wrap_orphan_list_items', 35 );
    183208                                        try {
    184                                                 $before = apply_filters( $filter, $content );
     209                                                $before = apply_filters( $filter, $modern ? $wrapped : $content );
    185210                                        } finally {
    186211                                                add_filter( $filter, 'bbp_wrap_orphan_list_items', 35, 2 );
    … …  
    193218
    194219        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' ) );
    195221                foreach ( array( 'http&#58;//example.com', 'name&#64;example.com', 'www&#46;example&#46;com', 'http&colon;//example.com', 'name&commat;example.com', 'www&period;example&period;com' ) as $text ) {
    196222                        $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 ) );
    198224                        foreach ( array( 'bbp_get_topic_content', 'bbp_get_reply_content' ) as $filter ) {
    199225                                $this->assertStringNotContainsString( '<a ', apply_filters( $filter, $content ) );
    200226                        }
    201227                }
     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 &#038; chips', '&copy; Example', 'http&#58;//example.com', 'name&commat;example.com', 'One&#10;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&#47;html"><p>Two</p></annotation-xml></math>',
     282                        // Masked table whitespace requires unsupported foster parenting.
     283                        '<li>One</li><table>&#32;<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 . ' &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>';
     305                $this->assertSame( $modern ? '<ul>' . $content . '</ul>' : $content, bbp_wrap_orphan_list_items( $content ) );
    202306        }
    203307
Note: See TracChangeset for help on using the changeset viewer.