Skip to content

Commit 5139923

Browse files
committed
Script Loader: Only emit CDATA wrapper comments in wp_get_inline_script_tag() for JavaScript.
This avoids erroneously adding CDATA wrapper comments for non-JavaScript scripts, including those for JSON such as the `importmap` for script modules in #56313. Props westonruter, flixos90, mukesh27, dmsnell. See #56313. Fixes #60320. git-svn-id: https://develop.svn.wordpress.org/trunk@57341 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a21e447 commit 5139923

2 files changed

Lines changed: 95 additions & 1 deletion

File tree

src/wp-includes/script-loader.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2879,7 +2879,17 @@ function wp_get_inline_script_tag( $javascript, $attributes = array() ) {
28792879
*
28802880
* @see https://www.w3.org/TR/xhtml1/#h-4.8
28812881
*/
2882-
if ( ! $is_html5 ) {
2882+
if (
2883+
! $is_html5 &&
2884+
(
2885+
! isset( $attributes['type'] ) ||
2886+
'module' === $attributes['type'] ||
2887+
str_contains( $attributes['type'], 'javascript' ) ||
2888+
str_contains( $attributes['type'], 'ecmascript' ) ||
2889+
str_contains( $attributes['type'], 'jscript' ) ||
2890+
str_contains( $attributes['type'], 'livescript' )
2891+
)
2892+
) {
28832893
/*
28842894
* If the string `]]>` exists within the JavaScript it would break
28852895
* out of any wrapping CDATA section added here, so to start, it's

tests/phpunit/tests/dependencies/wpInlineScriptTag.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
*/
1111
class Tests_Functions_wpInlineScriptTag extends WP_UnitTestCase {
1212

13+
private $original_theme_features = array();
14+
15+
public function set_up() {
16+
global $_wp_theme_features;
17+
parent::set_up();
18+
$this->original_theme_features = $_wp_theme_features;
19+
}
20+
21+
public function tear_down() {
22+
global $_wp_theme_features;
23+
$_wp_theme_features = $this->original_theme_features;
24+
parent::tear_down();
25+
}
26+
1327
private $event_handler = <<<'JS'
1428
document.addEventListener( 'DOMContentLoaded', function () {
1529
document.getElementById( 'elementID' )
@@ -133,4 +147,74 @@ public function test_get_inline_script_tag_with_duplicated_cdata_wrappers() {
133147
wp_get_inline_script_tag( "/* <![CDATA[ */ console.log( 'Hello World!' ); /* ]]> */" )
134148
);
135149
}
150+
151+
public function data_provider_to_test_cdata_wrapper_omitted_for_non_javascript_scripts() {
152+
return array(
153+
'no-type' => array(
154+
'type' => null,
155+
'data' => 'alert("hello")',
156+
'expected_cdata' => true,
157+
),
158+
'js-type' => array(
159+
'type' => 'text/javascript',
160+
'data' => 'alert("hello")',
161+
'expected_cdata' => true,
162+
),
163+
'js-alt-type' => array(
164+
'type' => 'application/javascript',
165+
'data' => 'alert("hello")',
166+
'expected_cdata' => true,
167+
),
168+
'module' => array(
169+
'type' => 'module',
170+
'data' => 'alert("hello")',
171+
'expected_cdata' => true,
172+
),
173+
'importmap' => array(
174+
'type' => 'importmap',
175+
'data' => '{"imports":{"bar":"http:\/\/localhost:10023\/bar.js?ver=6.5-alpha-57321"}}',
176+
'expected_cdata' => false,
177+
),
178+
'html' => array(
179+
'type' => 'text/html',
180+
'data' => '<div>template code</div>',
181+
'expected_cdata' => false,
182+
),
183+
'json' => array(
184+
'type' => 'application/json',
185+
'data' => '{}',
186+
'expected_cdata' => false,
187+
),
188+
'ld' => array(
189+
'type' => 'application/ld+json',
190+
'data' => '{}',
191+
'expected_cdata' => false,
192+
),
193+
'specrules' => array(
194+
'type' => 'speculationrules',
195+
'data' => '{}',
196+
'expected_cdata' => false,
197+
),
198+
);
199+
}
200+
201+
/**
202+
* Tests that CDATA wrapper is not added for non-JavaScript scripts.
203+
*
204+
* @ticket 60320
205+
*
206+
* @dataProvider data_provider_to_test_cdata_wrapper_omitted_for_non_javascript_scripts
207+
*/
208+
public function test_cdata_wrapper_omitted_for_non_javascript_scripts( $type, $data, $expected_cdata ) {
209+
remove_theme_support( 'html5' );
210+
211+
$attrs = array();
212+
if ( $type ) {
213+
$attrs['type'] = $type;
214+
}
215+
$script = wp_get_inline_script_tag( $data, $attrs );
216+
$this->assertSame( $expected_cdata, str_contains( $script, '/* <![CDATA[ */' ) );
217+
$this->assertSame( $expected_cdata, str_contains( $script, '/* ]]> */' ) );
218+
$this->assertStringContainsString( $data, $script );
219+
}
136220
}

0 commit comments

Comments
 (0)