Make WordPress Core


Ignore:
Timestamp:
01/31/2024 08:29:18 AM (13 months ago)
Author:
gziolo
Message:

Script Modules API: Add import map polyfill for older browsers

Syncs the changes from https://github.com/WordPress/gutenberg/pull/58263. Adds a polyfill to make import maps compatible with unsported browsers (https://caniuse.com/import-maps).

Fixes #60348.
Props cbravobernal, jorbin, luisherranz, jonsurrell.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r57244 r57492  
    31033103        $this->assertEqualSets( $expected_in_footer, wp_scripts()->in_footer, 'Expected to have the same handles for in_footer.' );
    31043104        $this->assertEquals( $expected_groups, wp_scripts()->groups, 'Expected groups to match.' );
     3105    }
     3106
     3107    /**
     3108     * Test that get_script_polyfill() returns the correct polyfill.
     3109     *
     3110     * @ticket 60348
     3111     *
     3112     * @covers ::wp_get_script_polyfill
     3113     *
     3114     * @global WP_Scripts $wp_scripts WP_Scripts instance.
     3115     */
     3116    public function test_wp_get_script_polyfill() {
     3117        global $wp_scripts;
     3118        $script_name = 'wp-polyfill-importmap';
     3119        $test_script = 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")';
     3120        $script_url  = 'https://example.com/wp-polyfill-importmap.js';
     3121        wp_register_script( $script_name, $script_url );
     3122
     3123        $polyfill = wp_get_script_polyfill(
     3124            $wp_scripts,
     3125            array(
     3126                $test_script => $script_name,
     3127            )
     3128        );
     3129
     3130        wp_deregister_script( $script_name );
     3131
     3132        $expected = '( ' . $test_script . ' ) || document.write( \'<script src="' . $script_url . '"></scr\' + \'ipt>\' );';
     3133
     3134        $this->assertEquals( $expected, $polyfill );
    31053135    }
    31063136
Note: See TracChangeset for help on using the changeset viewer.