diff --git a/src/wp-includes/html-api/class-wp-html-tag-processor.php b/src/wp-includes/html-api/class-wp-html-tag-processor.php
index 0c153c4c34d89..9289d5d27f880 100644
--- a/src/wp-includes/html-api/class-wp-html-tag-processor.php
+++ b/src/wp-includes/html-api/class-wp-html-tag-processor.php
@@ -3817,7 +3817,14 @@ public function set_modifiable_text( string $plaintext_content ): bool {
return true;
}
- if ( self::STATE_MATCHED_TAG !== $this->parser_state ) {
+ /*
+ * The rest of this function handles modifiable text for special "atomic" HTML elements.
+ * Only tags in the HTML namespace should be processed.
+ */
+ if (
+ self::STATE_MATCHED_TAG !== $this->parser_state ||
+ 'html' !== $this->get_namespace()
+ ) {
return false;
}
diff --git a/tests/phpunit/tests/html-api/wpHtmlProcessorModifiableText.php b/tests/phpunit/tests/html-api/wpHtmlProcessorModifiableText.php
index 08a4514fa14fd..5d093ae05dd07 100644
--- a/tests/phpunit/tests/html-api/wpHtmlProcessorModifiableText.php
+++ b/tests/phpunit/tests/html-api/wpHtmlProcessorModifiableText.php
@@ -24,7 +24,7 @@ class Tests_HtmlApi_WpHtmlProcessorModifiableText extends WP_UnitTestCase {
* @param string $set_text Text to set.
* @param string $expected_html Expected HTML output.
*/
- public function test_modifiable_text_special_textarea( string $set_text, string $expected_html ) {
+ public function test_modifiable_text_special_textarea( string $set_text, string $expected_html ): void {
$processor = WP_HTML_Processor::create_fragment( '' );
$processor->next_token();
$processor->set_modifiable_text( $set_text );
@@ -50,9 +50,9 @@ public function test_modifiable_text_special_textarea( string $set_text, string
/**
* Data provider.
*
- * @return array[]
+ * @return array
*/
- public static function data_modifiable_text_special_textarea() {
+ public static function data_modifiable_text_special_textarea(): array {
return array(
'Leading newline' => array(
"\nAFTER NEWLINE",
@@ -68,4 +68,59 @@ public static function data_modifiable_text_special_textarea() {
),
);
}
+
+ /**
+ * Ensures that `set_modifiable_text()` returns false for elements that are not special "atomic" elements.
+ *
+ * This includes atomic-like foreign elements (`