Skip to content

Commit f99957f

Browse files
committed
Script Loader: Omit sourceURL from translation data when not printed.
The data returned from `WP_Scripts::print_translations()` when `$display` is `false` may be used in unpredictable ways that are incompatible with `sourceURL` comments. Omit the `sourceURL` comment in this case. Developed in #10505. Follow-up to [60719]. Props jonsurrell, ralucastn, westonruter, peterwilsoncc. See #63887. git-svn-id: https://develop.svn.wordpress.org/trunk@61223 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 81e3dbe commit f99957f

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/wp-includes/class-wp-scripts.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,16 @@ public function do_item( $handle, $group = false ) {
350350

351351
$translations = $this->print_translations( $handle, false );
352352
if ( $translations ) {
353-
$translations = wp_get_inline_script_tag( $translations, array( 'id' => "{$handle}-js-translations" ) );
353+
/*
354+
* The sourceURL comment is not included by WP_Scripts::print_translations()
355+
* when `$display` is `false` to prevent issues where the script tag contents are used
356+
* by extenders for other purposes, for example concatenated with other script content.
357+
*
358+
* Include the sourceURL comment here as it would be when printed directly.
359+
*/
360+
$source_url = rawurlencode( "{$handle}-js-translations" );
361+
$translations .= "\n//# sourceURL={$source_url}";
362+
$translations = wp_get_inline_script_tag( $translations, array( 'id' => "{$handle}-js-translations" ) );
354363
}
355364

356365
if ( $this->do_concat ) {
@@ -722,18 +731,17 @@ public function print_translations( $handle, $display = true ) {
722731
return false;
723732
}
724733

725-
$source_url = rawurlencode( "{$handle}-js-translations" );
726-
727734
$output = <<<JS
728735
( function( domain, translations ) {
729736
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
730737
localeData[""].domain = domain;
731738
wp.i18n.setLocaleData( localeData, domain );
732739
} )( "{$domain}", {$json_translations} );
733-
//# sourceURL={$source_url}
734740
JS;
735741

736742
if ( $display ) {
743+
$source_url = rawurlencode( "{$handle}-js-translations" );
744+
$output .= "\n//# sourceURL={$source_url}";
737745
wp_print_inline_script_tag( $output, array( 'id' => "{$handle}-js-translations" ) );
738746
}
739747

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4075,4 +4075,22 @@ public function test_source_url_with_concat() {
40754075

40764076
$this->assertEqualHTML( $expected, $print_scripts );
40774077
}
4078+
4079+
/**
4080+
* Ensure that `::print_translations()` does not include the sourceURL comment when `$display` is false.
4081+
*
4082+
* @ticket 63887
4083+
* @covers ::print_translations
4084+
*/
4085+
public function test_print_translations_no_display_no_sourceurl() {
4086+
global $wp_scripts;
4087+
$this->add_html5_script_theme_support();
4088+
4089+
wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
4090+
wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
4091+
wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' );
4092+
4093+
$translations_script_data = $wp_scripts->print_translations( 'test-example', false );
4094+
$this->assertStringNotContainsStringIgnoringCase( 'sourceURL=', $translations_script_data );
4095+
}
40784096
}

0 commit comments

Comments
 (0)