Skip to content

Commit

Permalink
Update tests and prevent wp_print_inline_script_tag fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal committed Jan 30, 2024
1 parent f76f80f commit ea3ab1f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
20 changes: 11 additions & 9 deletions src/wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,19 @@ public function print_import_map() {
$import_map = $this->get_import_map();
if ( ! empty( $import_map['imports'] ) ) {
global $wp_scripts;
wp_print_inline_script_tag(
wp_get_script_polyfill(
$wp_scripts,
if ( isset( $wp_scripts ) ) {
wp_print_inline_script_tag(
wp_get_script_polyfill(
$wp_scripts,
array(
'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")' => 'wp-polyfill-importmap',
)
),
array(
'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")' => 'wp-polyfill-importmap',
'id' => 'wp-load-polyfill-importmap',
)
),
array(
'id' => 'wp-load-polyfill-importmap',
)
);
);
}
wp_print_inline_script_tag(
wp_json_encode( $import_map, JSON_HEX_TAG | JSON_HEX_AMP ),
array(
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/tests/script-modules/wpScriptModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,9 @@ public function test_wp_enqueue_script_module_registers_all_params() {
* @covers ::print_import_map_polyfill()
*/

public function test_wp_print_import_map_polyfill() {
public function test_wp_print_import_map_has_polyfill() {
// Polyfill is empty when no modules are registered.
$import_map_polyfill = get_echo( array( $this->script_modules, 'print_import_map_polyfill' ) );
$import_map_polyfill = get_echo( array( $this->script_modules, 'print_import_map' ) );

$this->assertEquals( '', $import_map_polyfill );

Expand All @@ -623,7 +623,7 @@ public function test_wp_print_import_map_polyfill() {

$this->script_modules->enqueue( 'foo', '/foo.js', array( 'dep' ), '1.0' );
$this->script_modules->register( 'dep', '/dep.js' );
$import_map_polyfill = get_echo( array( $this->script_modules, 'print_import_map_polyfill' ) );
$import_map_polyfill = get_echo( array( $this->script_modules, 'print_import_map' ) );
$p = new WP_HTML_Tag_Processor( $import_map_polyfill );
$p->next_tag( array( 'tag' => 'SCRIPT' ) );
$id = $p->get_attribute( 'id' );
Expand Down

0 comments on commit ea3ab1f

Please sign in to comment.