Make WordPress Core


Ignore:
Timestamp:
01/23/2024 05:54:25 PM (3 years ago)
Author:
westonruter
Message:

Script Loader: Only emit CDATA wrapper comments in wp_get_inline_script_tag() for JavaScript.

This avoids erroneously adding CDATA wrapper comments for non-JavaScript scripts, including those for JSON such as the importmap for script modules in #56313.

Props westonruter, flixos90, mukesh27, dmsnell.
See #56313.
Fixes #60320.

File:
1 edited

Legend:

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

    r56687 r57341  
    1111class Tests_Functions_wpInlineScriptTag extends WP_UnitTestCase {
    1212
     13        private $original_theme_features = array();
     14
     15        public function set_up() {
     16                global $_wp_theme_features;
     17                parent::set_up();
     18                $this->original_theme_features = $_wp_theme_features;
     19        }
     20
     21        public function tear_down() {
     22                global $_wp_theme_features;
     23                $_wp_theme_features = $this->original_theme_features;
     24                parent::tear_down();
     25        }
     26
    1327        private $event_handler = <<<'JS'
    1428document.addEventListener( 'DOMContentLoaded', function () {
     
    134148                );
    135149        }
     150
     151        public function data_provider_to_test_cdata_wrapper_omitted_for_non_javascript_scripts() {
     152                return array(
     153                        'no-type'     => array(
     154                                'type'           => null,
     155                                'data'           => 'alert("hello")',
     156                                'expected_cdata' => true,
     157                        ),
     158                        'js-type'     => array(
     159                                'type'           => 'text/javascript',
     160                                'data'           => 'alert("hello")',
     161                                'expected_cdata' => true,
     162                        ),
     163                        'js-alt-type' => array(
     164                                'type'           => 'application/javascript',
     165                                'data'           => 'alert("hello")',
     166                                'expected_cdata' => true,
     167                        ),
     168                        'module'      => array(
     169                                'type'           => 'module',
     170                                'data'           => 'alert("hello")',
     171                                'expected_cdata' => true,
     172                        ),
     173                        'importmap'   => array(
     174                                'type'           => 'importmap',
     175                                'data'           => '{"imports":{"bar":"http:\/\/localhost:10023\/bar.js?ver=6.5-alpha-57321"}}',
     176                                'expected_cdata' => false,
     177                        ),
     178                        'html'        => array(
     179                                'type'           => 'text/html',
     180                                'data'           => '<div>template code</div>',
     181                                'expected_cdata' => false,
     182                        ),
     183                        'json'        => array(
     184                                'type'           => 'application/json',
     185                                'data'           => '{}',
     186                                'expected_cdata' => false,
     187                        ),
     188                        'ld'          => array(
     189                                'type'           => 'application/ld+json',
     190                                'data'           => '{}',
     191                                'expected_cdata' => false,
     192                        ),
     193                        'specrules'   => array(
     194                                'type'           => 'speculationrules',
     195                                'data'           => '{}',
     196                                'expected_cdata' => false,
     197                        ),
     198                );
     199        }
     200
     201        /**
     202         * Tests that CDATA wrapper is not added for non-JavaScript scripts.
     203         *
     204         * @ticket 60320
     205         *
     206         * @dataProvider data_provider_to_test_cdata_wrapper_omitted_for_non_javascript_scripts
     207         */
     208        public function test_cdata_wrapper_omitted_for_non_javascript_scripts( $type, $data, $expected_cdata ) {
     209                remove_theme_support( 'html5' );
     210
     211                $attrs = array();
     212                if ( $type ) {
     213                        $attrs['type'] = $type;
     214                }
     215                $script = wp_get_inline_script_tag( $data, $attrs );
     216                $this->assertSame( $expected_cdata, str_contains( $script, '/* <![CDATA[ */' ) );
     217                $this->assertSame( $expected_cdata, str_contains( $script, '/* ]]> */' ) );
     218                $this->assertStringContainsString( $data, $script );
     219        }
    136220}
Note: See TracChangeset for help on using the changeset viewer.