From dcd2a32c5572580feb355ca20f838404c6947f6b Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Mar 2023 18:33:13 +0000 Subject: [PATCH 01/57] standalone and non-standalone --- src/wp-includes/class-wp-scripts.php | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index bde93df63d8c7..38428c2bfa2ed 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -294,18 +294,35 @@ public function do_item( $handle, $group = false ) { $cond_after = "\n"; } + $strategy = $this->get_eligible_loading_strategy( $handle ); + $before_handle = $this->print_inline_script( $handle, 'before', false ); - $after_handle = $this->print_inline_script( $handle, 'after', false ); if ( $before_handle ) { $before_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $before_handle ); } - if ( $after_handle ) { - $after_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_handle ); + if( '' !== $strategy ) { + $after_handle = $this->print_inline_script( $handle, 'after', false ); + + if ( $after_handle ) { + $after_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_handle ); + } + } else { + $after_standalone_handle = $this->print_inline_script( $handle, 'after-standalone', false ); + + if ( $after_standalone_handle ) { + $after_standalone_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_standalone_handle ); + } + + $after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false ); + + if ( $after_non_standalone_handle ) { + $after_non_standalone_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_non_standalone_handle ); + } } - if ( $before_handle || $after_handle ) { + if ( $before_handle || $after_handle || $after_standalone_handle || $after_non_standalone_handle ) { $inline_script_tag = $cond_before . $before_handle . $after_handle . $cond_after; } else { $inline_script_tag = ''; @@ -390,7 +407,6 @@ public function do_item( $handle, $group = false ) { return true; } - $strategy = $this->get_eligible_loading_strategy( $handle ); if ( '' !== $strategy ) { $strategy = ' ' . $strategy; } From 8d33fac01affef155f1be73c79c790b4ebaed948 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Mar 2023 18:41:13 +0000 Subject: [PATCH 02/57] phpcs fixes --- src/wp-includes/class-wp-scripts.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 38428c2bfa2ed..71b75b430a502 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -302,20 +302,20 @@ public function do_item( $handle, $group = false ) { $before_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $before_handle ); } - if( '' !== $strategy ) { - $after_handle = $this->print_inline_script( $handle, 'after', false ); + if ( '' !== $strategy ) { + $after_handle = $this->print_inline_script( $handle, 'after', false ); if ( $after_handle ) { $after_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_handle ); } } else { - $after_standalone_handle = $this->print_inline_script( $handle, 'after-standalone', false ); + $after_standalone_handle = $this->print_inline_script( $handle, 'after-standalone', false ); if ( $after_standalone_handle ) { $after_standalone_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_standalone_handle ); } - $after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false ); + $after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false ); if ( $after_non_standalone_handle ) { $after_non_standalone_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_non_standalone_handle ); From e4b461d451c5d2a770614920ae5649875c950b94 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Mar 2023 18:50:24 +0000 Subject: [PATCH 03/57] set defaults --- src/wp-includes/class-wp-scripts.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 71b75b430a502..4c90427288a17 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -302,6 +302,9 @@ public function do_item( $handle, $group = false ) { $before_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $before_handle ); } + $after_handle = false; + $after_standalone_handle = false; + $after_non_standalone_handle = false; if ( '' !== $strategy ) { $after_handle = $this->print_inline_script( $handle, 'after', false ); From 8421a1c7a4864d3f330abb5e8c0e70e176844941 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Mar 2023 19:04:26 +0000 Subject: [PATCH 04/57] update print_inline_script for stand alone --- src/wp-includes/class-wp-scripts.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 4c90427288a17..91e592d041021 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -501,7 +501,11 @@ public function print_inline_script( $handle, $position = 'after', $display = tr $output = trim( implode( "\n", $output ), "\n" ); if ( $display ) { - printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); + if( 'after-non-standalone' === $position ) { + printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); + } else { + printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); + } } return $output; From 371e42fd0e8ca9aaf965bdb0d78485d5b92530bd Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Mar 2023 19:22:23 +0000 Subject: [PATCH 05/57] fix issue with standalone appending --- src/wp-includes/class-wp-scripts.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 91e592d041021..1fa1aa4778f65 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -302,9 +302,7 @@ public function do_item( $handle, $group = false ) { $before_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $before_handle ); } - $after_handle = false; - $after_standalone_handle = false; - $after_non_standalone_handle = false; + $after_handle = ''; if ( '' !== $strategy ) { $after_handle = $this->print_inline_script( $handle, 'after', false ); @@ -315,17 +313,17 @@ public function do_item( $handle, $group = false ) { $after_standalone_handle = $this->print_inline_script( $handle, 'after-standalone', false ); if ( $after_standalone_handle ) { - $after_standalone_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_standalone_handle ); + $after_handle .= sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_standalone_handle ); } $after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false ); if ( $after_non_standalone_handle ) { - $after_non_standalone_handle = sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_non_standalone_handle ); + $after_handle .= sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_non_standalone_handle ); } } - if ( $before_handle || $after_handle || $after_standalone_handle || $after_non_standalone_handle ) { + if ( $before_handle || $after_handle ) { $inline_script_tag = $cond_before . $before_handle . $after_handle . $cond_after; } else { $inline_script_tag = ''; @@ -421,7 +419,6 @@ public function do_item( $handle, $group = false ) { esc_attr( $handle ), $strategy ); - // TODO: Handle onload logic for defer/async here. $tag .= $after_handle . $cond_after; /** @@ -501,7 +498,7 @@ public function print_inline_script( $handle, $position = 'after', $display = tr $output = trim( implode( "\n", $output ), "\n" ); if ( $display ) { - if( 'after-non-standalone' === $position ) { + if ( 'after-non-standalone' === $position ) { printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); } else { printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); From c517bc17cc8fd1ed3684901211f28a850e080405 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Mar 2023 19:29:49 +0000 Subject: [PATCH 06/57] bug fix incorrect strategy check --- 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 1fa1aa4778f65..04ac0a4765a62 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -303,7 +303,7 @@ public function do_item( $handle, $group = false ) { } $after_handle = ''; - if ( '' !== $strategy ) { + if ( '' === $strategy ) { $after_handle = $this->print_inline_script( $handle, 'after', false ); if ( $after_handle ) { From 04aabf5c634a50679187fad019966350ed1dbbca Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Mar 2023 20:35:14 +0000 Subject: [PATCH 07/57] numbered attributes --- src/wp-includes/class-wp-scripts.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 04ac0a4765a62..fc5794d915f93 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -499,7 +499,13 @@ public function print_inline_script( $handle, $position = 'after', $display = tr if ( $display ) { if ( 'after-non-standalone' === $position ) { - printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); + printf( + '\n%4$s\n\n', + $this->type_attr, + esc_attr( $handle ), + esc_attr( $position ), + $output + ); } else { printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); } From e366d5d35976240309cb4a701a4be7cc5063aec2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Mar 2023 22:07:21 +0000 Subject: [PATCH 08/57] onload script --- src/wp-includes/class-wp-scripts.php | 32 ++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index fc5794d915f93..e5ab88b2f8217 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -245,6 +245,26 @@ public function print_extra_script( $handle, $display = true ) { return true; } + /** + * Prints a loader script if there is text/template registered script. + * + * @param bool $display Optional. Whether to print the extra script + * instead of just returning it. Default true. + * @return bool|string Print loader script if `$display` is true, string otherwise. + */ + public function print_template_loader_script( $display ) { + $output = << { + let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); + scripts.forEach( (script) => { script.setAttribute("type","text/javascript") }) +} +JS; + + $script = sprintf( "\n", $output ); + + return true; + } + /** * Processes a script dependency. * @@ -319,7 +339,12 @@ public function do_item( $handle, $group = false ) { $after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false ); if ( $after_non_standalone_handle ) { - $after_handle .= sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_non_standalone_handle ); + $after_handle .= sprintf( + '\n%s\n\n', + $this->type_attr, + esc_attr( $handle ), + $after_non_standalone_handle + ); } } @@ -410,10 +435,13 @@ public function do_item( $handle, $group = false ) { if ( '' !== $strategy ) { $strategy = ' ' . $strategy; + if ( ! empty( $after_non_standalone_handle ) ) { + $strategy .= ' onload=\'wpLoadAfterScripts(\"%3$s\")\''; + } } $tag = $translations . $cond_before . $before_handle; $tag .= sprintf( - "\n", + '\n', $this->type_attr, esc_url( $src ), esc_attr( $handle ), From 0fafd88b431a8e19914ef11d720b082aa82d7866 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 10 Mar 2023 22:19:30 +0000 Subject: [PATCH 09/57] fix bug --- 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 e5ab88b2f8217..368f03bb7ab52 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -441,7 +441,7 @@ public function do_item( $handle, $group = false ) { } $tag = $translations . $cond_before . $before_handle; $tag .= sprintf( - '\n', + '\n', $this->type_attr, esc_url( $src ), esc_attr( $handle ), From 386022edd75a29d53d89a53b862a2a0c643ad1cf Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Mar 2023 01:02:18 +0000 Subject: [PATCH 10/57] sprintf issue with \n --- src/wp-includes/class-wp-scripts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 368f03bb7ab52..1fcc640476466 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -436,12 +436,12 @@ public function do_item( $handle, $group = false ) { if ( '' !== $strategy ) { $strategy = ' ' . $strategy; if ( ! empty( $after_non_standalone_handle ) ) { - $strategy .= ' onload=\'wpLoadAfterScripts(\"%3$s\")\''; + $strategy .= sprintf( " onload='wpLoadAfterScripts(\"%s\")'", esc_attr( $handle ) ); } } $tag = $translations . $cond_before . $before_handle; $tag .= sprintf( - '\n', + "\n", $this->type_attr, esc_url( $src ), esc_attr( $handle ), From fe7acc059934502bf2bc95fb0b6175cea0218a38 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Mar 2023 01:02:39 +0000 Subject: [PATCH 11/57] phpcs fixes --- 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 1fcc640476466..d037bb3940543 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -436,7 +436,7 @@ public function do_item( $handle, $group = false ) { if ( '' !== $strategy ) { $strategy = ' ' . $strategy; if ( ! empty( $after_non_standalone_handle ) ) { - $strategy .= sprintf( " onload='wpLoadAfterScripts(\"%s\")'", esc_attr( $handle ) ); + $strategy .= sprintf( " onload='wpLoadAfterScripts(\"%s\")'", esc_attr( $handle ) ); } } $tag = $translations . $cond_before . $before_handle; From 394405fdfc05376ff59333fe30d8e54117c5dfee Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Mar 2023 01:06:36 +0000 Subject: [PATCH 12/57] print template loader script --- src/wp-includes/class-wp-scripts.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index d037bb3940543..a0f0d7e22bb48 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -252,15 +252,19 @@ public function print_extra_script( $handle, $display = true ) { * instead of just returning it. Default true. * @return bool|string Print loader script if `$display` is true, string otherwise. */ - public function print_template_loader_script( $display ) { + public function print_template_loader_script( $display = true ) { $output = << { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript") }) } JS; - $script = sprintf( "\n", $output ); + if ( $display ) { + echo $script; + } else { + return $script; + } return true; } From cc5dace90f5386bc1ce2cbe8fc4e69b86185258f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Sat, 11 Mar 2023 01:16:35 +0000 Subject: [PATCH 13/57] \n to EOL --- src/wp-includes/class-wp-scripts.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index a0f0d7e22bb48..0f6cc13ea612d 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -344,10 +344,11 @@ public function do_item( $handle, $group = false ) { if ( $after_non_standalone_handle ) { $after_handle .= sprintf( - '\n%s\n\n', + '%4$s%3$s%4$s%4$s', $this->type_attr, esc_attr( $handle ), - $after_non_standalone_handle + $after_non_standalone_handle, + PHP_EOL ); } } @@ -532,11 +533,12 @@ public function print_inline_script( $handle, $position = 'after', $display = tr if ( $display ) { if ( 'after-non-standalone' === $position ) { printf( - '\n%4$s\n\n', + '%5$s%4$s%5$s%5$s', $this->type_attr, esc_attr( $handle ), esc_attr( $position ), - $output + $output, + PHP_EOL ); } else { printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); From b558f345d4fc8f5f17879d266cc7fe28e8fd8d0a Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 13 Mar 2023 15:52:42 +0000 Subject: [PATCH 14/57] Loading the script before other script --- src/wp-includes/class-wp-scripts.php | 24 ------------------------ src/wp-includes/default-filters.php | 1 + src/wp-includes/script-loader.php | 26 ++++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 0f6cc13ea612d..4d20b89037ca8 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -245,30 +245,6 @@ public function print_extra_script( $handle, $display = true ) { return true; } - /** - * Prints a loader script if there is text/template registered script. - * - * @param bool $display Optional. Whether to print the extra script - * instead of just returning it. Default true. - * @return bool|string Print loader script if `$display` is true, string otherwise. - */ - public function print_template_loader_script( $display = true ) { - $output = << { - let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); - scripts.forEach( (script) => { script.setAttribute("type","text/javascript") }) -} -JS; - $script = sprintf( "\n", $output ); - if ( $display ) { - echo $script; - } else { - return $script; - } - - return true; - } - /** * Processes a script dependency. * diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index f10dc6f280b2b..0ad5131e2af97 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -568,6 +568,7 @@ add_action( 'enqueue_block_editor_assets', 'wp_enqueue_editor_format_library_assets' ); add_action( 'enqueue_block_editor_assets', 'wp_enqueue_global_styles_css_custom_properties' ); add_filter( 'wp_print_scripts', 'wp_just_in_time_script_localization' ); +add_action( 'wp_print_scripts', 'wp_print_template_loader_script' ); add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' ); add_filter( 'customize_controls_print_styles', 'wp_resource_hints', 1 ); add_action( 'admin_head', 'wp_check_widget_editor_deps' ); diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index f584df22de73c..9ab37704e2281 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1843,6 +1843,32 @@ function wp_just_in_time_script_localization() { ); } + +/** + * Prints a loader script if there is text/template registered script. + * + * @param bool $display Optional. Whether to print the extra script + * instead of just returning it. Default true. + * @return bool|string Print loader script if `$display` is true, string otherwise. + */ +function wp_print_template_loader_script( $display = true ) { + $output = << { +let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); +scripts.forEach( (script) => { script.setAttribute("type","text/javascript") }) +} +JS; + $script = sprintf( "\n", $output ); + if ( $display ) { + echo $script; + } else { + return $script; + } + + return true; +} + + /** * Localizes the jQuery UI datepicker. * From f04a24644038a0eaa1c99345a4aa388face1e1f4 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 14 Mar 2023 15:51:28 +0000 Subject: [PATCH 15/57] temp commit --- src/wp-includes/class-wp-scripts.php | 8 +++++++ src/wp-includes/script-loader.php | 33 ++++++++++++---------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 4d20b89037ca8..a8247fc95361e 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -121,6 +121,13 @@ class WP_Scripts extends WP_Dependencies { * @var array */ public $default_dirs; + + /** + * True if there is any inline script that is meant to load later. + * + * @var bool + */ + public $has_load_later_inline = false; /** * Holds a string which contains the type attribute for script tag. @@ -326,6 +333,7 @@ public function do_item( $handle, $group = false ) { $after_non_standalone_handle, PHP_EOL ); + $this->has_load_later_inline = true; } } diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 9ab37704e2281..2c0fa0f82c65f 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1846,27 +1846,22 @@ function wp_just_in_time_script_localization() { /** * Prints a loader script if there is text/template registered script. - * - * @param bool $display Optional. Whether to print the extra script - * instead of just returning it. Default true. - * @return bool|string Print loader script if `$display` is true, string otherwise. */ -function wp_print_template_loader_script( $display = true ) { - $output = << { -let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); -scripts.forEach( (script) => { script.setAttribute("type","text/javascript") }) -} -JS; - $script = sprintf( "\n", $output ); - if ( $display ) { - echo $script; - } else { - return $script; +function wp_print_template_loader_script() { + $wp_scripts = wp_scripts(); + if( $wp_scripts->has_load_later_inline ) { + $output = << { + let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); + scripts.forEach( (script) => { + script.setAttribute("type","text/javascript"); + eval(script.innerHTML); + }) + } + JS; + printf( "\n", $output ); + } } - - return true; -} /** From b5f4df492b19edd304fb881e4c2902bc061badc0 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 15 Mar 2023 01:41:57 +0000 Subject: [PATCH 16/57] check if any delayed inline scripts --- src/wp-includes/class-wp-scripts.php | 26 ++++++++++++++++++-------- src/wp-includes/script-loader.php | 5 ++--- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index a8247fc95361e..b1622e12bc204 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -121,13 +121,6 @@ class WP_Scripts extends WP_Dependencies { * @var array */ public $default_dirs; - - /** - * True if there is any inline script that is meant to load later. - * - * @var bool - */ - public $has_load_later_inline = false; /** * Holds a string which contains the type attribute for script tag. @@ -326,7 +319,7 @@ public function do_item( $handle, $group = false ) { $after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false ); if ( $after_non_standalone_handle ) { - $after_handle .= sprintf( + $after_handle .= sprintf( '%4$s%3$s%4$s%4$s', $this->type_attr, esc_attr( $handle ), @@ -795,6 +788,23 @@ public function add_data( $handle, $key, $value ) { return parent::add_data( $handle, $key, $value ); } + /** + * Check all handles for any delayed inline scripts. + * + * @return bool True if script present. False if empty. + */ + public function maybe_has_delayed_inline_script() { + foreach ( $this->registered as $handle => $script ) { + if ( in_array( $this->get_intended_strategy( $handle ), array( 'defer', 'async' ), true ) ) { + // non standalone after scripts of async or defer are usually delayed. + if ( $this->has_non_standalone_inline_script( $handle, 'after' ) ) { + return true; + } + } + } + return false; + } + /** * Normalize the data inside the $args parameter and support backward compatibility. * diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 2c0fa0f82c65f..177ce9239500e 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1849,7 +1849,7 @@ function wp_just_in_time_script_localization() { */ function wp_print_template_loader_script() { $wp_scripts = wp_scripts(); - if( $wp_scripts->has_load_later_inline ) { + if ( $wp_scripts->maybe_has_delayed_inline_script() ) { $output = << { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); @@ -1861,8 +1861,7 @@ function wp_print_template_loader_script() { JS; printf( "\n", $output ); } - } - +} /** * Localizes the jQuery UI datepicker. From e0e560e9d287d553d4020caf92815add4ad3c8f0 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 15 Mar 2023 19:11:41 +0000 Subject: [PATCH 17/57] non-stanadalone after script test cases --- tests/phpunit/tests/dependencies/scripts.php | 68 ++++++++++++++++++-- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 93f4dae28aed7..38f832ddd8aae 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -63,6 +63,62 @@ public function test_wp_enqueue_script() { $this->assertSame( '', get_echo( 'wp_print_scripts' ) ); } + /** + * Test non standalone after with defer. + * + * @ticket 12009 + * @dataProvider data_non_standalone_after_inline_script_with_defer + */ + public function test_non_standalone_after_inline_script_with_defer( $expected, $output, $message ) { + $this->assertSame( $expected, $output, $message ); + } + + public function data_non_standalone_after_inline_script_with_defer() { + $data = array(); + + // If the main script with defer strategy has a `after` inline script; the inline script is inserted as type='module'. + wp_enqueue_script( 'ms-insa-1', 'http://example.org/ms-insa-1.js', array(), null, array( 'strategy' => 'defer' ) ); + wp_add_inline_script( 'ms-insa-1', 'console.log("after one");', 'after' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $expected .= "\n"; + array_push( $data, array( $expected, $output, 'Expected type="text/template" for inline script.' ) ); + + // If the main script with async strategy has a `after` inline script; the inline script is inserted as type='module'. + wp_enqueue_script( 'ms-insa-2', 'http://example.org/ms-insa-2.js', array(), null, array( 'strategy' => 'defer' ) ); + wp_add_inline_script( 'ms-insa-2', 'console.log("after one");', 'after' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $expected .= "\n"; + array_push( $data, array( $expected, $output, 'Expected type="text/template" for inline script.' ) ); + + // If the main script with blocking strategy has a `after` inline script; the inline script is inserted as type='javascript'. + wp_enqueue_script( 'ms-insa-3', 'http://example.org/ms-insa-3.js', array(), null, array( 'strategy' => 'blocking' ) ); + wp_add_inline_script( 'ms-insa-3', 'console.log("after one");', 'after' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $expected .= "\n"; + array_push( $data, array( $expected, $output, 'Expected type="text/javascript" or no type for inline script.' ) ); + + // If the main script with no strategy has a `after` inline script; the inline script is inserted as type='javascript'. + wp_enqueue_script( 'ms-insa-4', 'http://example.org/ms-insa-4.js', array(), null ); + wp_add_inline_script( 'ms-insa-4', 'console.log("after one");', 'after' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $expected .= "\n"; + array_push( $data, array( $expected, $output, 'Expected type="text/javascript" or no type for inline script.' ) ); + + return $data; + } + /** * Test non standalone before with defer. * @@ -82,7 +138,7 @@ public function data_non_standalone_before_inline_script_with_defer() { wp_enqueue_script( 'ds-i1-3', 'http://example.org/ds-i1-3.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-i1-1', 'http://example.org/ms-i1-1.js', array( 'ds-i1-1', 'ds-i1-2', 'ds-i1-3' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-i1-1', 'console.log("before one");', 'before' ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; $expected .= "\n"; $expected .= "\n"; @@ -98,7 +154,7 @@ public function data_non_standalone_before_inline_script_with_defer() { wp_enqueue_script( 'ds-i2-3', 'http://example.org/ds-i2-3.js', array( 'ds-i2-2' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-i2-1', 'http://example.org/ms-i2-1.js', array( 'ds-i2-3' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ds-i2-2', 'console.log("before one");', 'before' ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; $expected .= "\n"; @@ -129,7 +185,7 @@ public function data_non_standalone_before_inline_script_with_defer() { wp_enqueue_script( 'ch2-ds-i4-2', 'http://example.org/ch2-ds-i4-2.js', array( 'ch2-ds-i4-1' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ch2-ds-i4-2', 'console.log("before one");', 'before' ); wp_enqueue_script( 'ms-i4-1', 'http://example.org/ms-i4-1.js', array( 'ch2-ds-i4-1', 'ch2-ds-i4-2' ), null, array( 'strategy' => 'defer' ) ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; $expected .= "\n"; $expected .= "\n"; @@ -162,7 +218,7 @@ public function data_standalone_inline_script() { wp_enqueue_script( 'ds-is1-3', 'http://example.org/ds-is1-3.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-is1-1', 'http://example.org/ms-is1-1.js', array( 'ds-is1-1', 'ds-is1-2', 'ds-is1-3' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-is1-1', 'console.log("before one");', 'before', true ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; $expected .= "\n"; $expected .= "\n"; @@ -178,7 +234,7 @@ public function data_standalone_inline_script() { wp_enqueue_script( 'ds-is2-3', 'http://example.org/ds-is2-3.js', array( 'ds-is2-2' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-is2-1', 'http://example.org/ms-is2-1.js', array( 'ds-is2-3' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ds-is2-2', 'console.log("before one");', 'before', true ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; $expected .= "\n"; + $expected .= "\n"; + $expected .= "\n"; + $this->assertSame( $expected, $output ); + } + + /** + * Test standalone after inline script. + * + * @ticket 12009 + * @dataProvider data_standalone_after_inline_script + */ + public function test_standalone_after_inline_script( $expected, $output, $message ) { + $this->assertSame( $expected, $output, $message ); + } + + public function data_standalone_after_inline_script() { + $data = array(); + + // If the main script with defer strategy has a `after` inline script; the inline script is not affected. + wp_enqueue_script( 'ms-isa-1', 'http://example.org/ms-isa-1.js', array(), null, array( 'strategy' => 'defer' ) ); + wp_add_inline_script( 'ms-isa-1', 'console.log("after one");', 'after', true ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $expected .= "\n"; + array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); + + // If the main script with async strategy has a `after` inline script; the inline script is not affected. + wp_enqueue_script( 'ms-isa-2', 'http://example.org/ms-isa-2.js', array(), null, array( 'strategy' => 'defer' ) ); + wp_add_inline_script( 'ms-isa-2', 'console.log("after one");', 'after', true ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; + $expected .= "\n"; + array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); + + return $data; + } + /** - * Test non standalone after with defer. + * Test non standalone after inline script. * * @ticket 12009 - * @dataProvider data_non_standalone_after_inline_script_with_defer + * @dataProvider data_non_standalone_after_inline_script */ - public function test_non_standalone_after_inline_script_with_defer( $expected, $output, $message ) { + public function test_non_standalone_after_inline_script( $expected, $output, $message ) { $this->assertSame( $expected, $output, $message ); } - public function data_non_standalone_after_inline_script_with_defer() { + public function data_non_standalone_after_inline_script() { $data = array(); // If the main script with defer strategy has a `after` inline script; the inline script is inserted as type='module'. @@ -104,7 +163,7 @@ public function data_non_standalone_after_inline_script_with_defer() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected type="text/javascript" or no type for inline script.' ) ); + array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); // If the main script with no strategy has a `after` inline script; the inline script is inserted as type='javascript'. wp_enqueue_script( 'ms-insa-4', 'http://example.org/ms-insa-4.js', array(), null ); @@ -114,7 +173,7 @@ public function data_non_standalone_after_inline_script_with_defer() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected type="text/javascript" or no type for inline script.' ) ); + array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); return $data; } From ff25567cce81763e8a109642df621970c59f2230 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 15 Mar 2023 19:54:00 +0000 Subject: [PATCH 19/57] phpcs fix --- tests/phpunit/tests/dependencies/scripts.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 11a5d1f345c74..a9b7f6a18a781 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -70,7 +70,6 @@ public function test_wp_enqueue_script() { * @ticket 12009 */ public function test_non_standalone_and_standalone_after_script_combined() { - // If the main script with defer strategy has a `after` inline script; expected one with type='javascript' and other with type='module'. wp_enqueue_script( 'ms-isinsa-1', 'http://example.org/ms-isinsa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isinsa-1', 'console.log("after one");', 'after', true ); From b2d6ec1292283e3f4e1cf31c5d26b6a596440f9a Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 15 Mar 2023 19:57:40 +0000 Subject: [PATCH 20/57] for consistency --- 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 04ac0a4765a62..5cd81aca2869e 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -499,7 +499,7 @@ public function print_inline_script( $handle, $position = 'after', $display = tr if ( $display ) { if ( 'after-non-standalone' === $position ) { - printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); + printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); } else { printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); } From d110556951c28d4984cd92575a4cafab31e8aef2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 16 Mar 2023 01:15:31 +0000 Subject: [PATCH 21/57] add new line --- src/wp-includes/script-loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 177ce9239500e..98d3f75644b1f 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1859,7 +1859,7 @@ function wp_print_template_loader_script() { }) } JS; - printf( "\n", $output ); + printf( "\n", $output ); } } From b6b7022c07324848823081f3b4ab2cd900847976 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 16 Mar 2023 02:21:46 +0000 Subject: [PATCH 22/57] backward compatibility --- src/wp-includes/script-loader.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 98d3f75644b1f..bf0b28c61c7e9 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1851,14 +1851,14 @@ function wp_print_template_loader_script() { $wp_scripts = wp_scripts(); if ( $wp_scripts->maybe_has_delayed_inline_script() ) { $output = << { - let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); - scripts.forEach( (script) => { - script.setAttribute("type","text/javascript"); - eval(script.innerHTML); - }) - } - JS; +let wpLoadAfterScripts = ( handle ) => { + let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); + scripts.forEach( (script) => { + script.setAttribute("type","text/javascript"); + eval(script.innerHTML); + }) +} +JS; printf( "\n", $output ); } } From 9e8c37876b5027be93583831569424401aa9658e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 16 Mar 2023 02:46:13 +0000 Subject: [PATCH 23/57] update test utility and scripts. --- tests/phpunit/includes/utils.php | 8 ++ tests/phpunit/tests/dependencies/scripts.php | 101 ++++++++++++++----- 2 files changed, 83 insertions(+), 26 deletions(-) diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php index 30af6aa348490..7a4042b2cee7d 100644 --- a/tests/phpunit/includes/utils.php +++ b/tests/phpunit/includes/utils.php @@ -645,3 +645,11 @@ function test_rest_expand_compact_links( $links ) { } return $links; } + +/** + * Removes all handles from $wp_script. + */ +function unregister_all_script_handles() { + global $wp_scripts; + $wp_scripts->registered = array(); +} diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index a9b7f6a18a781..985e2438cc70c 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -71,17 +71,30 @@ public function test_wp_enqueue_script() { */ public function test_non_standalone_and_standalone_after_script_combined() { // If the main script with defer strategy has a `after` inline script; expected one with type='javascript' and other with type='module'. + unregister_all_script_handles(); wp_enqueue_script( 'ms-isinsa-1', 'http://example.org/ms-isinsa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isinsa-1', 'console.log("after one");', 'after', true ); wp_add_inline_script( 'ms-isinsa-1', 'console.log("after two");', 'after' ); - $output = get_echo( 'wp_print_scripts' ); - $expected = "\n"; - $expected .= "\n"; - $expected .= "\n"; + $output = get_echo( 'wp_print_scripts' ); + $expected = << +let wpLoadAfterScripts = ( handle ) => { + let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); + scripts.forEach( (script) => { + script.setAttribute("type","text/javascript"); + eval(script.innerHTML); + }) +} + + + + + +EXP; $this->assertSame( $expected, $output ); } @@ -99,6 +112,7 @@ public function data_standalone_after_inline_script() { $data = array(); // If the main script with defer strategy has a `after` inline script; the inline script is not affected. + unregister_all_script_handles(); wp_enqueue_script( 'ms-isa-1', 'http://example.org/ms-isa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isa-1', 'console.log("after one");', 'after', true ); $output = get_echo( 'wp_print_scripts' ); @@ -109,6 +123,7 @@ public function data_standalone_after_inline_script() { array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); // If the main script with async strategy has a `after` inline script; the inline script is not affected. + unregister_all_script_handles(); wp_enqueue_script( 'ms-isa-2', 'http://example.org/ms-isa-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isa-2', 'console.log("after one");', 'after', true ); $output = get_echo( 'wp_print_scripts' ); @@ -135,26 +150,53 @@ public function data_non_standalone_after_inline_script() { $data = array(); // If the main script with defer strategy has a `after` inline script; the inline script is inserted as type='module'. + unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-1', 'http://example.org/ms-insa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-insa-1', 'console.log("after one");', 'after' ); - $output = get_echo( 'wp_print_scripts' ); - $expected = "\n"; - $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected type="text/template" for inline script.' ) ); + $output = get_echo( 'wp_print_scripts' ); + $expected = << +let wpLoadAfterScripts = ( handle ) => { + let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); + scripts.forEach( (script) => { + script.setAttribute("type","text/javascript"); + eval(script.innerHTML); + }) +} + + + + +EXP; + array_push( $data, array( $expected, $output, 'Main Deferred; expected type="text/template" for inline script.' ) ); // If the main script with async strategy has a `after` inline script; the inline script is inserted as type='module'. - wp_enqueue_script( 'ms-insa-2', 'http://example.org/ms-insa-2.js', array(), null, array( 'strategy' => 'defer' ) ); + unregister_all_script_handles(); + wp_enqueue_script( 'ms-insa-2', 'http://example.org/ms-insa-2.js', array(), null, array( 'strategy' => 'async' ) ); wp_add_inline_script( 'ms-insa-2', 'console.log("after one");', 'after' ); - $output = get_echo( 'wp_print_scripts' ); - $expected = "\n"; - $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected type="text/template" for inline script.' ) ); + $output = get_echo( 'wp_print_scripts' ); + $expected = << +let wpLoadAfterScripts = ( handle ) => { + let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); + scripts.forEach( (script) => { + script.setAttribute("type","text/javascript"); + eval(script.innerHTML); + }) +} + + + + +EXP; + array_push( $data, array( $expected, $output, 'Main Async; expected type="text/template" for inline script.' ) ); // If the main script with blocking strategy has a `after` inline script; the inline script is inserted as type='javascript'. + unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-3', 'http://example.org/ms-insa-3.js', array(), null, array( 'strategy' => 'blocking' ) ); wp_add_inline_script( 'ms-insa-3', 'console.log("after one");', 'after' ); $output = get_echo( 'wp_print_scripts' ); @@ -162,9 +204,10 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); + array_push( $data, array( $expected, $output, 'Main blocking; expected no type attribute for inline script.' ) ); // If the main script with no strategy has a `after` inline script; the inline script is inserted as type='javascript'. + unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-4', 'http://example.org/ms-insa-4.js', array(), null ); wp_add_inline_script( 'ms-insa-4', 'console.log("after one");', 'after' ); $output = get_echo( 'wp_print_scripts' ); @@ -172,7 +215,7 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); + array_push( $data, array( $expected, $output, 'No strategy; expected no type attribute for inline script.' ) ); return $data; } @@ -191,6 +234,7 @@ public function data_non_standalone_before_inline_script_with_defer() { $data = array(); // If the main script has a `before` inline script; all dependencies will be blocking. + unregister_all_script_handles(); wp_enqueue_script( 'ds-i1-1', 'http://example.org/ds-i1-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i1-2', 'http://example.org/ds-i1-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i1-3', 'http://example.org/ds-i1-3.js', array(), null, array( 'strategy' => 'defer' ) ); @@ -207,6 +251,7 @@ public function data_non_standalone_before_inline_script_with_defer() { array_push( $data, array( $expected, $output, 'All dependency in the chain should be blocking' ) ); // One of the dependency in the chain has a `before` inline script; all script above it will be blocking. + unregister_all_script_handles(); wp_enqueue_script( 'ds-i2-1', 'http://example.org/ds-i2-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i2-2', 'http://example.org/ds-i2-2.js', array( 'ds-i2-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i2-3', 'http://example.org/ds-i2-3.js', array( 'ds-i2-2' ), null, array( 'strategy' => 'defer' ) ); @@ -223,6 +268,7 @@ public function data_non_standalone_before_inline_script_with_defer() { array_push( $data, array( $expected, $output, 'Scripts in the chain before the script having before must be blocking.' ) ); // Top most dependency in the chain has a `before` inline script; none of the script bellow it will be blocking. + unregister_all_script_handles(); wp_enqueue_script( 'ds-i3-1', 'http://example.org/ds-i3-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i3-2', 'http://example.org/ds-i3-2.js', array( 'ds-i3-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-i3-1', 'http://example.org/ms-i3-1.js', array( 'ds-i3-2' ), null, array( 'strategy' => 'defer' ) ); @@ -237,6 +283,7 @@ public function data_non_standalone_before_inline_script_with_defer() { array_push( $data, array( $expected, $output, 'Top most has before inline script. All the script in the chain defer.' ) ); // If there are two dependencies chain; rules are applied to the scripts in the chain having a `before` inline script. + unregister_all_script_handles(); wp_enqueue_script( 'ch1-ds-i4-1', 'http://example.org/ch1-ds-i4-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ch1-ds-i4-2', 'http://example.org/ch1-ds-i4-2.js', array( 'ch1-ds-i4-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ch2-ds-i4-1', 'http://example.org/ch2-ds-i4-1.js', array(), null, array( 'strategy' => 'defer' ) ); @@ -261,16 +308,17 @@ public function data_non_standalone_before_inline_script_with_defer() { * Test standalone tests. * * @ticket 12009 - * @dataProvider data_standalone_inline_script + * @dataProvider data_standalone_before_inline_script */ - public function test_standalone_inline_script( $expected, $output, $message ) { + public function test_standalone_before_inline_script( $expected, $output, $message ) { $this->assertSame( $expected, $output, $message ); } - public function data_standalone_inline_script() { + public function data_standalone_before_inline_script() { $data = array(); // If the main script has a `before` inline script; standalone doesn't effect any script. + unregister_all_script_handles(); wp_enqueue_script( 'ds-is1-1', 'http://example.org/ds-is1-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is1-2', 'http://example.org/ds-is1-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is1-3', 'http://example.org/ds-is1-3.js', array(), null, array( 'strategy' => 'defer' ) ); @@ -287,6 +335,7 @@ public function data_standalone_inline_script() { array_push( $data, array( $expected, $output, 'All dependency in the chain should be blocking' ) ); // One of the dependency in the chain has a `before` inline script; standalone doesn't effect any script. + unregister_all_script_handles(); wp_enqueue_script( 'ds-is2-1', 'http://example.org/ds-is2-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is2-2', 'http://example.org/ds-is2-2.js', array( 'ds-is2-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is2-3', 'http://example.org/ds-is2-3.js', array( 'ds-is2-2' ), null, array( 'strategy' => 'defer' ) ); From ffc40c11859bff3079f587c3238bcff0d6fd82c1 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 16 Mar 2023 02:48:14 +0000 Subject: [PATCH 24/57] php cs fixes --- src/wp-includes/script-loader.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index bf0b28c61c7e9..d2ae81708ee79 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1108,9 +1108,9 @@ function wp_default_scripts( $scripts ) { $scripts->add( 'mediaelement-vimeo', '/wp-includes/js/mediaelement/renderers/vimeo.min.js', array( 'mediaelement' ), '4.2.17', 1 ); $scripts->add( 'wp-mediaelement', "/wp-includes/js/mediaelement/wp-mediaelement$suffix.js", array( 'mediaelement' ), false, 1 ); $mejs_settings = array( - 'pluginPath' => includes_url( 'js/mediaelement/', 'relative' ), - 'classPrefix' => 'mejs-', - 'stretching' => 'responsive', + 'pluginPath' => includes_url( 'js/mediaelement/', 'relative' ), + 'classPrefix' => 'mejs-', + 'stretching' => 'responsive', /** This filter is documented in wp-includes/media.php */ 'audioShortcodeLibrary' => apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ), /** This filter is documented in wp-includes/media.php */ From 8af918c707d2003e25ec2a9a570b59331b10ae3b Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 16 Mar 2023 02:49:31 +0000 Subject: [PATCH 25/57] fix csfix issue --- src/wp-includes/script-loader.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index d2ae81708ee79..bf0b28c61c7e9 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1108,9 +1108,9 @@ function wp_default_scripts( $scripts ) { $scripts->add( 'mediaelement-vimeo', '/wp-includes/js/mediaelement/renderers/vimeo.min.js', array( 'mediaelement' ), '4.2.17', 1 ); $scripts->add( 'wp-mediaelement', "/wp-includes/js/mediaelement/wp-mediaelement$suffix.js", array( 'mediaelement' ), false, 1 ); $mejs_settings = array( - 'pluginPath' => includes_url( 'js/mediaelement/', 'relative' ), - 'classPrefix' => 'mejs-', - 'stretching' => 'responsive', + 'pluginPath' => includes_url( 'js/mediaelement/', 'relative' ), + 'classPrefix' => 'mejs-', + 'stretching' => 'responsive', /** This filter is documented in wp-includes/media.php */ 'audioShortcodeLibrary' => apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ), /** This filter is documented in wp-includes/media.php */ From d952da58a2a290b5b814369ecd7f6712903efaae Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Tue, 21 Mar 2023 11:18:09 +0000 Subject: [PATCH 26/57] Update test message. Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 985e2438cc70c..cd898dba8b207 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -204,7 +204,7 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Main blocking; expected no type attribute for inline script.' ) ); + array_push( $data, array( $expected, $output, 'The main script is blocking. Only the text/javascript type is expected for the inline script.' ) ); // If the main script with no strategy has a `after` inline script; the inline script is inserted as type='javascript'. unregister_all_script_handles(); From b71b5570e3f85e086e1f49b2a200c9ab72170da3 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Tue, 21 Mar 2023 11:20:40 +0000 Subject: [PATCH 27/57] Update test comment Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index cd898dba8b207..1570988f6bd6a 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -70,7 +70,7 @@ public function test_wp_enqueue_script() { * @ticket 12009 */ public function test_non_standalone_and_standalone_after_script_combined() { - // If the main script with defer strategy has a `after` inline script; expected one with type='javascript' and other with type='module'. + // If a main script with a `defer` strategy has an `after` inline script, we expect one instance with type='javascript' and another with type='text/template'. unregister_all_script_handles(); wp_enqueue_script( 'ms-isinsa-1', 'http://example.org/ms-isinsa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isinsa-1', 'console.log("after one");', 'after', true ); From 479d6567d07033b1c0d1a9de6335d0771e39f3e8 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Tue, 21 Mar 2023 11:21:10 +0000 Subject: [PATCH 28/57] Update test error message Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 1570988f6bd6a..321fc659b208f 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -215,7 +215,7 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'No strategy; expected no type attribute for inline script.' ) ); + array_push( $data, array( $expected, $output, 'No script strategy is present. Only the text/javascript type is expected for the inline script.' ) ); return $data; } From 968d5b7f36f3d5d999bf91a2411f4abd88e30a20 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 21 Mar 2023 12:41:38 +0000 Subject: [PATCH 29/57] change function names --- src/wp-includes/class-wp-scripts.php | 2 +- src/wp-includes/script-loader.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 521a5187859a6..4934de1be368a 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -793,7 +793,7 @@ public function add_data( $handle, $key, $value ) { * * @return bool True if script present. False if empty. */ - public function maybe_has_delayed_inline_script() { + public function has_delayed_inline_script() { foreach ( $this->registered as $handle => $script ) { if ( in_array( $this->get_intended_strategy( $handle ), array( 'defer', 'async' ), true ) ) { // non standalone after scripts of async or defer are usually delayed. diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index bf0b28c61c7e9..11b57280c5f58 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1849,7 +1849,7 @@ function wp_just_in_time_script_localization() { */ function wp_print_template_loader_script() { $wp_scripts = wp_scripts(); - if ( $wp_scripts->maybe_has_delayed_inline_script() ) { + if ( $wp_scripts->has_delayed_inline_script() ) { $output = << { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); From d522c46a384f1508360add117898d6437ffe8126 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:32:44 +0000 Subject: [PATCH 30/57] Update doc header. Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index a9b7f6a18a781..a2ea145ce6417 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -65,7 +65,7 @@ public function test_wp_enqueue_script() { /** - * Test standalone and non standalone after script one a single main script. + * Test standalone and non standalone inline scripts in the 'after' position of a single main script. * * @ticket 12009 */ From ce5fc8754dce14ff02a415912348370443345f4d Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:40:57 +0000 Subject: [PATCH 31/57] Update comment text Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index a2ea145ce6417..cc30d07e34b6e 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -98,7 +98,7 @@ public function test_standalone_after_inline_script( $expected, $output, $messag public function data_standalone_after_inline_script() { $data = array(); - // If the main script with defer strategy has a `after` inline script; the inline script is not affected. + // If the main script with a `defer` loading strategy has an `after` inline script, the inline script should not be affected. wp_enqueue_script( 'ms-isa-1', 'http://example.org/ms-isa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isa-1', 'console.log("after one");', 'after', true ); $output = get_echo( 'wp_print_scripts' ); From 0a0b253a26678d744ca6faab703870861fb73b63 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:41:15 +0000 Subject: [PATCH 32/57] comment text grammar. Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index cc30d07e34b6e..dd9a15b9b676a 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -108,7 +108,7 @@ public function data_standalone_after_inline_script() { $expected .= "\n"; array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); - // If the main script with async strategy has a `after` inline script; the inline script is not affected. + // If a main script with an `async` strategy has an `after` inline script, the inline script should not be affected. wp_enqueue_script( 'ms-isa-2', 'http://example.org/ms-isa-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isa-2', 'console.log("after one");', 'after', true ); $output = get_echo( 'wp_print_scripts' ); From a24665a434a2cfc9b8838c82529912f230a3eebd Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:41:54 +0000 Subject: [PATCH 33/57] update doc block Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index dd9a15b9b676a..0e92bff55d921 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -122,7 +122,7 @@ public function data_standalone_after_inline_script() { } /** - * Test non standalone after inline script. + * Test non standalone inline scripts in the `after` position. * * @ticket 12009 * @dataProvider data_non_standalone_after_inline_script From f29c14413b08af3e38fb3b080e8a2915f3a76178 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:43:06 +0000 Subject: [PATCH 34/57] comment grammar fixes Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 0e92bff55d921..59dfee30f85c4 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -134,7 +134,7 @@ public function test_non_standalone_after_inline_script( $expected, $output, $me public function data_non_standalone_after_inline_script() { $data = array(); - // If the main script with defer strategy has a `after` inline script; the inline script is inserted as type='module'. + // If a main script with a `defer` loading strategy has an `after` inline script, the inline script should be rendered as type='module'. wp_enqueue_script( 'ms-insa-1', 'http://example.org/ms-insa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-insa-1', 'console.log("after one");', 'after' ); $output = get_echo( 'wp_print_scripts' ); From 098865561e100a7fa78a6c39a5fcfa5cd4bf9901 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:43:23 +0000 Subject: [PATCH 35/57] comment grammar fixes Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 59dfee30f85c4..d9d49d7d01050 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -154,7 +154,7 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; array_push( $data, array( $expected, $output, 'Expected type="text/template" for inline script.' ) ); - // If the main script with blocking strategy has a `after` inline script; the inline script is inserted as type='javascript'. + // If a main script with a `blocking` strategy has an `after` inline script, the inline script should be rendered as type='javascript'. wp_enqueue_script( 'ms-insa-3', 'http://example.org/ms-insa-3.js', array(), null, array( 'strategy' => 'blocking' ) ); wp_add_inline_script( 'ms-insa-3', 'console.log("after one");', 'after' ); $output = get_echo( 'wp_print_scripts' ); From d66907e54822a512e44ded473df218bd263cee99 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:43:40 +0000 Subject: [PATCH 36/57] comment grammar Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index d9d49d7d01050..481295bb4f248 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -164,7 +164,7 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); - // If the main script with no strategy has a `after` inline script; the inline script is inserted as type='javascript'. + // If a main script with no loading strategy has an `after` inline script, the inline script should be rendered as type='javascript'. wp_enqueue_script( 'ms-insa-4', 'http://example.org/ms-insa-4.js', array(), null ); wp_add_inline_script( 'ms-insa-4', 'console.log("after one");', 'after' ); $output = get_echo( 'wp_print_scripts' ); From b7bff57d60a36f5dd8e1a1c7323ac2c9638ef12a Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:44:06 +0000 Subject: [PATCH 37/57] doc block grammar Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 481295bb4f248..7e19c49599ec8 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -178,7 +178,7 @@ public function data_non_standalone_after_inline_script() { } /** - * Test non standalone before with defer. + * Test non standalone inline scripts in the `before` position attached to deferred main scrips. * * @ticket 12009 * @dataProvider data_non_standalone_before_inline_script_with_defer From 013cb1e6380fbbeabc64d3ef1a140a48c8ff1852 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:44:24 +0000 Subject: [PATCH 38/57] comment grammar Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 7e19c49599ec8..ce28204b05e20 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -144,7 +144,7 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; array_push( $data, array( $expected, $output, 'Expected type="text/template" for inline script.' ) ); - // If the main script with async strategy has a `after` inline script; the inline script is inserted as type='module'. + // If a main script with an `async` loading strategy has an `after` inline script, the inline script should be rendered as type='module'. wp_enqueue_script( 'ms-insa-2', 'http://example.org/ms-insa-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-insa-2', 'console.log("after one");', 'after' ); $output = get_echo( 'wp_print_scripts' ); From db029256d493d5d232f558575ab9c2a9f2d66c2e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:44:54 +0000 Subject: [PATCH 39/57] update doc block for grammar Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index ce28204b05e20..c288fea77299a 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -70,7 +70,7 @@ public function test_wp_enqueue_script() { * @ticket 12009 */ public function test_non_standalone_and_standalone_after_script_combined() { - // If the main script with defer strategy has a `after` inline script; expected one with type='javascript' and other with type='module'. + // If a main script containing a `defer` strategy has an `after` inline script, the expected script type is type='javascript', otherwise type='text/template'. wp_enqueue_script( 'ms-isinsa-1', 'http://example.org/ms-isinsa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isinsa-1', 'console.log("after one");', 'after', true ); wp_add_inline_script( 'ms-isinsa-1', 'console.log("after two");', 'after' ); From 42bc576c4063e40b6d2d578b553ed8f9ecdcbc3e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:45:26 +0000 Subject: [PATCH 40/57] updated grammar Co-authored-by: Simon Dowdles <72872375+10upsimon@users.noreply.github.com> --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index c288fea77299a..1b1a54433c5f1 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -86,7 +86,7 @@ public function test_non_standalone_and_standalone_after_script_combined() { } /** - * Test standalone after inline script. + * Test `standalone` inline scripts in the `after` position. * * @ticket 12009 * @dataProvider data_standalone_after_inline_script From 326555db59abe330d2d33431667968377812e01c Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 22 Mar 2023 10:16:50 +0000 Subject: [PATCH 41/57] use unregister column --- tests/phpunit/includes/utils.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php index 7a4042b2cee7d..f4d7dafadd502 100644 --- a/tests/phpunit/includes/utils.php +++ b/tests/phpunit/includes/utils.php @@ -651,5 +651,7 @@ function test_rest_expand_compact_links( $links ) { */ function unregister_all_script_handles() { global $wp_scripts; - $wp_scripts->registered = array(); + foreach ( $wp_scripts->registered as $handle_name => $handle ) { + wp_deregister_script( $handle_name ); + } } From 1321d6f28fdeafe9fbfd6105523a57d48155587e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 23 Mar 2023 09:01:15 +0000 Subject: [PATCH 42/57] avoid removal of jquery libraries --- tests/phpunit/includes/utils.php | 36 +++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php index f4d7dafadd502..371b7c3d09fcc 100644 --- a/tests/phpunit/includes/utils.php +++ b/tests/phpunit/includes/utils.php @@ -651,7 +651,41 @@ function test_rest_expand_compact_links( $links ) { */ function unregister_all_script_handles() { global $wp_scripts; + + /** + * Do not deregister following library through this function. + */ + $libraries = array( + 'jquery', + 'jquery-core', + 'jquery-migrate', + 'jquery-ui-core', + 'jquery-ui-accordion', + 'jquery-ui-autocomplete', + 'jquery-ui-button', + 'jquery-ui-datepicker', + 'jquery-ui-dialog', + 'jquery-ui-draggable', + 'jquery-ui-droppable', + 'jquery-ui-menu', + 'jquery-ui-mouse', + 'jquery-ui-position', + 'jquery-ui-progressbar', + 'jquery-ui-resizable', + 'jquery-ui-selectable', + 'jquery-ui-slider', + 'jquery-ui-sortable', + 'jquery-ui-spinner', + 'jquery-ui-tabs', + 'jquery-ui-tooltip', + 'jquery-ui-widget', + 'backbone', + 'underscore', + ); + foreach ( $wp_scripts->registered as $handle_name => $handle ) { - wp_deregister_script( $handle_name ); + if ( ! in_array( $handle_name, $libraries, true ) ) { + wp_deregister_script( $handle_name ); + } } } From 66ab60c7ac4ba13181a0bb4eb1e4b3ca0c417eb2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 23 Mar 2023 09:08:07 +0000 Subject: [PATCH 43/57] Dataprovider doc block. --- tests/phpunit/tests/dependencies/scripts.php | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 289b131e49a0d..ddc3f10fdc4f9 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -107,6 +107,11 @@ public function test_standalone_after_inline_script( $expected, $output, $messag $this->assertSame( $expected, $output, $message ); } + /** + * Data provider. + * + * @return array + */ public function data_standalone_after_inline_script() { $data = array(); @@ -145,6 +150,11 @@ public function test_non_standalone_after_inline_script( $expected, $output, $me $this->assertSame( $expected, $output, $message ); } + /** + * Data provider. + * + * @return array + */ public function data_non_standalone_after_inline_script() { $data = array(); @@ -229,6 +239,11 @@ public function test_non_standalone_before_inline_script_with_defer( $expected, $this->assertSame( $expected, $output, $message ); } + /** + * Data provider. + * + * @return array + */ public function data_non_standalone_before_inline_script_with_defer() { $data = array(); @@ -313,6 +328,11 @@ public function test_standalone_before_inline_script( $expected, $output, $messa $this->assertSame( $expected, $output, $message ); } + /** + * Data provider. + * + * @return array + */ public function data_standalone_before_inline_script() { $data = array(); @@ -397,6 +417,11 @@ public function test_loading_strategy_with_valid_defer_registration( $expected, $this->assertStringContainsString( $expected, $output, $message ); } + /** + * Data provider. + * + * @return array + */ public function data_loading_strategy_with_valid_defer_registration() { $data = array(); From bbdad377686903cf3242102702abcfc8edfd2e8b Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 23 Mar 2023 09:15:22 +0000 Subject: [PATCH 44/57] doc block change --- tests/phpunit/tests/dependencies/scripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index ddc3f10fdc4f9..b2f221a616bc4 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -319,7 +319,7 @@ public function data_non_standalone_before_inline_script_with_defer() { } /** - * Test standalone tests. + * Test standalone `before` scripts. * * @ticket 12009 * @dataProvider data_standalone_before_inline_script From 966be0c59f0f43b36281e3b8d877bc667ff05bd2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Tue, 28 Mar 2023 14:24:40 +0100 Subject: [PATCH 45/57] Update src/wp-includes/class-wp-scripts.php Co-authored-by: Mukesh Panchal --- 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 70dc116fcea57..017721cb6bf6b 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -789,7 +789,7 @@ public function add_data( $handle, $key, $value ) { } /** - * Check all handles for any delayed inline scripts. + * Checks all handles for any delayed inline scripts. * * @return bool True if script present. False if empty. */ From dcbf0af2f6f754ef95061dff8d4cdfbf30fac3ad Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Fri, 31 Mar 2023 08:42:48 +0100 Subject: [PATCH 46/57] Update doc text Co-authored-by: Mukesh Panchal --- 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 017721cb6bf6b..0972dca46963e 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -791,7 +791,7 @@ public function add_data( $handle, $key, $value ) { /** * Checks all handles for any delayed inline scripts. * - * @return bool True if script present. False if empty. + * @return bool True if the inline script present, otherwise false. */ public function has_delayed_inline_script() { foreach ( $this->registered as $handle => $script ) { From 01c890e5133857a53f5c7b9a38267a9de9dd0dab Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 08:54:59 +0100 Subject: [PATCH 47/57] bug: multiple type in a single script. --- src/wp-includes/class-wp-scripts.php | 6 +++++- tests/phpunit/tests/dependencies/scripts.php | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 277fd3c368557..a5086324994a1 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -319,7 +319,11 @@ public function do_item( $handle, $group = false ) { $after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false ); if ( $after_non_standalone_handle ) { - $after_handle .= sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_non_standalone_handle ); + $initial_type_attr = $this->type_attr; + $this->type_attr = " type='text/template'"; + $after_handle .= sprintf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $after_non_standalone_handle ); + $this->type_attr = $initial_type_attr; + } } diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 4f50339a312f2..218d2ace456f9 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -78,7 +78,7 @@ public function test_non_standalone_and_standalone_after_script_combined() { $expected .= "\n"; - $expected .= "\n"; $this->assertSame( $expected, $output ); @@ -138,7 +138,7 @@ public function data_non_standalone_after_inline_script() { wp_add_inline_script( 'ms-insa-1', 'console.log("after one");', 'after' ); $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; - $expected .= "\n"; array_push( $data, array( $expected, $output, 'Expected type="text/template" for inline script.' ) ); @@ -148,7 +148,7 @@ public function data_non_standalone_after_inline_script() { wp_add_inline_script( 'ms-insa-2', 'console.log("after one");', 'after' ); $output = get_echo( 'wp_print_scripts' ); $expected = "\n"; - $expected .= "\n"; array_push( $data, array( $expected, $output, 'Expected type="text/template" for inline script.' ) ); From 9aaca52722225f3d308f61d2eb91ac578762084e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 09:05:38 +0100 Subject: [PATCH 48/57] print_display --- src/wp-includes/class-wp-scripts.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index a5086324994a1..2b857b0f7c9d6 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -503,7 +503,10 @@ public function print_inline_script( $handle, $position = 'after', $display = tr if ( $display ) { if ( 'after-non-standalone' === $position ) { - printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); + $initial_type_attr = $this->type_attr; + $this->type_attr = " type='text/template'"; + printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), $output ); + $this->type_attr = $initial_type_attr; } else { printf( "\n%s\n\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); } From b3f722575d9b7591cefefc541e8d993e75aa5ccf Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 11:09:22 +0100 Subject: [PATCH 49/57] fix type_attr no there issue --- src/wp-includes/script-loader.php | 5 +++-- tests/phpunit/tests/dependencies/scripts.php | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 11b57280c5f58..2501dab2cd32e 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1850,7 +1850,7 @@ function wp_just_in_time_script_localization() { function wp_print_template_loader_script() { $wp_scripts = wp_scripts(); if ( $wp_scripts->has_delayed_inline_script() ) { - $output = << { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { @@ -1859,7 +1859,8 @@ function wp_print_template_loader_script() { }) } JS; - printf( "\n", $output ); + $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : " type='text/javascript'"; + printf( "\n%s\n\n", $type_attr, $output ); } } diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 96fba823c56dc..0fa30a54ca628 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -76,7 +76,7 @@ public function test_non_standalone_and_standalone_after_script_combined() { wp_add_inline_script( 'ms-isinsa-1', 'console.log("after two");', 'after' ); $output = get_echo( 'wp_print_scripts' ); $expected = << +\n"; - array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); + $this->assertSame( $expected, $output ); + } - // If the main script with async strategy has a `after` inline script; the inline script is not affected. + /** + * Test `standalone` inline scripts in the `after` position. + * + * If the main script with async strategy has a `after` inline script, + * the inline script is not affected. + * + * @ticket 12009 + */ + public function test_standalone_async_main_script_with_after_inline_script() { unregister_all_script_handles(); wp_enqueue_script( 'ms-isa-2', 'http://example.org/ms-isa-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isa-2', 'console.log("after one");', 'after', true ); @@ -135,9 +134,7 @@ public function data_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); - - return $data; + $this->assertSame( $expected, $output ); } /** From 556ae91627f489d7fefcfcdab4c92b0b9e0086eb Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 12:06:42 +0100 Subject: [PATCH 52/57] non-standalone after script test broken --- tests/phpunit/tests/dependencies/scripts.php | 83 ++++++++++++-------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 972399ed1da9b..5a7b7904c24aa 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -98,14 +98,14 @@ public function test_non_standalone_and_standalone_after_script_combined() { } /** - * Test `standalone` inline scripts in the `after` position. + * Test `standalone` inline scripts in the `after` position with deferred main script. * - * If the main script with a `defer` loading strategy has an `after` inline script, + * If the main script with a `defer` loading strategy has an `after` inline script, * the inline script should not be affected. - * + * * @ticket 12009 */ - public function test_standalone_defer_main_script_with_after_inline_script() { + public function test_standalone_after_inline_script_with_defer_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ms-isa-1', 'http://example.org/ms-isa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isa-1', 'console.log("after one");', 'after', true ); @@ -118,14 +118,14 @@ public function test_standalone_defer_main_script_with_after_inline_script() { } /** - * Test `standalone` inline scripts in the `after` position. + * Test `standalone` inline scripts in the `after` position with async main script. + * + * If the main script with async strategy has a `after` inline script, + * the inline script should not be affected. * - * If the main script with async strategy has a `after` inline script, - * the inline script is not affected. - * * @ticket 12009 */ - public function test_standalone_async_main_script_with_after_inline_script() { + public function test_standalone_after_inline_script_with_async_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ms-isa-2', 'http://example.org/ms-isa-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-isa-2', 'console.log("after one");', 'after', true ); @@ -138,24 +138,15 @@ public function test_standalone_async_main_script_with_after_inline_script() { } /** - * Test non standalone inline scripts in the `after` position. + * Test non standalone inline scripts in the `after` position with deferred main script. * - * @ticket 12009 - * @dataProvider data_non_standalone_after_inline_script - */ - public function test_non_standalone_after_inline_script( $expected, $output, $message ) { - $this->assertSame( $expected, $output, $message ); - } - - /** - * Data provider. + * If a main script with a `defer` loading strategy has an `after` inline script, + * the inline script should be rendered as type='text/template'. + * The common loader script should also be injected in this case. * - * @return array + * @ticket 12009 */ - public function data_non_standalone_after_inline_script() { - $data = array(); - - // If a main script with a `defer` loading strategy has an `after` inline script, the inline script should be rendered as type='module'. + public function test_non_standalone_after_inline_script_with_defer_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-1', 'http://example.org/ms-insa-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-insa-1', 'console.log("after one");', 'after' ); @@ -176,9 +167,19 @@ public function data_non_standalone_after_inline_script() { EXP; - array_push( $data, array( $expected, $output, 'Main Deferred; expected type="text/template" for inline script.' ) ); + $this->assertSame( $expected, $output ); + } - // If a main script with an `async` loading strategy has an `after` inline script, the inline script should be rendered as type='module'. + /** + * Test non standalone inline scripts in the `after` position with async main script. + * + * If a main script with an `async` loading strategy has an `after` inline script, + * the inline script should be rendered as type='text/template'. + * The common loader script should also be injected in this case. + * + * @ticket 12009 + */ + public function test_non_standalone_after_inline_script_with_async_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-2', 'http://example.org/ms-insa-2.js', array(), null, array( 'strategy' => 'async' ) ); wp_add_inline_script( 'ms-insa-2', 'console.log("after one");', 'after' ); @@ -199,9 +200,18 @@ public function data_non_standalone_after_inline_script() { EXP; - array_push( $data, array( $expected, $output, 'Main Async; expected type="text/template" for inline script.' ) ); + $this->assertSame( $expected, $output ); + } - // If a main script with a `blocking` strategy has an `after` inline script, the inline script should be rendered as type='javascript'. + /** + * Test non standalone inline scripts in the `after` position with blocking main script. + * + * If a main script with a `blocking` strategy has an `after` inline script, + * the inline script should be rendered as type='text/javascript'. + * + * @ticket 12009 + */ + public function test_non_standalone_after_inline_script_with_blocking_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-3', 'http://example.org/ms-insa-3.js', array(), null, array( 'strategy' => 'blocking' ) ); wp_add_inline_script( 'ms-insa-3', 'console.log("after one");', 'after' ); @@ -210,9 +220,18 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); + $this->assertSame( $expected, $output ); + } - // If a main script with no loading strategy has an `after` inline script, the inline script should be rendered as type='javascript'. + /** + * Test non standalone inline scripts in the `after` position with deferred main script. + * + * If a main script with no loading strategy has an `after` inline script, + * the inline script should be rendered as type='text/javascript'. + * + * @ticket 12009 + */ + public function test_non_standalone_after_inline_script_with_main_script_with_no_strategy() { unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-4', 'http://example.org/ms-insa-4.js', array(), null ); wp_add_inline_script( 'ms-insa-4', 'console.log("after one");', 'after' ); @@ -221,9 +240,7 @@ public function data_non_standalone_after_inline_script() { $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Expected no type attribute for inline script.' ) ); - - return $data; + $this->assertSame( $expected, $output ); } /** From d4be88d31ed35d82f87dfd9115f327ba07c82fae Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 12:44:02 +0100 Subject: [PATCH 53/57] before standalone test broken --- tests/phpunit/tests/dependencies/scripts.php | 43 ++++++++++---------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 5a7b7904c24aa..9568daad7314c 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -333,31 +333,22 @@ public function data_non_standalone_before_inline_script_with_defer() { } /** - * Test standalone `before` scripts. + * Test `standalone` inline scripts in the `before` position with deferred main script. * - * @ticket 12009 - * @dataProvider data_standalone_before_inline_script - */ - public function test_standalone_before_inline_script( $expected, $output, $message ) { - $this->assertSame( $expected, $output, $message ); - } - - /** - * Data provider. + * If the main script has a `before` inline script, `standalone` doesn't apply to + * any inline script associated with the main script. * - * @return array + * @ticket 12009 */ - public function data_standalone_before_inline_script() { - $data = array(); - - // If the main script has a `before` inline script, `standalone` doesn't apply to any inline script associated with the main script. + public function test_standalone_before_inline_script_with_defer_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ds-is1-1', 'http://example.org/ds-is1-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is1-2', 'http://example.org/ds-is1-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is1-3', 'http://example.org/ds-is1-3.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-is1-1', 'http://example.org/ms-is1-1.js', array( 'ds-is1-1', 'ds-is1-2', 'ds-is1-3' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-is1-1', 'console.log("before one");', 'before', true ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; @@ -365,16 +356,27 @@ public function data_standalone_before_inline_script() { $expected .= "console.log(\"before one\");\n"; $expected .= "\n"; $expected .= "\n"; - array_push( $data, array( $expected, $output, 'All dependency in the chain should be blocking' ) ); - // If one of the dependencies in the chain has a `before` inline script associated with it, `standalone` doesn't apply to any inline script(s) associated with the main script. + $this->assertSame( $expected, $output ); + } + + /** + * Test `standalone` inline scripts in the `before` position with defer main script. + * + * If one of the deferred dependencies in the chain has a `before` inline `standalone` script associated with it, + * strategy of the dependencies above it remains unchanged. + * + * @ticket 12009 + */ + public function test_standalone_before_inline_script_with_defer_dependency_script() { unregister_all_script_handles(); wp_enqueue_script( 'ds-is2-1', 'http://example.org/ds-is2-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is2-2', 'http://example.org/ds-is2-2.js', array( 'ds-is2-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-is2-3', 'http://example.org/ds-is2-3.js', array( 'ds-is2-2' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-is2-1', 'http://example.org/ms-is2-1.js', array( 'ds-is2-3' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ds-is2-2', 'console.log("before one");', 'before', true ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Scripts in the chain before the script having before must be blocking.' ) ); - return $data; + $this->assertSame( $expected, $output ); } /** From 180a6ae87c1a27183d8f50de81e3e09db3352f50 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 13:16:10 +0100 Subject: [PATCH 54/57] non standalone with before and defer --- tests/phpunit/tests/dependencies/scripts.php | 80 +++++++++++++------- 1 file changed, 52 insertions(+), 28 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 9568daad7314c..44e0df80c5cd9 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -215,11 +215,13 @@ public function test_non_standalone_after_inline_script_with_blocking_main_scrip unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-3', 'http://example.org/ms-insa-3.js', array(), null, array( 'strategy' => 'blocking' ) ); wp_add_inline_script( 'ms-insa-3', 'console.log("after one");', 'after' ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; + $this->assertSame( $expected, $output ); } @@ -235,40 +237,32 @@ public function test_non_standalone_after_inline_script_with_main_script_with_no unregister_all_script_handles(); wp_enqueue_script( 'ms-insa-4', 'http://example.org/ms-insa-4.js', array(), null ); wp_add_inline_script( 'ms-insa-4', 'console.log("after one");', 'after' ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; + $this->assertSame( $expected, $output ); } /** * Test non standalone `before` inline scripts attached to deferred main scripts. * - * @ticket 12009 - * @dataProvider data_non_standalone_before_inline_script_with_defer - */ - public function test_non_standalone_before_inline_script_with_defer( $expected, $output, $message ) { - $this->assertSame( $expected, $output, $message ); - } - - /** - * Data provider. + * If the main script has a `before` inline script, all dependencies will be blocking. * - * @return array + * @ticket 12009 */ - public function data_non_standalone_before_inline_script_with_defer() { - $data = array(); - - // If the main script has a `before` inline script, all dependencies will be blocking. + public function test_non_standalone_before_inline_script_with_defer_main_script() { unregister_all_script_handles(); wp_enqueue_script( 'ds-i1-1', 'http://example.org/ds-i1-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i1-2', 'http://example.org/ds-i1-2.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i1-3', 'http://example.org/ds-i1-3.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-i1-1', 'http://example.org/ms-i1-1.js', array( 'ds-i1-1', 'ds-i1-2', 'ds-i1-3' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ms-i1-1', 'console.log("before one");', 'before' ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; @@ -276,16 +270,26 @@ public function data_non_standalone_before_inline_script_with_defer() { $expected .= "console.log(\"before one\");\n"; $expected .= "\n"; $expected .= "\n"; - array_push( $data, array( $expected, $output, 'All dependency in the chain should be blocking' ) ); - // If any of the dependencies in the chain have a `before` inline script, all scripts above it should be blocking. + $this->assertSame( $expected, $output ); + } + + /** + * Test non standalone `before` inline scripts attached to a dependency scripts in a all scripts `defer` chain. + * + * If any of the dependencies in the chain have a `before` inline script, all scripts above it should be blocking. + * + * @ticket 12009 + */ + public function test_non_standalone_before_inline_script_on_dependency_script() { unregister_all_script_handles(); wp_enqueue_script( 'ds-i2-1', 'http://example.org/ds-i2-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i2-2', 'http://example.org/ds-i2-2.js', array( 'ds-i2-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i2-3', 'http://example.org/ds-i2-3.js', array( 'ds-i2-2' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-i2-1', 'http://example.org/ms-i2-1.js', array( 'ds-i2-3' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ds-i2-2', 'console.log("before one");', 'before' ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Scripts in the chain before the script having before must be blocking.' ) ); - // If the top most dependency in the chain has a `before` inline script, none of the scripts bellow it will be blocking. + $this->assertSame( $expected, $output ); + } + + /** + * Test non standalone `before` inline scripts attached to top most dependency in a all scripts `defer` chain. + * + * If the top most dependency in the chain has a `before` inline script, + * none of the scripts bellow it will be blocking. + * + * @ticket 12009 + */ + public function test_non_standalone_before_inline_script_on_top_most_dependency_script() { unregister_all_script_handles(); wp_enqueue_script( 'ds-i3-1', 'http://example.org/ds-i3-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ds-i3-2', 'http://example.org/ds-i3-2.js', array( 'ds-i3-1' ), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ms-i3-1', 'http://example.org/ms-i3-1.js', array( 'ds-i3-2' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ds-i3-1', 'console.log("before one");', 'before' ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Top most has before inline script. All the script in the chain defer.' ) ); - // If there are two dependency chains, rules are applied to the scripts in the chain that contain a `before` inline script. + $this->assertSame( $expected, $output ); + } + + /** + * Test non standalone `before` inline scripts attached to one the chain, of the two all scripts `defer` chains. + * + * If there are two dependency chains, rules are applied to the scripts in the chain that contain a `before` inline script. + * + * @ticket 12009 + */ + public function test_non_standalone_before_inline_script_on_multiple_defer_script_chain() { unregister_all_script_handles(); wp_enqueue_script( 'ch1-ds-i4-1', 'http://example.org/ch1-ds-i4-1.js', array(), null, array( 'strategy' => 'defer' ) ); wp_enqueue_script( 'ch1-ds-i4-2', 'http://example.org/ch1-ds-i4-2.js', array( 'ch1-ds-i4-1' ), null, array( 'strategy' => 'defer' ) ); @@ -318,7 +342,8 @@ public function data_non_standalone_before_inline_script_with_defer() { wp_enqueue_script( 'ch2-ds-i4-2', 'http://example.org/ch2-ds-i4-2.js', array( 'ch2-ds-i4-1' ), null, array( 'strategy' => 'defer' ) ); wp_add_inline_script( 'ch2-ds-i4-2', 'console.log("before one");', 'before' ); wp_enqueue_script( 'ms-i4-1', 'http://example.org/ms-i4-1.js', array( 'ch2-ds-i4-1', 'ch2-ds-i4-2' ), null, array( 'strategy' => 'defer' ) ); - $output = get_echo( 'wp_print_scripts' ); + $output = get_echo( 'wp_print_scripts' ); + $expected = "\n"; $expected .= "\n"; $expected .= "\n"; @@ -327,9 +352,8 @@ public function data_non_standalone_before_inline_script_with_defer() { $expected .= "\n"; $expected .= "\n"; $expected .= "\n"; - array_push( $data, array( $expected, $output, 'Only top dependency script in chain two should be blocking.' ) ); - return $data; + $this->assertSame( $expected, $output ); } /** From 4d0140d83dc1c6126919c6122747fc9686f966d2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 14:34:30 +0100 Subject: [PATCH 55/57] used function instead of arrow function. --- src/wp-includes/script-loader.php | 6 +++--- tests/phpunit/tests/dependencies/scripts.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index c3db191fd8834..cf4b2289fcb9e 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1846,15 +1846,15 @@ function wp_just_in_time_script_localization() { /** * Prints a loader script if there is text/template registered script. - * - * When injected in DOM, this script converts any text/template script + * + * When injected in DOM, this script converts any text/template script * associated with a handle to type/javascript and execute them. */ function wp_print_template_loader_script() { $wp_scripts = wp_scripts(); if ( $wp_scripts->has_delayed_inline_script() ) { $output = << { +function wpLoadAfterScripts() { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript"); diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 44e0df80c5cd9..f16cefc76aa93 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -77,7 +77,7 @@ public function test_non_standalone_and_standalone_after_script_combined() { $output = get_echo( 'wp_print_scripts' ); $expected = << -let wpLoadAfterScripts = ( handle ) => { +function wpLoadAfterScripts() { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript"); @@ -153,7 +153,7 @@ public function test_non_standalone_after_inline_script_with_defer_main_script() $output = get_echo( 'wp_print_scripts' ); $expected = << -let wpLoadAfterScripts = ( handle ) => { +function wpLoadAfterScripts() { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript"); @@ -186,7 +186,7 @@ public function test_non_standalone_after_inline_script_with_async_main_script() $output = get_echo( 'wp_print_scripts' ); $expected = << -let wpLoadAfterScripts = ( handle ) => { +function wpLoadAfterScripts() { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript"); From a5b83a41c3d36d297100a45c6fea00f6ce59784f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 14:41:36 +0100 Subject: [PATCH 56/57] single condition --- src/wp-includes/class-wp-scripts.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 1c7883699aed4..10b81ceb6f23e 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -802,11 +802,11 @@ public function add_data( $handle, $key, $value ) { */ public function has_delayed_inline_script() { foreach ( $this->registered as $handle => $script ) { - if ( in_array( $this->get_intended_strategy( $handle ), array( 'defer', 'async' ), true ) ) { - // non standalone after scripts of async or defer are usually delayed. - if ( $this->has_non_standalone_inline_script( $handle, 'after' ) ) { - return true; - } + // non standalone after scripts of async or defer are usually delayed. + if ( in_array( $this->get_intended_strategy( $handle ), array( 'defer', 'async' ), true ) && + $this->has_non_standalone_inline_script( $handle, 'after' ) + ) { + return true; } } return false; From 026912803937679b7b9c72bb5af763a48cc9825d Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 31 Mar 2023 14:59:57 +0100 Subject: [PATCH 57/57] put back handle --- src/wp-includes/script-loader.php | 2 +- tests/phpunit/tests/dependencies/scripts.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index cf4b2289fcb9e..a3979e7bc1e6e 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1854,7 +1854,7 @@ function wp_print_template_loader_script() { $wp_scripts = wp_scripts(); if ( $wp_scripts->has_delayed_inline_script() ) { $output = << { script.setAttribute("type","text/javascript"); diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index f16cefc76aa93..090684c105e8b 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -77,7 +77,7 @@ public function test_non_standalone_and_standalone_after_script_combined() { $output = get_echo( 'wp_print_scripts' ); $expected = << -function wpLoadAfterScripts() { +function wpLoadAfterScripts( handle ) { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript"); @@ -153,7 +153,7 @@ public function test_non_standalone_after_inline_script_with_defer_main_script() $output = get_echo( 'wp_print_scripts' ); $expected = << -function wpLoadAfterScripts() { +function wpLoadAfterScripts( handle ) { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript"); @@ -186,7 +186,7 @@ public function test_non_standalone_after_inline_script_with_async_main_script() $output = get_echo( 'wp_print_scripts' ); $expected = << -function wpLoadAfterScripts() { +function wpLoadAfterScripts( handle ) { let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`); scripts.forEach( (script) => { script.setAttribute("type","text/javascript");