diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 6425afe874..a04c580274 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2879,7 +2879,17 @@ function wp_get_inline_script_tag( $javascript, $attributes = array() ) { * * @see https://www.w3.org/TR/xhtml1/#h-4.8 */ - if ( ! $is_html5 ) { + if ( + ! $is_html5 && + ( + ! isset( $attributes['type'] ) || + 'module' === $attributes['type'] || + str_contains( $attributes['type'], 'javascript' ) || + str_contains( $attributes['type'], 'ecmascript' ) || + str_contains( $attributes['type'], 'jscript' ) || + str_contains( $attributes['type'], 'livescript' ) + ) + ) { /* * If the string `]]>` exists within the JavaScript it would break * out of any wrapping CDATA section added here, so to start, it's diff --git a/tests/phpunit/tests/dependencies/wpInlineScriptTag.php b/tests/phpunit/tests/dependencies/wpInlineScriptTag.php index 2bbb665d39..7192570838 100644 --- a/tests/phpunit/tests/dependencies/wpInlineScriptTag.php +++ b/tests/phpunit/tests/dependencies/wpInlineScriptTag.php @@ -10,6 +10,20 @@ */ class Tests_Functions_wpInlineScriptTag extends WP_UnitTestCase { + private $original_theme_features = array(); + + public function set_up() { + global $_wp_theme_features; + parent::set_up(); + $this->original_theme_features = $_wp_theme_features; + } + + public function tear_down() { + global $_wp_theme_features; + $_wp_theme_features = $this->original_theme_features; + parent::tear_down(); + } + private $event_handler = <<<'JS' document.addEventListener( 'DOMContentLoaded', function () { document.getElementById( 'elementID' ) @@ -133,4 +147,74 @@ JS; wp_get_inline_script_tag( "/* */" ) ); } + + public function data_provider_to_test_cdata_wrapper_omitted_for_non_javascript_scripts() { + return array( + 'no-type' => array( + 'type' => null, + 'data' => 'alert("hello")', + 'expected_cdata' => true, + ), + 'js-type' => array( + 'type' => 'text/javascript', + 'data' => 'alert("hello")', + 'expected_cdata' => true, + ), + 'js-alt-type' => array( + 'type' => 'application/javascript', + 'data' => 'alert("hello")', + 'expected_cdata' => true, + ), + 'module' => array( + 'type' => 'module', + 'data' => 'alert("hello")', + 'expected_cdata' => true, + ), + 'importmap' => array( + 'type' => 'importmap', + 'data' => '{"imports":{"bar":"http:\/\/localhost:10023\/bar.js?ver=6.5-alpha-57321"}}', + 'expected_cdata' => false, + ), + 'html' => array( + 'type' => 'text/html', + 'data' => '