From fcc0f498d2335a1588b1b1c0b4c50b0452e7a70b Mon Sep 17 00:00:00 2001 From: HasnainAshfaq Date: Sat, 25 Jul 2026 17:59:39 +0500 Subject: [PATCH] Formatting: Fix wptexturize encoding ampersands inside script/pre/code when content contains < MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a no-texturize tag (script, pre, code, etc.) contains a raw '<' character — as in JavaScript like `if(a) are only encountered at the top level, so the fix does not affect them. This became more user-visible in WordPress 7.0, which introduced a dedicated JavaScript panel in the Custom HTML block where users enter code that can contain '<' comparisons. Fixes #43785. --- src/wp-includes/formatting.php | 14 ++++++++-- .../phpunit/tests/formatting/wpTexturize.php | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 74a28109b6536..c67a5be811c25 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -250,8 +250,18 @@ function wptexturize( $text, $reset = false ) { } else { // This is an HTML element delimiter. - // Replace each & with & unless it already looks like an entity. - $curl = preg_replace( '/&(?!#(?:\d+|x[a-f0-9]+);|[a-z1-4]{1,8};)/i', '&', $curl ); + /* + * Replace each & with & unless it already looks like an entity, + * but only when outside no-texturize tags like ', + wptexturize( '' ) + ); + $this->assertSame( + '', + wptexturize( '' ) + ); + $this->assertSame( + '
if(a',
+			wptexturize( '
if(a' )
+		);
+		$this->assertSame(
+			'if(a',
+			wptexturize( 'if(a' )
+		);
+		// Ensure & in real HTML attributes is still encoded outside no-texturize tags.
+		$this->assertSame(
+			'link',
+			wptexturize( 'link' )
+		);
+	}
+
 	/**
 	 * @ticket 1418
 	 */