From 31451f83ba2e408b7a48517bfc24865f1648732d Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 29 Aug 2025 19:22:15 +0200 Subject: [PATCH 01/13] Revert "Script Loader: Revert sourceURL addition." This reverts commit 2fe30e7828d4036f01848d7e348860595d0f6a08. --- src/wp-includes/class-wp-scripts.php | 10 ++ src/wp-includes/class-wp-styles.php | 5 + tests/phpunit/tests/blocks/editor.php | 1 + tests/phpunit/tests/dependencies/scripts.php | 169 +++++++++++++----- tests/phpunit/tests/dependencies/styles.php | 36 +++- .../tests/dependencies/wpLocalizeScript.php | 2 +- 6 files changed, 171 insertions(+), 52 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index b1e4c76c73d43..9f64e30283a85 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -222,6 +222,11 @@ public function print_extra_script( $handle, $display = true ) { return; } + $output .= sprintf( + "\n//# sourceURL=%s", + rawurlencode( "{$handle}-js-extra" ) + ); + if ( ! $display ) { return $output; } @@ -521,6 +526,11 @@ public function get_inline_script_data( $handle, $position = 'after' ) { return ''; } + $data[] = sprintf( + '//# sourceURL=%s', + rawurlencode( "{$handle}-js-{$position}" ) + ); + return trim( implode( "\n", $data ), "\n" ); } diff --git a/src/wp-includes/class-wp-styles.php b/src/wp-includes/class-wp-styles.php index e64378be5fc8d..0ddedcc58d6fb 100644 --- a/src/wp-includes/class-wp-styles.php +++ b/src/wp-includes/class-wp-styles.php @@ -337,6 +337,11 @@ public function print_inline_style( $handle, $display = true ) { return false; } + $output[] = sprintf( + '/*# sourceURL=%s */', + rawurlencode( "{$handle}-inline-css" ) + ); + $output = implode( "\n", $output ); if ( ! $display ) { diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index 97d18d89529e3..2ba757921038d 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -762,6 +762,7 @@ public function test_ensure_preload_data_script_tag_closes() { HTML; diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 2cd51aad6ab70..fca4d183b786d 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -116,11 +116,11 @@ public function data_provider_delayed_strategies() { */ public function test_after_inline_script_with_delayed_main_script( $strategy ) { wp_enqueue_script( 'ms-isa-1', 'http://example.org/ms-isa-1.js', array(), null, compact( 'strategy' ) ); - wp_add_inline_script( 'ms-isa-1', 'console.log("after one");', 'after' ); + wp_add_inline_script( 'ms-isa-1', 'console.log(\'after one\');', 'after' ); $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; $expected .= wp_get_inline_script_tag( - 'console.log("after one");', + "console.log('after one');\n//# sourceURL=ms-isa-1-js-after", array( 'id' => 'ms-isa-1-js-after', ) @@ -143,12 +143,12 @@ public function test_after_inline_script_with_delayed_main_script( $strategy ) { */ public function test_after_inline_script_with_blocking_main_script() { wp_enqueue_script( 'ms-insa-3', 'http://example.org/ms-insa-3.js', array(), null ); - wp_add_inline_script( 'ms-insa-3', 'console.log("after one");', 'after' ); + wp_add_inline_script( 'ms-insa-3', 'console.log(\'after one\');', 'after' ); $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; $expected .= wp_get_inline_script_tag( - 'console.log("after one");', + "console.log('after one');\n//# sourceURL=ms-insa-3-js-after", array( 'id' => 'ms-insa-3-js-after', ) @@ -174,15 +174,15 @@ public function test_after_inline_script_with_blocking_main_script() { */ public function test_before_inline_scripts_with_delayed_main_script( $strategy ) { wp_enqueue_script( 'ds-i1-1', 'http://example.org/ds-i1-1.js', array(), null, compact( 'strategy' ) ); - wp_add_inline_script( 'ds-i1-1', 'console.log("before first");', 'before' ); + wp_add_inline_script( 'ds-i1-1', 'console.log(\'before first\');', 'before' ); wp_enqueue_script( 'ds-i1-2', 'http://example.org/ds-i1-2.js', array(), null, compact( 'strategy' ) ); wp_enqueue_script( 'ds-i1-3', 'http://example.org/ds-i1-3.js', array(), null, compact( 'strategy' ) ); wp_enqueue_script( 'ms-i1-1', 'http://example.org/ms-i1-1.js', array( 'ds-i1-1', 'ds-i1-2', 'ds-i1-3' ), null, compact( 'strategy' ) ); - wp_add_inline_script( 'ms-i1-1', 'console.log("before last");', 'before' ); + wp_add_inline_script( 'ms-i1-1', 'console.log(\'before last\');', 'before' ); $output = get_echo( 'wp_print_scripts' ); $expected = wp_get_inline_script_tag( - 'console.log("before first");', + "console.log('before first');\n//# sourceURL=ds-i1-1-js-before", array( 'id' => 'ds-i1-1-js-before', ) @@ -191,7 +191,7 @@ public function test_before_inline_scripts_with_delayed_main_script( $strategy ) $expected .= "\n"; $expected .= "\n"; $expected .= wp_get_inline_script_tag( - 'console.log("before last");', + "console.log('before last');\n//# sourceURL=ms-i1-1-js-before", array( 'id' => 'ms-i1-1-js-before', 'type' => 'text/javascript', @@ -513,23 +513,27 @@ public function data_provider_to_test_various_strategy_dependency_chains() { HTML @@ -558,34 +562,40 @@ public function data_provider_to_test_various_strategy_dependency_chains() { HTML @@ -606,23 +616,27 @@ public function data_provider_to_test_various_strategy_dependency_chains() { HTML @@ -643,23 +657,27 @@ public function data_provider_to_test_various_strategy_dependency_chains() { HTML @@ -683,22 +701,26 @@ public function data_provider_to_test_various_strategy_dependency_chains() { HTML @@ -725,34 +747,40 @@ public function data_provider_to_test_various_strategy_dependency_chains() { HTML @@ -778,22 +806,26 @@ public function data_provider_to_test_various_strategy_dependency_chains() { HTML @@ -817,34 +849,40 @@ public function data_provider_to_test_various_strategy_dependency_chains() { HTML @@ -868,34 +906,40 @@ public function data_provider_to_test_various_strategy_dependency_chains() { HTML @@ -916,23 +960,27 @@ public function data_provider_to_test_various_strategy_dependency_chains() { HTML @@ -949,6 +997,7 @@ public function data_provider_to_test_various_strategy_dependency_chains() { @@ -967,6 +1016,7 @@ public function data_provider_to_test_various_strategy_dependency_chains() { HTML @@ -1014,12 +1064,14 @@ public function data_provider_to_test_various_strategy_dependency_chains() { HTML @@ -1618,7 +1670,7 @@ public function test_wp_script_add_data_with_data_key() { // Enqueue and add data. wp_enqueue_script( 'test-only-data', 'example.com', array(), null ); wp_script_add_data( 'test-only-data', 'data', 'testing' ); - $expected = "\n"; + $expected = "\n"; $expected .= "\n"; // Go! @@ -1656,7 +1708,7 @@ public function test_wp_script_add_data_with_data_and_conditional_keys() { wp_enqueue_script( 'test-conditional-with-data', 'example.com', array(), null ); wp_script_add_data( 'test-conditional-with-data', 'data', 'testing' ); wp_script_add_data( 'test-conditional-with-data', 'conditional', 'lt IE 9' ); - $expected = "\n"; + $expected = "\n"; $expected .= "\n"; $expected = str_replace( "'", '"', $expected ); @@ -1880,6 +1932,7 @@ public function test_wp_add_inline_script_before() { HTML; @@ -1900,6 +1953,7 @@ public function test_wp_add_inline_script_after() { HTML; @@ -1915,9 +1969,9 @@ public function test_wp_add_inline_script_before_and_after() { wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); wp_add_inline_script( 'test-example', 'console.log("after");' ); - $expected = "\n"; + $expected = "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -1930,7 +1984,7 @@ public function test_wp_add_inline_script_before_for_handle_without_source() { wp_enqueue_script( 'test-example' ); wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); - $expected = "\n"; + $expected = "\n"; $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -1943,7 +1997,7 @@ public function test_wp_add_inline_script_after_for_handle_without_source() { wp_enqueue_script( 'test-example' ); wp_add_inline_script( 'test-example', 'console.log("after");' ); - $expected = "\n"; + $expected = "\n"; $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -1957,8 +2011,8 @@ public function test_wp_add_inline_script_before_and_after_for_handle_without_so wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); wp_add_inline_script( 'test-example', 'console.log("after");' ); - $expected = "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -1973,9 +2027,9 @@ public function test_wp_add_inline_script_multiple() { wp_add_inline_script( 'test-example', 'console.log("after");' ); wp_add_inline_script( 'test-example', 'console.log("after");' ); - $expected = "\n"; + $expected = "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -1989,10 +2043,10 @@ public function test_wp_add_inline_script_localized_data_is_added_first() { wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); wp_add_inline_script( 'test-example', 'console.log("after");' ); - $expected = "\n"; - $expected .= "\n"; + $expected = "\n"; + $expected .= "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) ); } @@ -2013,9 +2067,9 @@ public function test_wp_add_inline_script_before_with_concat() { wp_add_inline_script( 'one', 'console.log("before one");', 'before' ); wp_add_inline_script( 'two', 'console.log("before two");', 'before' ); - $expected = "\n"; + $expected = "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2037,7 +2091,7 @@ public function test_wp_add_inline_script_before_with_concat2() { wp_add_inline_script( 'one', 'console.log("before one");', 'before' ); - $expected = "\n"; + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2064,9 +2118,9 @@ public function test_wp_add_inline_script_after_with_concat() { $expected = "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) ); @@ -2082,14 +2136,14 @@ public function test_wp_add_inline_script_after_and_before_with_concat_and_condi $wp_scripts->default_dirs = array( '/wp-admin/js/', '/wp-includes/js/' ); // Default dirs as in wp-includes/script-loader.php. $expected_localized = "\n"; $expected_localized = str_replace( "'", '"', $expected_localized ); $expected = "\n"; $expected = str_replace( "'", '"', $expected ); @@ -2117,7 +2171,7 @@ public function test_wp_add_inline_script_after_with_concat_and_core_dependency( $expected = "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); wp_add_inline_script( 'test-example', 'console.log("after");' ); @@ -2142,7 +2196,7 @@ public function test_wp_add_inline_script_after_with_concat_and_conditional_and_ $expected = "\n"; $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); @@ -2168,7 +2222,7 @@ public function test_wp_add_inline_script_before_with_concat_and_core_dependency $wp_scripts->do_concat = true; $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); @@ -2193,18 +2247,18 @@ public function test_wp_add_inline_script_before_after_concat_with_core_dependen $wp_scripts->do_concat = true; $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; - $expected .= "\n"; - + $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); wp_add_inline_script( 'test-example', 'console.log("before");', 'before' ); wp_enqueue_script( 'test-example2', 'http://example2.com', array( 'wp-a11y' ), null ); @@ -2248,6 +2302,7 @@ public function test_wp_add_inline_script_customize_dependency() { $expected_tail .= "\n"; @@ -2283,7 +2338,7 @@ public function test_wp_add_inline_script_after_for_core_scripts_with_concat_is_ wp_enqueue_script( 'four', '/wp-includes/js/script4.js' ); $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2307,7 +2362,7 @@ public function test_wp_add_inline_script_before_third_core_script_prints_two_co wp_enqueue_script( 'four', '/wp-includes/js/script4.js' ); $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2327,8 +2382,8 @@ public function data_provider_to_test_get_inline_script() { '/*before foo 1*/', ), 'delayed' => false, - 'expected_data' => '/*before foo 1*/', - 'expected_tag' => "\n", + 'expected_data' => "/*before foo 1*/\n//# sourceURL=foo-js-before", + 'expected_tag' => "\n", ), 'after-blocking' => array( 'position' => 'after', @@ -2337,8 +2392,8 @@ public function data_provider_to_test_get_inline_script() { '/*after foo 2*/', ), 'delayed' => false, - 'expected_data' => "/*after foo 1*/\n/*after foo 2*/", - 'expected_tag' => "\n", + 'expected_data' => "/*after foo 1*/\n/*after foo 2*/\n//# sourceURL=foo-js-after", + 'expected_tag' => "\n", ), 'before-delayed' => array( 'position' => 'before', @@ -2346,8 +2401,8 @@ public function data_provider_to_test_get_inline_script() { '/*before foo 1*/', ), 'delayed' => true, - 'expected_data' => '/*before foo 1*/', - 'expected_tag' => "\n", + 'expected_data' => "/*before foo 1*/\n//# sourceURL=foo-js-before", + 'expected_tag' => "\n", ), 'after-delayed' => array( 'position' => 'after', @@ -2356,8 +2411,8 @@ public function data_provider_to_test_get_inline_script() { '/*after foo 2*/', ), 'delayed' => true, - 'expected_data' => "/*after foo 1*/\n/*after foo 2*/", - 'expected_tag' => "\n", + 'expected_data' => "/*after foo 1*/\n/*after foo 2*/\n//# sourceURL=foo-js-after", + 'expected_tag' => "\n", ), ); } @@ -2999,7 +3054,7 @@ public function test_wp_localize_script_data_formats( $l10n_data, $expected ) { wp_enqueue_script( 'test-example', 'example.com', array(), null ); wp_localize_script( 'test-example', 'testExample', $l10n_data ); - $expected = "\n"; + $expected = "\n"; $expected .= "\n"; $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) ); @@ -3568,4 +3623,26 @@ private function _scripts_from_package_json() { $provider = array(); return $data['dependencies']; } + + /** + * @ticket 63887 + */ + public function test_source_url_encoding() { + $this->add_html5_script_theme_support(); + + $handle = '# test/ #'; + wp_enqueue_script( $handle, '/example.js', array(), '0.0' ); + wp_add_inline_script( $handle, '"ok";' ); + + $expected = << + + +HTML; + + $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) ); + } } diff --git a/tests/phpunit/tests/dependencies/styles.php b/tests/phpunit/tests/dependencies/styles.php index 9cb6283c488d5..e077e3b320006 100644 --- a/tests/phpunit/tests/dependencies/styles.php +++ b/tests/phpunit/tests/dependencies/styles.php @@ -150,6 +150,7 @@ public function test_inline_styles() { $expected = "\n"; $expected .= "\n"; wp_enqueue_style( 'handle', 'http://example.com', array(), 1 ); @@ -179,6 +180,7 @@ public function test_inline_styles_concat() { $expected = "\n"; $expected .= "\n"; wp_enqueue_style( 'handle', 'http://example.com', array(), 1 ); @@ -274,6 +276,7 @@ public function test_multiple_inline_styles() { $expected .= "\n"; wp_enqueue_style( 'handle', 'http://example.com', array(), 1 ); @@ -292,18 +295,19 @@ public function test_multiple_inline_styles() { */ public function test_plugin_doing_inline_styles_wrong() { - $style = "'; + $style .= '}'; $expected = "\n"; + $expected .= "\n"; wp_enqueue_style( 'handle', 'http://example.com', array(), 1 ); - wp_add_inline_style( 'handle', $style ); + wp_add_inline_style( 'handle', "" ); $this->assertSame( $expected, get_echo( 'wp_print_styles' ) ); } @@ -332,6 +336,7 @@ public function test_conditional_inline_styles_are_also_conditional() { @@ -363,6 +368,7 @@ public function test_wp_add_inline_style_for_handle_without_source() { $expected .= "\n"; $expected .= "\n"; wp_register_style( 'handle-one', 'http://example.com', array(), 1 ); @@ -632,4 +638,24 @@ public function test_wp_maybe_inline_styles_no_path() { $this->assertSame( $GLOBALS['wp_styles']->registered['test-handle']->src, $url ); } + + /** + * @ticket 63887 + */ + public function test_source_url_encoding() { + $handle = '# test/ #'; + wp_enqueue_style( $handle, '/example.css', array(), '0.0' ); + wp_add_inline_style( $handle, 'custom-el { content: "ok"; }' ); + + $expected = << + + +HTML; + + $this->assertEqualHTML( $expected, get_echo( 'wp_print_styles' ) ); + } } diff --git a/tests/phpunit/tests/dependencies/wpLocalizeScript.php b/tests/phpunit/tests/dependencies/wpLocalizeScript.php index a57754973cf40..7ec8604d40b87 100644 --- a/tests/phpunit/tests/dependencies/wpLocalizeScript.php +++ b/tests/phpunit/tests/dependencies/wpLocalizeScript.php @@ -56,7 +56,7 @@ public function test_wp_localize_script_outputs_safe_json() { $output = get_echo( 'wp_print_scripts' ); - $expected = "\n"; + $expected = "\n"; $expected .= "\n"; $this->assertEqualHTML( $expected, $output ); From 012eb7f481ad0393d78f513589893e1739dc5e6a Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 1 Sep 2025 11:19:23 +0200 Subject: [PATCH 02/13] Move global concatenate_scripts mod/reset to set_up/tear_down --- tests/phpunit/tests/dependencies/scripts.php | 27 ++++++++------------ 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index fca4d183b786d..30ce26c64e81e 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -16,6 +16,11 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { */ protected $old_wp_scripts; + /** + * @var bool + */ + protected $old_concatenate_scripts; + /** * @var WP_Styles */ @@ -32,8 +37,9 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { public function set_up() { parent::set_up(); - $this->old_wp_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null; - $this->old_wp_styles = isset( $GLOBALS['wp_styles'] ) ? $GLOBALS['wp_styles'] : null; + $this->old_wp_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null; + $this->old_wp_styles = isset( $GLOBALS['wp_styles'] ) ? $GLOBALS['wp_styles'] : null; + $this->old_concatenate_scripts = isset( $GLOBALS['concatenate_scripts'] ) ? $GLOBALS['concatenate_scripts'] : null; remove_action( 'wp_default_scripts', 'wp_default_scripts' ); remove_action( 'wp_default_scripts', 'wp_default_packages' ); $GLOBALS['wp_scripts'] = new WP_Scripts(); @@ -55,8 +61,9 @@ public function set_up() { } public function tear_down() { - $GLOBALS['wp_scripts'] = $this->old_wp_scripts; - $GLOBALS['wp_styles'] = $this->old_wp_styles; + $GLOBALS['wp_scripts'] = $this->old_wp_scripts; + $GLOBALS['wp_styles'] = $this->old_wp_styles; + $GLOBALS['concatenate_scripts'] = $this->old_concatenate_scripts; add_action( 'wp_default_scripts', 'wp_default_scripts' ); parent::tear_down(); } @@ -1480,7 +1487,6 @@ public function test_script_strategy_doing_it_wrong_via_enqueue() { public function test_concatenate_with_defer_strategy() { global $wp_scripts, $concatenate_scripts, $wp_version; - $old_value = $concatenate_scripts; $concatenate_scripts = true; $wp_scripts->do_concat = true; @@ -1494,9 +1500,6 @@ public function test_concatenate_with_defer_strategy() { wp_print_scripts(); $print_scripts = get_echo( '_print_scripts' ); - // Reset global before asserting. - $concatenate_scripts = $old_value; - $expected = "\n"; $expected .= "\n"; @@ -1515,7 +1518,6 @@ public function test_concatenate_with_defer_strategy() { public function test_concatenate_with_async_strategy() { global $wp_scripts, $concatenate_scripts, $wp_version; - $old_value = $concatenate_scripts; $concatenate_scripts = true; $wp_scripts->do_concat = true; @@ -1529,9 +1531,6 @@ public function test_concatenate_with_async_strategy() { wp_print_scripts(); $print_scripts = get_echo( '_print_scripts' ); - // Reset global before asserting. - $concatenate_scripts = $old_value; - $expected = "\n"; $expected .= "\n"; @@ -1551,7 +1550,6 @@ public function test_concatenate_with_async_strategy() { public function test_concatenate_with_blocking_script_before_and_after_script_with_defer_strategy() { global $wp_scripts, $concatenate_scripts, $wp_version; - $old_value = $concatenate_scripts; $concatenate_scripts = true; $wp_scripts->do_concat = true; @@ -1568,9 +1566,6 @@ public function test_concatenate_with_blocking_script_before_and_after_script_wi wp_print_scripts(); $print_scripts = get_echo( '_print_scripts' ); - // Reset global before asserting. - $concatenate_scripts = $old_value; - $expected = "\n"; $expected .= "\n"; From 1cfa0766ff8e4d1d77952f89da3b2bfed083aa0d Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 1 Sep 2025 13:07:29 +0200 Subject: [PATCH 03/13] Add tests to disable sourceURL with concat --- tests/phpunit/tests/dependencies/scripts.php | 34 +++++++++++++++++++ tests/phpunit/tests/dependencies/styles.php | 35 +++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 30ce26c64e81e..6912b38c4993d 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -3640,4 +3640,38 @@ public function test_source_url_encoding() { $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) ); } + + /** + * Concatenated scripts are problematic with sourceURL. + * + * @ticket 63887 + */ + public function test_source_url_disabled_with_concat() { + global $wp_scripts, $concatenate_scripts, $wp_version; + $this->add_html5_script_theme_support(); + + $concatenate_scripts = true; + + $wp_scripts->do_concat = true; + $wp_scripts->default_dirs = array( $this->default_scripts_dir ); + + wp_enqueue_script( 'one', $this->default_scripts_dir . '1.js' ); + wp_enqueue_script( 'two', $this->default_scripts_dir . '2.js' ); + wp_localize_script( 'one', 'one', array( 'key' => 'val' ) ); + wp_localize_script( 'two', 'two', array( 'key' => 'val' ) ); + + wp_print_scripts(); + $print_scripts = get_echo( '_print_scripts' ); + + $expected = << +/* */ + + + +HTML; + + $this->assertEqualHTML( $expected, $print_scripts ); + } } diff --git a/tests/phpunit/tests/dependencies/styles.php b/tests/phpunit/tests/dependencies/styles.php index e077e3b320006..9453bd04b13b5 100644 --- a/tests/phpunit/tests/dependencies/styles.php +++ b/tests/phpunit/tests/dependencies/styles.php @@ -167,7 +167,6 @@ public function test_inline_styles() { * @ticket 24813 */ public function test_inline_styles_concat() { - global $wp_styles; $wp_styles->do_concat = true; @@ -658,4 +657,38 @@ public function test_source_url_encoding() { $this->assertEqualHTML( $expected, get_echo( 'wp_print_styles' ) ); } + + /** + * Concatenated styles are problematic with sourceURL. + * + * @ticket 63887 + */ + public function test_source_url_disabled_with_concat() { + global $wp_styles, $wp_version; + add_theme_support( 'html5', array( 'style' ) ); + + $wp_styles->do_concat = true; + $wp_styles->default_dirs = array( '/wp-admin/' ); + + wp_enqueue_style( 'one', '/wp-admin/1.css' ); + wp_enqueue_style( 'two', '/wp-admin/2.css' ); + wp_add_inline_style( 'one', 'h1 { background: blue; }' ); + wp_add_inline_style( 'two', 'h2 { color: green; }' ); + + wp_print_styles(); + $printed = get_echo( '_print_styles' ); + + $expected = << + +HTML; + + $this->assertEqualHTML( $expected, $printed ); + } } From cd1a726be5a747ef44caeb9551cf6b070f278ff8 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 1 Sep 2025 13:08:49 +0200 Subject: [PATCH 04/13] Disable sourceURL comments when concatenating --- src/wp-includes/class-wp-scripts.php | 20 ++++++++++++-------- src/wp-includes/class-wp-styles.php | 10 ++++++---- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 9f64e30283a85..ab58c4070b4cf 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -222,10 +222,12 @@ public function print_extra_script( $handle, $display = true ) { return; } - $output .= sprintf( - "\n//# sourceURL=%s", - rawurlencode( "{$handle}-js-extra" ) - ); + if ( ! $this->do_concat ) { + $output .= sprintf( + "\n//# sourceURL=%s", + rawurlencode( "{$handle}-js-extra" ) + ); + } if ( ! $display ) { return $output; @@ -526,10 +528,12 @@ public function get_inline_script_data( $handle, $position = 'after' ) { return ''; } - $data[] = sprintf( - '//# sourceURL=%s', - rawurlencode( "{$handle}-js-{$position}" ) - ); + if ( ! $this->do_concat ) { + $data[] = sprintf( + '//# sourceURL=%s', + rawurlencode( "{$handle}-js-{$position}" ) + ); + } return trim( implode( "\n", $data ), "\n" ); } diff --git a/src/wp-includes/class-wp-styles.php b/src/wp-includes/class-wp-styles.php index 0ddedcc58d6fb..d1637bcd809d4 100644 --- a/src/wp-includes/class-wp-styles.php +++ b/src/wp-includes/class-wp-styles.php @@ -337,10 +337,12 @@ public function print_inline_style( $handle, $display = true ) { return false; } - $output[] = sprintf( - '/*# sourceURL=%s */', - rawurlencode( "{$handle}-inline-css" ) - ); + if ( ! $this->do_concat ) { + $output[] = sprintf( + '/*# sourceURL=%s */', + rawurlencode( "{$handle}-inline-css" ) + ); + } $output = implode( "\n", $output ); From 2b2af0c5058f6207e49168fb3661d46473e39e99 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 1 Sep 2025 13:29:30 +0200 Subject: [PATCH 05/13] Update tests for do_concat changes --- tests/phpunit/tests/dependencies/scripts.php | 25 ++++++++++---------- tests/phpunit/tests/dependencies/styles.php | 1 - 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 6912b38c4993d..8cba6da918076 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -2062,7 +2062,7 @@ public function test_wp_add_inline_script_before_with_concat() { wp_add_inline_script( 'one', 'console.log("before one");', 'before' ); wp_add_inline_script( 'two', 'console.log("before two");', 'before' ); - $expected = "\n"; + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2086,7 +2086,7 @@ public function test_wp_add_inline_script_before_with_concat2() { wp_add_inline_script( 'one', 'console.log("before one");', 'before' ); - $expected = "\n"; + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2113,7 +2113,7 @@ public function test_wp_add_inline_script_after_with_concat() { $expected = "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2131,14 +2131,14 @@ public function test_wp_add_inline_script_after_and_before_with_concat_and_condi $wp_scripts->default_dirs = array( '/wp-admin/js/', '/wp-includes/js/' ); // Default dirs as in wp-includes/script-loader.php. $expected_localized = "\n"; $expected_localized = str_replace( "'", '"', $expected_localized ); $expected = "\n"; $expected = str_replace( "'", '"', $expected ); @@ -2166,7 +2166,7 @@ public function test_wp_add_inline_script_after_with_concat_and_core_dependency( $expected = "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); wp_add_inline_script( 'test-example', 'console.log("after");' ); @@ -2191,7 +2191,7 @@ public function test_wp_add_inline_script_after_with_concat_and_conditional_and_ $expected = "\n"; $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); @@ -2217,7 +2217,7 @@ public function test_wp_add_inline_script_before_with_concat_and_core_dependency $wp_scripts->do_concat = true; $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); @@ -2242,13 +2242,12 @@ public function test_wp_add_inline_script_before_after_concat_with_core_dependen $wp_scripts->do_concat = true; $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2333,7 +2332,7 @@ public function test_wp_add_inline_script_after_for_core_scripts_with_concat_is_ wp_enqueue_script( 'four', '/wp-includes/js/script4.js' ); $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2357,7 +2356,7 @@ public function test_wp_add_inline_script_before_third_core_script_prints_two_co wp_enqueue_script( 'four', '/wp-includes/js/script4.js' ); $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; diff --git a/tests/phpunit/tests/dependencies/styles.php b/tests/phpunit/tests/dependencies/styles.php index 9453bd04b13b5..681619792ea46 100644 --- a/tests/phpunit/tests/dependencies/styles.php +++ b/tests/phpunit/tests/dependencies/styles.php @@ -179,7 +179,6 @@ public function test_inline_styles_concat() { $expected = "\n"; $expected .= "\n"; wp_enqueue_style( 'handle', 'http://example.com', array(), 1 ); From 4f72a4b6c6d94c0aee2d0b03fbe52387d01b9d68 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 2 Sep 2025 12:00:02 +0200 Subject: [PATCH 06/13] Add sourceURL comments to concatenated scripts and styles --- src/wp-includes/script-loader.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index def31178880d5..b610d9fe11694 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2209,6 +2209,7 @@ function _print_scripts() { echo "\n\n"; echo "/* print_code; + echo sprintf( "\n//# sourceURL=%s\n", rawurlencode( 'js-inline-concat-' . $concat ) ); echo "/* ]]> */\n"; echo "\n"; } @@ -2391,8 +2392,9 @@ function _print_styles() { $dir = $wp_styles->text_direction; $ver = $wp_styles->default_version; - $concat = str_split( $concat, 128 ); - $concatenated = ''; + $concat_source_url = 'css-inline-concat-' . $concat; + $concat = str_split( $concat, 128 ); + $concatenated = ''; foreach ( $concat as $key => $chunk ) { $concatenated .= "&load%5Bchunk_{$key}%5D={$chunk}"; @@ -2404,6 +2406,7 @@ function _print_styles() { if ( ! empty( $wp_styles->print_code ) ) { echo "\n"; echo $wp_styles->print_code; + echo sprintf( "\n/*# sourceURL=%s */", rawurlencode( $concat_source_url ) ); echo "\n\n"; } } From f74e4b1e72e7aec019c4871842ab7ba3b51d4575 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 2 Sep 2025 12:03:18 +0200 Subject: [PATCH 07/13] Update concat tests --- tests/phpunit/tests/dependencies/scripts.php | 8 ++++---- tests/phpunit/tests/dependencies/styles.php | 5 ++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 8cba6da918076..bd51b83084b94 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -3641,11 +3641,9 @@ public function test_source_url_encoding() { } /** - * Concatenated scripts are problematic with sourceURL. - * * @ticket 63887 */ - public function test_source_url_disabled_with_concat() { + public function test_source_url_with_concat() { global $wp_scripts, $concatenate_scripts, $wp_version; $this->add_html5_script_theme_support(); @@ -3665,7 +3663,9 @@ public function test_source_url_disabled_with_concat() { $expected = << /* */ +var one = {"key":"val"};var two = {"key":"val"}; +//# sourceURL=js-inline-concat-one%2Ctwo +/* ]]> */ diff --git a/tests/phpunit/tests/dependencies/styles.php b/tests/phpunit/tests/dependencies/styles.php index 681619792ea46..70755580290aa 100644 --- a/tests/phpunit/tests/dependencies/styles.php +++ b/tests/phpunit/tests/dependencies/styles.php @@ -658,11 +658,9 @@ public function test_source_url_encoding() { } /** - * Concatenated styles are problematic with sourceURL. - * * @ticket 63887 */ - public function test_source_url_disabled_with_concat() { + public function test_source_url_with_concat() { global $wp_styles, $wp_version; add_theme_support( 'html5', array( 'style' ) ); @@ -685,6 +683,7 @@ public function test_source_url_disabled_with_concat() { > HTML; From d0ea5ff5b63c81810626d3c13a19c91ec3856fed Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 4 Sep 2025 10:09:25 +0200 Subject: [PATCH 08/13] Always print a sourceURL in before/after script tags --- src/wp-includes/class-wp-scripts.php | 10 ++++---- tests/phpunit/tests/dependencies/scripts.php | 25 +++++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 9c511801a14a5..6db1390ad3747 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -531,12 +531,10 @@ public function get_inline_script_data( $handle, $position = 'after' ) { return ''; } - if ( ! $this->do_concat ) { - $data[] = sprintf( - '//# sourceURL=%s', - rawurlencode( "{$handle}-js-{$position}" ) - ); - } + $data[] = sprintf( + "//# sourceURL=%s", + rawurlencode( "{$handle}-js-{$position}" ) + ); return trim( implode( "\n", $data ), "\n" ); } diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 0f688a708cf67..251b8a510a204 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -2195,7 +2195,7 @@ public function test_wp_add_inline_script_before_with_concat() { wp_add_inline_script( 'one', 'console.log("before one");', 'before' ); wp_add_inline_script( 'two', 'console.log("before two");', 'before' ); - $expected = "\n"; + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2219,7 +2219,7 @@ public function test_wp_add_inline_script_before_with_concat2() { wp_add_inline_script( 'one', 'console.log("before one");', 'before' ); - $expected = "\n"; + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2246,7 +2246,7 @@ public function test_wp_add_inline_script_after_with_concat() { $expected = "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2269,9 +2269,9 @@ public function test_wp_add_inline_script_after_and_before_with_concat_and_condi $expected_localized = str_replace( "'", '"', $expected_localized ); $expected = "\n"; $expected = str_replace( "'", '"', $expected ); @@ -2299,7 +2299,7 @@ public function test_wp_add_inline_script_after_with_concat_and_core_dependency( $expected = "\n"; $expected .= "\n"; - $expected .= "\n"; + $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); wp_add_inline_script( 'test-example', 'console.log("after");' ); @@ -2324,7 +2324,7 @@ public function test_wp_add_inline_script_after_with_concat_and_conditional_and_ $expected = "\n"; $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); @@ -2350,7 +2350,7 @@ public function test_wp_add_inline_script_before_with_concat_and_core_dependency $wp_scripts->do_concat = true; $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null ); @@ -2375,12 +2375,15 @@ public function test_wp_add_inline_script_before_after_concat_with_core_dependen $wp_scripts->do_concat = true; $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2465,7 +2468,7 @@ public function test_wp_add_inline_script_after_for_core_scripts_with_concat_is_ wp_enqueue_script( 'four', '/wp-includes/js/script4.js' ); $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; @@ -2489,7 +2492,7 @@ public function test_wp_add_inline_script_before_third_core_script_prints_two_co wp_enqueue_script( 'four', '/wp-includes/js/script4.js' ); $expected = "\n"; - $expected .= "\n"; + $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; From aa432f71191e6c76cecca4ae001851023b731fea Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 4 Sep 2025 10:19:29 +0200 Subject: [PATCH 09/13] Comment the do_concat behavior on extra scripts --- src/wp-includes/class-wp-scripts.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 6db1390ad3747..faec380b7db52 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -222,6 +222,13 @@ public function print_extra_script( $handle, $display = true ) { return; } + /* + * Do not print a sourceURL comment if concatenation is enabled. + * + * Extra scripts may be concatenated into a single script. + * The line-based sourceURL comments may a concatenated script and + * do not make sense when multiple are joined together. + */ if ( ! $this->do_concat ) { $output .= sprintf( "\n//# sourceURL=%s", From e0329740b0ffbd026039c308b25c0e3111883300 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 4 Sep 2025 10:26:44 +0200 Subject: [PATCH 10/13] Add more sourceURL encoding tests --- tests/phpunit/tests/dependencies/scripts.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 251b8a510a204..1fa3d2094dfd5 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -3761,13 +3761,24 @@ public function test_source_url_encoding() { $this->add_html5_script_theme_support(); $handle = '# test/ #'; + wp_enqueue_script( $handle, '/example.js', array(), '0.0' ); - wp_add_inline_script( $handle, '"ok";' ); + wp_add_inline_script( $handle, '"before";', 'before' ); + wp_add_inline_script( $handle, '"after";' ); + wp_localize_script( $handle, 'test', array() ); $expected = << +var test = []; +//# sourceURL=%23%20test%2F%3C%2Fscript%3E%20%23-js-extra + + From b29be704e0ed98e9cb9e7b24edd67bc2f4266423 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 4 Sep 2025 10:38:50 +0200 Subject: [PATCH 11/13] Add sourceURL comments to translations --- src/wp-includes/class-wp-scripts.php | 3 +++ tests/phpunit/tests/dependencies/scripts.php | 1 + 2 files changed, 4 insertions(+) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index faec380b7db52..1575bbb007607 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -715,12 +715,15 @@ public function print_translations( $handle, $display = true ) { return false; } + $source_url = rawurlencode( "{$handle}-js-translations" ); + $output = << */ JS; From 15a4b50ff5ea6af58efb78e2c8c6c7f654b9ae46 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 4 Sep 2025 10:40:51 +0200 Subject: [PATCH 12/13] Single quotes lint --- src/wp-includes/class-wp-scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 1575bbb007607..ca849e1ef7422 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -539,7 +539,7 @@ public function get_inline_script_data( $handle, $position = 'after' ) { } $data[] = sprintf( - "//# sourceURL=%s", + '//# sourceURL=%s', rawurlencode( "{$handle}-js-{$position}" ) ); From 1bbecc8d667481cb19d91f4cf341723de202123c Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 8 Sep 2025 13:11:07 +0200 Subject: [PATCH 13/13] Add comment about sourceURL for inline scripts --- src/wp-includes/class-wp-scripts.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index ca849e1ef7422..ce3963e741e89 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -538,6 +538,12 @@ public function get_inline_script_data( $handle, $position = 'after' ) { return ''; } + /* + * Print sourceURL comment regardless of concatenation. + * + * Inline scripts prevent scripts from being concatenated, so + * sourceURL comments are safe to print for inline scripts. + */ $data[] = sprintf( '//# sourceURL=%s', rawurlencode( "{$handle}-js-{$position}" )