Commit 166f9bb35b for wordpress.org
commit 166f9bb35b392e688795c366ecc2f99f17c3c2bb
Author: lancewillett <lancewillett@git.wordpress.org>
Date: Wed Nov 25 19:34:54 2026 +0000
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.
Built from https://develop.svn.wordpress.org/trunk@65790
git-svn-id: http://core.svn.wordpress.org/trunk@64819 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/class-wp-text-diff-renderer-table.php b/wp-includes/class-wp-text-diff-renderer-table.php
index 9eca479b61..36ed8a2205 100644
--- a/wp-includes/class-wp-text-diff-renderer-table.php
+++ b/wp-includes/class-wp-text-diff-renderer-table.php
@@ -67,6 +67,30 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
*/
protected $_diff_threshold = 0.6;
+ /**
+ * Maximum line length in bytes for word-level highlighting.
+ *
+ * @since 7.3.0
+ * @var int
+ */
+ protected $_max_word_diff_line_length = 10000;
+
+ /**
+ * Maximum number of line pairs to compare when matching changed lines.
+ *
+ * @since 7.3.0
+ * @var int
+ */
+ protected $_max_changed_line_pairs = 250000;
+
+ /**
+ * Maximum total number of old and new lines to match in a changed block.
+ *
+ * @since 7.3.0
+ * @var int
+ */
+ protected $_max_changed_lines = 5000;
+
/**
* Inline display helper object name.
*
@@ -285,6 +309,15 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
* @return string
*/
public function _changed( $orig, $final ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.finalFound
+ $old_count = count( $orig );
+ $new_count = count( $final );
+ // interleave_changed_lines() scores every old line against every new line.
+ if ( $old_count + $new_count > $this->_max_changed_lines
+ || $old_count * $new_count > $this->_max_changed_line_pairs
+ ) {
+ return $this->_deleted( $orig ) . $this->_added( $final );
+ }
+
$r = '';
/*
@@ -305,6 +338,12 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
// Compute word diffs for each matched pair using the inline diff.
foreach ( $orig_matches as $o => $f ) {
if ( is_numeric( $o ) && is_numeric( $f ) ) {
+ if ( strlen( $orig[ $o ] ) > $this->_max_word_diff_line_length
+ || strlen( $final[ $f ] ) > $this->_max_word_diff_line_length
+ ) {
+ continue;
+ }
+
$text_diff = new Text_Diff( 'auto', array( array( $orig[ $o ] ), array( $final[ $f ] ) ) );
$renderer = new $this->inline_diff_renderer();
$diff = $renderer->render( $text_diff );
diff --git a/wp-includes/pluggable.php b/wp-includes/pluggable.php
index 76198dcbd8..0093f2a33f 100644
--- a/wp-includes/pluggable.php
+++ b/wp-includes/pluggable.php
@@ -3411,6 +3411,8 @@ if ( ! function_exists( 'wp_text_diff' ) ) :
* are equivalent, then an empty string will be returned.
*
* @since 2.6.0
+ * @since 7.3.0 Added the `$max_word_diff_line_length`, `$max_changed_lines`,
+ * and `$max_changed_line_pairs` arguments.
*
* @see wp_parse_args() Used to change defaults to user defined settings.
* @uses Text_Diff
@@ -3421,14 +3423,22 @@ if ( ! function_exists( 'wp_text_diff' ) ) :
* @param string|array $args {
* Associative array of options to pass to WP_Text_Diff_Renderer_Table().
*
- * @type string $title Titles the diff in a manner compatible
- * with the output. Default empty.
- * @type string $title_left Change the HTML to the left of the title.
- * Default empty.
- * @type string $title_right Change the HTML to the right of the title.
- * Default empty.
- * @type bool $show_split_view True for split view (two columns), false for
- * un-split view (single column). Default true.
+ * @type string $title Titles the diff in a manner compatible
+ * with the output. Default empty.
+ * @type string $title_left Change the HTML to the left of the title.
+ * Default empty.
+ * @type string $title_right Change the HTML to the right of the title.
+ * Default empty.
+ * @type bool $show_split_view True for split view (two columns), false for
+ * un-split view (single column). Default true.
+ * @type int $max_word_diff_line_length Changed lines longer than this many bytes get
+ * no word-level highlighting. Default 10000.
+ * @type int $max_changed_lines A changed block with more old and new lines
+ * than this shows removed lines, then added
+ * lines. Default 5000.
+ * @type int $max_changed_line_pairs A changed block with more old-by-new line
+ * pairs than this shows removed lines, then
+ * added lines. Default 250000.
* }
* @return string Empty string if strings are equivalent or HTML with differences.
*/
diff --git a/wp-includes/version.php b/wp-includes/version.php
index ee10275810..28ca2c1c86 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.3-alpha-65789';
+$wp_version = '7.3-alpha-65790';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.