Make WordPress Core

Changeset 65790


Ignore:
Timestamp:
11/25/2026 07:33:37 PM (12 hours ago)
Author:
lancewillett
Message:

Revisions: Bound comparisons when rendering large diffs.

Skip word highlighting for very long lines. Display removed lines followed by added lines when matching a changed block would exceed the line or pair limit. Sites can adjust the limits through wp_text_diff().

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/16136

Props iverok, justjack1, arkaprabhachowdhury, adamsilverstein, dmsnell, josephscott, mukesh27.
Fixes #66095.
See #48973.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-text-diff-renderer-table.php

    r62649 r65790  
    6767         */
    6868        protected $_diff_threshold = 0.6;
     69
     70        /**
     71         * Maximum line length in bytes for word-level highlighting.
     72         *
     73         * @since 7.3.0
     74         * @var int
     75         */
     76        protected $_max_word_diff_line_length = 10000;
     77
     78        /**
     79         * Maximum number of line pairs to compare when matching changed lines.
     80         *
     81         * @since 7.3.0
     82         * @var int
     83         */
     84        protected $_max_changed_line_pairs = 250000;
     85
     86        /**
     87         * Maximum total number of old and new lines to match in a changed block.
     88         *
     89         * @since 7.3.0
     90         * @var int
     91         */
     92        protected $_max_changed_lines = 5000;
    6993
    7094        /**
    … …  
    286310         */
    287311        public function _changed( $orig, $final ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.finalFound
     312                $old_count = count( $orig );
     313                $new_count = count( $final );
     314                // interleave_changed_lines() scores every old line against every new line.
     315                if ( $old_count + $new_count > $this->_max_changed_lines
     316                        || $old_count * $new_count > $this->_max_changed_line_pairs
     317                ) {
     318                        return $this->_deleted( $orig ) . $this->_added( $final );
     319                }
     320
    288321                $r = '';
    289322
    … …  
    306339                foreach ( $orig_matches as $o => $f ) {
    307340                        if ( is_numeric( $o ) && is_numeric( $f ) ) {
     341                                if ( strlen( $orig[ $o ] ) > $this->_max_word_diff_line_length
     342                                        || strlen( $final[ $f ] ) > $this->_max_word_diff_line_length
     343                                ) {
     344                                        continue;
     345                                }
     346
    308347                                $text_diff = new Text_Diff( 'auto', array( array( $orig[ $o ] ), array( $final[ $f ] ) ) );
    309348                                $renderer  = new $this->inline_diff_renderer();
  • trunk/src/wp-includes/pluggable.php

    r65788 r65790  
    34123412         *
    34133413         * @since 2.6.0
     3414         * @since 7.3.0 Added the `$max_word_diff_line_length`, `$max_changed_lines`,
     3415         *              and `$max_changed_line_pairs` arguments.
    34143416         *
    34153417         * @see wp_parse_args() Used to change defaults to user defined settings.
    … …  
    34223424         *     Associative array of options to pass to WP_Text_Diff_Renderer_Table().
    34233425         *
    3424          *     @type string $title           Titles the diff in a manner compatible
    3425          *                                   with the output. Default empty.
    3426          *     @type string $title_left      Change the HTML to the left of the title.
    3427          *                                   Default empty.
    3428          *     @type string $title_right     Change the HTML to the right of the title.
    3429          *                                   Default empty.
    3430          *     @type bool   $show_split_view True for split view (two columns), false for
    3431          *                                   un-split view (single column). Default true.
     3426         *     @type string $title                      Titles the diff in a manner compatible
     3427         *                                              with the output. Default empty.
     3428         *     @type string $title_left                 Change the HTML to the left of the title.
     3429         *                                              Default empty.
     3430         *     @type string $title_right                Change the HTML to the right of the title.
     3431         *                                              Default empty.
     3432         *     @type bool   $show_split_view            True for split view (two columns), false for
     3433         *                                              un-split view (single column). Default true.
     3434         *     @type int    $max_word_diff_line_length  Changed lines longer than this many bytes get
     3435         *                                              no word-level highlighting. Default 10000.
     3436         *     @type int    $max_changed_lines          A changed block with more old and new lines
     3437         *                                              than this shows removed lines, then added
     3438         *                                              lines. Default 5000.
     3439         *     @type int    $max_changed_line_pairs     A changed block with more old-by-new line
     3440         *                                              pairs than this shows removed lines, then
     3441         *                                              added lines. Default 250000.
    34323442         * }
    34333443         * @return string Empty string if strings are equivalent or HTML with differences.
  • trunk/tests/phpunit/tests/diff/wpTextDiffRendererTable.php

    r65033 r65790  
    1414        public static function set_up_before_class() {
    1515                parent::set_up_before_class();
     16                require_once ABSPATH . 'wp-includes/Text/Diff.php';
    1617                require_once ABSPATH . 'wp-includes/Text/Diff/Renderer.php';
     18                require_once ABSPATH . 'wp-includes/Text/Diff/Renderer/inline.php';
    1719                require_once ABSPATH . 'wp-includes/class-wp-text-diff-renderer-table.php';
     20                require_once ABSPATH . 'wp-includes/class-wp-text-diff-renderer-inline.php';
    1821        }
    1922
    … …  
    2124                parent::set_up();
    2225                $this->diff_renderer_table = new WP_Text_Diff_Renderer_Table();
     26        }
     27
     28        /**
     29         * @ticket 66095
     30         * @ticket 48973
     31         * @covers WP_Text_Diff_Renderer_Table::_changed
     32         * @dataProvider data_changed_block_limits
     33         *
     34         * @param string $limit Name of the limit argument.
     35         * @param int    $value Limit that the 3 old and 3 new lines exceed.
     36         */
     37        public function test_changed_lists_deletions_before_additions_above_a_block_limit( $limit, $value ) {
     38                $renderer = $this->diff_renderer_table;
     39                $old      = array( 'alpha & old', 'bravo & old', 'charlie & old' );
     40                $new      = array( 'alpha & new', 'bravo & new', 'charlie & new' );
     41                $fallback = $renderer->_deleted( $old ) . $renderer->_added( $new );
     42
     43                $this->assertNotSame( $fallback, $renderer->_changed( $old, $new ) );
     44
     45                $limited = new WP_Text_Diff_Renderer_Table( array( $limit => $value ) );
     46                $this->assertSame( $fallback, $limited->_changed( $old, $new ) );
     47        }
     48
     49        /**
     50         * Data provider for test_changed_lists_deletions_before_additions_above_a_block_limit().
     51         *
     52         * @return array
     53         */
     54        public static function data_changed_block_limits() {
     55                return array(
     56                        'line pairs' => array( 'max_changed_line_pairs', 8 ),
     57                        'lines'      => array( 'max_changed_lines', 5 ),
     58                );
     59        }
     60
     61        /**
     62         * @ticket 66095
     63         * @covers WP_Text_Diff_Renderer_Table::_changed
     64         * @dataProvider data_long_line_pairs
     65         *
     66         * @param string $old_line Old line.
     67         * @param string $new_line New line.
     68         */
     69        public function test_changed_skips_word_highlighting_when_a_line_exceeds_the_length_limit( $old_line, $new_line ) {
     70                $renderer = $this->diff_renderer_table;
     71
     72                $this->assertSame(
     73                        '<tr>' . $renderer->deletedLine( str_replace( '&', '&amp;', $old_line ) ) . $renderer->addedLine( str_replace( '&', '&amp;', $new_line ) ) . "</tr>\n",
     74                        $renderer->_changed( array( $old_line ), array( $new_line ) )
     75                );
     76        }
     77
     78        /**
     79         * Data provider for test_changed_skips_word_highlighting_when_a_line_exceeds_the_length_limit().
     80         *
     81         * @return array
     82         */
     83        public static function data_long_line_pairs() {
     84                $short = str_repeat( 'x', 9000 ) . '&old';
     85                $long  = str_repeat( 'x', 9000 ) . '&' . str_repeat( 'y', 2999 );
     86
     87                return array(
     88                        'long old line' => array( $long, $short ),
     89                        'long new line' => array( $short, $long ),
     90                );
     91        }
     92
     93        /**
     94         * @ticket 66095
     95         * @covers ::wp_text_diff
     96         */
     97        public function test_wp_text_diff_limits_can_be_raised_through_args() {
     98                $old = str_repeat( 'x', 12000 ) . ' old';
     99                $new = str_repeat( 'x', 12000 ) . ' new';
     100
     101                $this->assertStringNotContainsString( '<del>old</del>', wp_text_diff( $old, $new ) );
     102                $this->assertStringContainsString( '<del>old</del>', wp_text_diff( $old, $new, array( 'max_word_diff_line_length' => 20000 ) ) );
    23103        }
    24104
Note: See TracChangeset for help on using the changeset viewer.