Make WordPress Core


Ignore:
Timestamp:
12/29/2025 11:32:17 AM (3 months ago)
Author:
jonsurrell
Message:

Scripts: Remove non-HTML5 script support.

Remove the following behaviors that are obsolete in HTML5:

  • CDATA wrappers around SCRIPT tag contents.
  • Conversion of boolean attributes to strings (attribute async="async" becomes async).

HTML5 was released in 2008 and data suggests virtually all WordPress sites are served as HTML5. See #59883 for more details.

Developed in https://github.com/WordPress/wordpress-develop/pull/10660.

Props jonsurrell, westonruter, azaozz, soyebsalar01, dmsnell.
Fixes #64442. See #59883.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/script-loader.php

    r61414 r61415  
    22112211        if ( ! empty( $wp_scripts->print_code ) ) {
    22122212            echo "\n<script>\n";
    2213             echo "/* <![CDATA[ */\n"; // Not needed in HTML 5.
    22142213            echo $wp_scripts->print_code;
    22152214            echo sprintf( "\n//# sourceURL=%s\n", rawurlencode( 'js-inline-concat-' . $concat ) );
    2216             echo "/* ]]> */\n";
    22172215            echo "</script>\n";
    22182216        }
     
    28722870 */
    28732871function wp_sanitize_script_attributes( $attributes ) {
    2874     $html5_script_support = is_admin() || current_theme_supports( 'html5', 'script' );
    2875     $attributes_string    = '';
     2872    $attributes_string = '';
    28762873
    28772874    /*
     
    28822879        if ( is_bool( $attribute_value ) ) {
    28832880            if ( $attribute_value ) {
    2884                 $attributes_string .= $html5_script_support ? ' ' . esc_attr( $attribute_name ) : sprintf( ' %1$s="%2$s"', esc_attr( $attribute_name ), esc_attr( $attribute_name ) );
     2881                $attributes_string .= ' ' . esc_attr( $attribute_name );
    28852882            }
    28862883        } else {
     
    29452942 */
    29462943function wp_get_inline_script_tag( $data, $attributes = array() ) {
    2947     /*
    2948      * XHTML extracts the contents of the SCRIPT element and then the XML parser
    2949      * decodes character references and other syntax elements. This can lead to
    2950      * misinterpretation of the script contents or invalid XHTML documents.
    2951      *
    2952      * Wrapping the contents in a CDATA section instructs the XML parser not to
    2953      * transform the contents of the SCRIPT element before passing them to the
    2954      * JavaScript engine.
    2955      *
    2956      * Example:
    2957      *
    2958      *     <script>console.log('&hellip;');</script>
    2959      *
    2960      *     In an HTML document this would print "&hellip;" to the console,
    2961      *     but in an XHTML document it would print "…" to the console.
    2962      *
    2963      *     <script>console.log('An image is <img> in HTML');</script>
    2964      *
    2965      *     In an HTML document this would print "An image is <img> in HTML",
    2966      *     but it's an invalid XHTML document because it interprets the `<img>`
    2967      *     as an empty tag missing its closing `/`.
    2968      *
    2969      * @see https://www.w3.org/TR/xhtml1/#h-4.8
    2970      */
    2971     if (
    2972         ( ! current_theme_supports( 'html5', 'script' ) && ! is_admin() )
    2973         && (
    2974             ! isset( $attributes['type'] ) ||
    2975             'module' === $attributes['type'] ||
    2976             str_contains( $attributes['type'], 'javascript' ) ||
    2977             str_contains( $attributes['type'], 'ecmascript' ) ||
    2978             str_contains( $attributes['type'], 'jscript' ) ||
    2979             str_contains( $attributes['type'], 'livescript' )
    2980         )
    2981     ) {
    2982         /*
    2983          * If the string `]]>` exists within the JavaScript it would break
    2984          * out of any wrapping CDATA section added here, so to start, it's
    2985          * necessary to escape that sequence which requires splitting the
    2986          * content into two CDATA sections wherever it's found.
    2987          *
    2988          * Note: it's only necessary to escape the closing `]]>` because
    2989          * an additional `<![CDATA[` leaves the contents unchanged.
    2990          */
    2991         $data = str_replace( ']]>', ']]]]><![CDATA[>', $data );
    2992 
    2993         // Wrap the entire escaped script inside a CDATA section.
    2994         $data = sprintf( "/* <![CDATA[ */\n%s\n/* ]]> */", $data );
    2995     }
    2996 
    29972944    $data = "\n" . trim( $data, "\n\r " ) . "\n";
    29982945
Note: See TracChangeset for help on using the changeset viewer.